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:
parent
c8854bcbd2
commit
6bc10e2ef2
|
@ -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) => {
|
||||
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('<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'}
|
||||
)
|
||||
return not_found_response()
|
||||
}).catch((error) => {
|
||||
console.log(error)
|
||||
})
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Reference in New Issue