Unverified Commit bc9a4da4 authored by Balázs Orbán's avatar Balázs Orbán Committed by GitHub

chore: update `with-service-worker` example (#38670)

Fixes #38613

Given `navigator.serviceWorker` [has ~96% support in browsers](https://caniuse.com/?search=navigator.serviceWorker), also removed the check for `"serviceWorker" in navigator`. 

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
parent de7aa2d6
/// <reference types="next" />
/// <reference types="next/image-types/global" />
// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
......@@ -7,7 +7,12 @@
},
"dependencies": {
"next": "latest",
"react": "^17.0.2",
"react-dom": "^17.0.2"
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/node": "^18.0.4",
"@types/react": "^18.0.15",
"typescript": "^4.7.4"
}
}
import { useEffect } from 'react'
function MyApp({ Component, pageProps }) {
useEffect(() => {
if ('serviceWorker' in navigator) {
window.addEventListener('load', function () {
navigator.serviceWorker.register('/sw.js').then(
function (registration) {
console.log(
'Service Worker registration successful with scope: ',
registration.scope
)
},
function (err) {
console.log('Service Worker registration failed: ', err)
}
)
})
}
}, [])
return <Component {...pageProps} />
}
export default MyApp
export default function Home() {
return <h1>with-service-worker</h1>
}
import { useEffect } from 'react'
export default function Home() {
useEffect(() => {
navigator.serviceWorker
.register('/sw.js')
.then((registration) =>
console.log(
'Service Worker registration successful with scope: ',
registration.scope
)
)
.catch((err) => console.log('Service Worker registration failed: ', err))
}, [])
return <h1>with-service-worker</h1>
}
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment