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

This commit is contained in:
Dan 2017-12-25 12:41:01 -08:00
parent f6ad1010bc
commit 6e51b26c9c
5 changed files with 9 additions and 11 deletions

View File

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

View File

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

View File

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

View File

@ -10,13 +10,15 @@ function App() {
function create() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/fetcher.js')
navigator.serviceWorker.register('/ipfs/fetcher.js')
.then((registration) => {
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)
if (window.location.pathname.startsWith('/ipfs/')) {
window.location.reload(false)
}
})
.catch((err) => {
console.log('failed to register the service worker:', err)

View File

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