diff --git a/src/fetcher.js b/src/fetcher.js
index 012ad0f..d8100a5 100644
--- a/src/fetcher.js
+++ b/src/fetcher.js
@@ -56,13 +56,30 @@ function response(status_code, status_text, body, headers={}) {
})
}
+function not_found_response() {
+ return response(
+ 404, 'Not Found',
+ Buffer.from('
404 Not Found404 Not Found
Intergalactic'),
+ {'Content-Type': 'text/html; charset=utf-8'}
+ )
+}
+
self.addEventListener('fetch', (event) => {
- if (!event.request.url.startsWith(self.location.origin + '/ipfs')) {
+ if (!event.request.url.startsWith(self.location.origin)) {
return console.log('Fetch not in scope:', event.request.url)
}
- console.log('Handling fetch event:', event.request.url)
+ console.log('handling fetch event:', event.request.url)
+ const url = new URL(event.request.url)
+ const multihash = url.pathname
+
+ // If this isn't an IPFS URL, bail.
+ if (!multihash.startsWith('/ipfs/')) {
+ console.log('not a valid IPFS hash:', multihash)
+ event.respondWith(not_found_response())
+ return
+ }
if (!ipfs_initialized) {
ipfs_initialized = initialize_ipfs()
@@ -70,8 +87,6 @@ self.addEventListener('fetch', (event) => {
event.respondWith(
ipfs_initialized.then(() => {
- const url = new URL(event.request.url)
- const multihash = url.pathname
return node.files.get(multihash)
}).then((files) => {
// If there's just one result, return it.
@@ -97,11 +112,7 @@ self.addEventListener('fetch', (event) => {
}
console.log('file not found at', event.request.url)
- return response(
- 404, 'Not Found',
- Buffer.from('404 Not Found404 Not Found
Intergalactic'),
- {'Content-Type': 'text/html; charset=utf-8'}
- )
+ return not_found_response()
}).catch((error) => {
console.log(error)
})
diff --git a/src/index.js b/src/index.js
index cdf090d..1842fc6 100644
--- a/src/index.js
+++ b/src/index.js
@@ -12,14 +12,14 @@ function App() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/fetcher.js')
.then((registration) => {
- console.log('Registered the service worker successfully')
+ console.log('registered the service worker successfully')
// Now that a service worker is registered to intercept URLs, reload the page so
// this page's URL gets loaded from IPFS by the service worker.
window.location.reload(false)
})
.catch((err) => {
- console.log('Failed to register:', err)
+ console.log('failed to register the service worker:', err)
})
}
}