Created
May 6, 2018 09:04
-
-
Save ScriptedAlchemy/66cf483c44d5296a3a2e39f0e218082f to your computer and use it in GitHub Desktop.
IPFS gateway worker interceptor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const IPFS = require('ipfs') | |
function App() { | |
let node | |
create() | |
function create() { | |
if ('serviceWorker' in navigator) { | |
const this_script_url = document.currentScript.src; | |
const base_script_url = this_script_url.slice(0, this_script_url.lastIndexOf('/')) | |
navigator.serviceWorker.register(base_script_url + '/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. | |
if (window.location.pathname.startsWith('/ipfs/')) { | |
window.location.reload(false) | |
} | |
}) | |
.catch((err) => { | |
console.log('failed to register the service worker:', err) | |
}) | |
} | |
} | |
} | |
module.exports = App | |
App() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment