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> <style type="text/css">body {margin: 0}</style>
</head> </head>
<body> <body>
<script type="text/javascript" src="/bundle.js"></script> <script type="text/javascript" src="/ipfs/bundle.js"></script>
</body> </body>
</html> </html>

View File

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

View File

@ -81,13 +81,8 @@ self.addEventListener('fetch', (event) => {
const request_path = (new URL(event.request.url)).pathname const request_path = (new URL(event.request.url)).pathname
// If this isn't an IPFS URL, bail. if (request_path == '/ipfs/bundle.js' || request_path == '/ipfs/fetcher.js') {
if (!request_path.startsWith('/ipfs/')) { return console.log('skipping bundles')
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 this is a same-origin or CORS request, and it's not to a URL within the same base IPFS // 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() { function create() {
if ('serviceWorker' in navigator) { if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/fetcher.js') navigator.serviceWorker.register('/ipfs/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) if (window.location.pathname.startsWith('/ipfs/')) {
window.location.reload(false)
}
}) })
.catch((err) => { .catch((err) => {
console.log('failed to register the service worker:', err) console.log('failed to register the service worker:', err)

View File

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