Browse Source

Scoping service worker to /ipfs/ so / can contain other pages.

Dan 1 year ago
parent
commit
6e51b26c9c
5 changed files with 9 additions and 11 deletions
  1. 1
    1
      index.html
  2. 1
    0
      package.json
  3. 2
    7
      src/fetcher.js
  4. 4
    2
      src/index.js
  5. 1
    1
      webpack.config.js

+ 1
- 1
index.html View File

@@ -4,6 +4,6 @@
4 4
     <style type="text/css">body {margin: 0}</style>
5 5
   </head>
6 6
   <body>
7
-    <script type="text/javascript" src="/bundle.js"></script>
7
+    <script type="text/javascript" src="/ipfs/bundle.js"></script>
8 8
   </body>
9 9
 </html>

+ 1
- 0
package.json View File

@@ -3,6 +3,7 @@
3 3
   "version": "1.0.0",
4 4
   "description": "Bundle js-ipfs with WebPack",
5 5
   "scripts": {
6
+    "build": "node node_modules/.bin/webpack -p && cp index.html dist/index.html",
6 7
     "start": "node server.js"
7 8
   },
8 9
   "license": "MIT",

+ 2
- 7
src/fetcher.js View File

@@ -81,13 +81,8 @@ self.addEventListener('fetch', (event) => {
81 81
 
82 82
     const request_path = (new URL(event.request.url)).pathname
83 83
 
84
-    // If this isn't an IPFS URL, bail.
85
-    if (!request_path.startsWith('/ipfs/')) {
86
-        console.log('not a valid IPFS hash:', request_path)
87
-        if (request_path != '/bundle.js' && request_path != '/fetcher.js') {
88
-            event.respondWith(not_found_response())
89
-        }
90
-        return
84
+    if (request_path == '/ipfs/bundle.js' || request_path == '/ipfs/fetcher.js') {
85
+        return console.log('skipping bundles')
91 86
     }
92 87
 
93 88
     // If this is a same-origin or CORS request, and it's not to a URL within the same base IPFS

+ 4
- 2
src/index.js View File

@@ -10,13 +10,15 @@ function App() {
10 10
 
11 11
     function create() {
12 12
         if ('serviceWorker' in navigator) {
13
-            navigator.serviceWorker.register('/fetcher.js')
13
+            navigator.serviceWorker.register('/ipfs/fetcher.js')
14 14
             .then((registration) => {
15 15
                 console.log('registered the service worker successfully')
16 16
 
17 17
                 // Now that a service worker is registered to intercept URLs, reload the page so
18 18
                 // this page's URL gets loaded from IPFS by the service worker.
19
-                window.location.reload(false)
19
+                if (window.location.pathname.startsWith('/ipfs/')) {
20
+                    window.location.reload(false)
21
+                }
20 22
             })
21 23
             .catch((err) => {
22 24
                 console.log('failed to register the service worker:', err)

+ 1
- 1
webpack.config.js View File

@@ -14,7 +14,7 @@ module.exports = {
14 14
     path: path.join(__dirname, 'dist'),
15 15
     filename: '[name].js',
16 16
     sourceMapFilename: '[name].js.map',
17
-    publicPath: '/'
17
+    publicPath: '/ipfs/'
18 18
   },
19 19
   plugins: [
20 20
 //    new UglifyJSPlugin()

Loading…
Cancel
Save