Created
November 16, 2018 16:18
-
-
Save cristoni/b55d862a77dae9f4381ada3a817c7a59 to your computer and use it in GitHub Desktop.
File _app.js per Nextjs com registrazione service worker
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
import App, { Container } from 'next/app' | |
export default class MyApp extends App { | |
static async getInitialProps({ Component, router, ctx }) { | |
let pageProps = {} | |
if (Component.getInitialProps) { | |
pageProps = await Component.getInitialProps(ctx) | |
} | |
return { pageProps } | |
} | |
componentDidMount() { | |
if ('serviceWorker' in navigator) { | |
window.addEventListener('load', () => { | |
navigator.serviceWorker.register('/service-worker.js').then(registration => { | |
console.log('SW registered: ', registration); | |
}).catch(registrationError => { | |
console.log('SW registration failed: ', registrationError); | |
}); | |
}); | |
} | |
} | |
render () { | |
const { Component, pageProps } = this.props | |
return ( | |
<Container> | |
<Component {...pageProps} /> | |
</Container> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment