mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +00:00
register page for browser
This commit is contained in:
parent
37c7e892d5
commit
0b51fb3bc3
@ -6,14 +6,47 @@ import { SigmaPreset, SigmaContext, SigmaInput } from '../context'
|
|||||||
import { worker } from './worker'
|
import { worker } from './worker'
|
||||||
|
|
||||||
export const browser: SigmaPreset = extendPreset(worker, (input: SigmaInput) => {
|
export const browser: SigmaPreset = extendPreset(worker, (input: SigmaInput) => {
|
||||||
|
const routerBase = input._nuxt.routerBase
|
||||||
|
|
||||||
const script = `<script>
|
const script = `<script>
|
||||||
if ('serviceWorker' in navigator) {
|
if ('serviceWorker' in navigator) {
|
||||||
window.addEventListener('load', function () {
|
window.addEventListener('load', function () {
|
||||||
navigator.serviceWorker.register('${input._nuxt.routerBase}_server/index.js');
|
navigator.serviceWorker.register('${routerBase}sw.js');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
</script>`
|
</script>`
|
||||||
|
|
||||||
|
// TEMP FIX
|
||||||
|
const html = `<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<link rel="prefetch" href="${routerBase}sw.js">
|
||||||
|
<link rel="prefetch" href="${routerBase}_server/index.js">
|
||||||
|
<script>
|
||||||
|
async function register () {
|
||||||
|
const registration = await navigator.serviceWorker.register('${routerBase}sw.js')
|
||||||
|
await navigator.serviceWorker.ready
|
||||||
|
registration.active.addEventListener('statechange', (event) => {
|
||||||
|
if (event.target.state === 'activated') {
|
||||||
|
window.location.reload()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
if (location.hostname !== 'localhost' && location.protocol === 'http:') {
|
||||||
|
location.replace(location.href.replace('http://', 'https://'))
|
||||||
|
} else {
|
||||||
|
register()
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
Loading...
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>`
|
||||||
|
|
||||||
return <SigmaInput> {
|
return <SigmaInput> {
|
||||||
entry: '{{ _internal.runtimeDir }}/entries/service-worker',
|
entry: '{{ _internal.runtimeDir }}/entries/service-worker',
|
||||||
output: {
|
output: {
|
||||||
@ -32,12 +65,12 @@ if ('serviceWorker' in navigator) {
|
|||||||
tmpl.compiled = tmpl.compiled.replace('</body>', script + '</body>')
|
tmpl.compiled = tmpl.compiled.replace('</body>', script + '</body>')
|
||||||
},
|
},
|
||||||
async 'sigma:compiled' ({ output }: SigmaContext) {
|
async 'sigma:compiled' ({ output }: SigmaContext) {
|
||||||
await writeFile(resolve(output.publicDir, 'sw.js'), 'self.importScripts(\'/_server/index.js\');')
|
await writeFile(resolve(output.publicDir, 'sw.js'), `self.importScripts('${input._nuxt.routerBase}_server/index.js');`)
|
||||||
|
|
||||||
// Temp fix
|
// Temp fix
|
||||||
await writeFile(resolve(output.publicDir, 'index.html'), script)
|
await writeFile(resolve(output.publicDir, 'index.html'), html)
|
||||||
await writeFile(resolve(output.publicDir, '200.html'), script)
|
await writeFile(resolve(output.publicDir, '200.html'), html)
|
||||||
await writeFile(resolve(output.publicDir, '404.html'), script)
|
await writeFile(resolve(output.publicDir, '404.html'), html)
|
||||||
|
|
||||||
consola.info('Ready to deploy to static hosting:', prettyPath(output.publicDir as string))
|
consola.info('Ready to deploy to static hosting:', prettyPath(output.publicDir as string))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user