Skip to content

Instantly share code, notes, and snippets.

@cristoni
Created November 16, 2018 16:18
Show Gist options
  • Save cristoni/b55d862a77dae9f4381ada3a817c7a59 to your computer and use it in GitHub Desktop.
Save cristoni/b55d862a77dae9f4381ada3a817c7a59 to your computer and use it in GitHub Desktop.
File _app.js per Nextjs com registrazione service worker
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