Kawiesh

Pwa

Oct 15th, 2021 (edited)
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Source: https://www.ibrahima-ndaw.com/blog/how-to-build-pwa-with-javascript */
  2.  
  3.  
  4. //HTML File---------------------------------------------
  5. <head>
  6. <link rel="manifest" href="manifest.json">
  7. </head>
  8.  
  9. //------------------------------------------------------
  10. //manifest.json (Root directory)
  11. {
  12. "name": "My Site",
  13. "short_name": "Site",
  14. "start_url": "index.html",
  15. "display": "standalone",
  16. "background_color": "#fdfdfd",
  17. "theme_color": "#db4938",
  18. "orientation": "portrait-primary",
  19. "icons": [
  20.     {
  21.       "src": "/images/icons/icon-72x72.png",
  22.       "type": "image/png",
  23.       "sizes": "72x72"
  24.     },
  25.     {
  26.       "src": "/images/icons/icon-96x96.png",
  27.       "type": "image/png",
  28.       "sizes": "96x96"
  29.     },
  30.     {
  31.       "src": "/images/icons/icon-128x128.png",
  32.       "type": "image/png",
  33.       "sizes": "128x128"
  34.     },
  35.     {
  36.       "src": "/images/icons/icon-144x144.png",
  37.       "type": "image/png",
  38.       "sizes": "144x144"
  39.     },
  40.     {
  41.       "src": "/images/icons/icon-152x152.png",
  42.       "type": "image/png",
  43.       "sizes": "152x152"
  44.     },
  45.     {
  46.       "src": "/images/icons/icon-192x192.png",
  47.       "type": "image/png",
  48.       "sizes": "192x192"
  49.     },
  50.     {
  51.       "src": "/images/icons/icon-384x384.png",
  52.       "type": "image/png",
  53.       "sizes": "384x384"
  54.     },
  55.     {
  56.       "src": "/images/icons/icon-512x512.png",
  57.       "type": "image/png",
  58.       "sizes": "512x512"
  59.     }
  60.   ]
  61. }
  62.  
  63. //------------------------------------------------------
  64. //script.js (or internal HTML script)
  65. if ("serviceWorker" in navigator) {
  66. window.addEventListener("load", function() {
  67. navigator.serviceWorker
  68. .register("/serviceWorker.js")
  69. .then(r => console.log("Success"))
  70. .catch(x => console.log(x));
  71. });
  72. }
  73.  
  74. //------------------------------------------------------
  75. //serviceWorker.js (Root directory)
  76. const mySite= "my-site-v1";
  77. const files= [
  78. "/",
  79. "index.html",
  80. "style.css",
  81. "script.js",
  82. "/resources/picture.jpg"
  83. ];
  84.  
  85. self.addEventListener("install", installEvent => {
  86. installEvent.waitUntil(
  87. caches.open(mySite).then(cache => {
  88. cache.addAll(files);
  89. })
  90. );
  91. });
  92.  
  93. self.addEventListener("fetch", fetchEvent => {
  94. fetchEvent.respondWith(
  95. caches.match(fetchEvent.request).then(res => {
  96. return res || fetch(fetchEvent.request);
  97. })
  98. );
  99. });
  100. //------------------------------------------------------
  101.  
  102.  
Add Comment
Please, Sign In to add comment