To prevent reload loops, return a 404 from the service worker if the URL isn't a valid IPFS URL.

This commit is contained in:
Dan 2017-12-22 21:42:13 -08:00
parent c8854bcbd2
commit 6bc10e2ef2
2 changed files with 22 additions and 11 deletions

View File

@ -56,13 +56,30 @@ function response(status_code, status_text, body, headers={}) {
}) })
} }
function not_found_response() {
return response(
404, 'Not Found',
Buffer.from('<html><head><title>404 Not Found</title></head><body bgcolor="white"><center><h1>404 Not Found</h1></center><hr><center>Intergalactic</center></body></html>'),
{'Content-Type': 'text/html; charset=utf-8'}
)
}
self.addEventListener('fetch', (event) => { 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) 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) { if (!ipfs_initialized) {
ipfs_initialized = initialize_ipfs() ipfs_initialized = initialize_ipfs()
@ -70,8 +87,6 @@ self.addEventListener('fetch', (event) => {
event.respondWith( event.respondWith(
ipfs_initialized.then(() => { ipfs_initialized.then(() => {
const url = new URL(event.request.url)
const multihash = url.pathname
return node.files.get(multihash) return node.files.get(multihash)
}).then((files) => { }).then((files) => {
// If there's just one result, return it. // 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) console.log('file not found at', event.request.url)
return response( return not_found_response()
404, 'Not Found',
Buffer.from('<html><head><title>404 Not Found</title></head><body bgcolor="white"><center><h1>404 Not Found</h1></center><hr><center>Intergalactic</center></body></html>'),
{'Content-Type': 'text/html; charset=utf-8'}
)
}).catch((error) => { }).catch((error) => {
console.log(error) console.log(error)
}) })

View File

@ -12,14 +12,14 @@ function App() {
if ('serviceWorker' in navigator) { if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/fetcher.js') navigator.serviceWorker.register('/fetcher.js')
.then((registration) => { .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 // 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. // this page's URL gets loaded from IPFS by the service worker.
window.location.reload(false) window.location.reload(false)
}) })
.catch((err) => { .catch((err) => {
console.log('Failed to register:', err) console.log('failed to register the service worker:', err)
}) })
} }
} }