-
-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add SSR workbox support #66
Draft
userquin
wants to merge
2
commits into
main
Choose a base branch
from
userquin/feat-ssr-pages-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<script setup lang="ts"> | ||
import type { NuxtError } from '#app' | ||
|
||
// prevent reactive update when clearing error | ||
const { error } = defineProps<{ | ||
error: Partial<NuxtError> | ||
}>() | ||
console.error(error) | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<NuxtLayout> | ||
<h1>Error</h1> | ||
<NuxtLink to="/"> | ||
Back Home | ||
</NuxtLink> | ||
</NuxtLayout> | ||
</div> | ||
</template> |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
<template> | ||
<div> | ||
<div>Nuxt Vite PWA</div> | ||
<h1>About</h1> | ||
<NuxtLink to="/"> | ||
Home 1 | ||
Go to Home | ||
</NuxtLink> | ||
</div> | ||
</template> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<template> | ||
<div> | ||
<h3 text-2xl font-500> | ||
Hi, | ||
</h3> | ||
<div text-xl> | ||
<NuxtPage :page-key="page => page.fullPath" /> | ||
</div> | ||
|
||
<div> | ||
<NuxtLink | ||
class="m-3 text-sm btn" | ||
to="/" | ||
> | ||
Back | ||
</NuxtLink> | ||
</div> | ||
</div> | ||
</template> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<script setup lang="ts"> | ||
const route = useRoute<'hi-id-detail'>() | ||
const name = route.params.id ?? '' | ||
if (process.server) { | ||
// eslint-disable-next-line no-console | ||
console.log('Server side', name) | ||
} | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<div text-xl> | ||
Detail {{ name }} | ||
</div> | ||
<NuxtLink :to="`/hi/${name}`" class="m-3 text-sm btn"> | ||
Back Hi | ||
</NuxtLink> | ||
</div> | ||
</template> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<script setup lang="ts"> | ||
const route = useRoute<'hi-id'>() | ||
const name = route.params.id ?? '' | ||
if (process.server) { | ||
// eslint-disable-next-line no-console | ||
console.log('Server side', name) | ||
} | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<div text-xl> | ||
{{ name }}! | ||
</div> | ||
<NuxtLink :to="`/hi/${name}/detail`" class="m-3 text-sm btn"> | ||
Detail | ||
</NuxtLink> | ||
</div> | ||
</template> |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<template> | ||
<div> | ||
<h1>List</h1> | ||
<NuxtPage /> | ||
<NuxtLink to="/"> | ||
Go to index | ||
</nuxtlink> | ||
</div> | ||
</template> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<script setup lang="ts"> | ||
definePageMeta({ | ||
key: route => route.fullPath, | ||
}) | ||
const route = useRoute<'list-id'>() | ||
const name = route.params.id ?? '' | ||
if (process.server) { | ||
// eslint-disable-next-line no-console | ||
console.log('Server side', name) | ||
} | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<h1>Child</h1> | ||
<NuxtLink to="/list"> | ||
Back | ||
</NuxtLink> | ||
</div> | ||
</template> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<template> | ||
<div> | ||
<NuxtPage /> | ||
<NuxtLink to="/list"> | ||
Back | ||
</NuxtLink> | ||
</div> | ||
</template> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<template> | ||
<div> | ||
<h1>Example</h1> | ||
</div> | ||
</template> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<template> | ||
<div> | ||
<h1>Example 2</h1> | ||
<NuxtLink to="/list"> | ||
Back | ||
</NuxtLink> | ||
</div> | ||
</template> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<script setup lang="ts"> | ||
|
||
</script> | ||
|
||
<template> | ||
<div> | ||
<NuxtLink to="/list/1"> | ||
Go to /list/1 | ||
</NuxtLink> | ||
<br> | ||
<NuxtLink to="/list/2"> | ||
Go to /list/2 | ||
</NuxtLink> | ||
<br> | ||
<NuxtLink to="/list/example"> | ||
Go to example | ||
</NuxtLink> | ||
<br> | ||
<NuxtLink to="/list/example2"> | ||
Go to example 2 | ||
</NuxtLink> | ||
</div> | ||
</template> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
import type { Nuxt } from '@nuxt/schema' | ||
import type { Nuxt, NuxtPage } from '@nuxt/schema' | ||
import { resolve } from 'pathe' | ||
import type { NitroConfig } from 'nitropack' | ||
import type { RuntimeCaching } from 'workbox-build' | ||
import type { ModuleOptions } from './types' | ||
|
||
export function configurePWAOptions(options: ModuleOptions, nuxt: Nuxt, nitroConfig: NitroConfig) { | ||
export function configurePWAOptions( | ||
options: ModuleOptions, | ||
nuxt: Nuxt, | ||
nitroConfig: NitroConfig, | ||
ssrPages?: NuxtPage[], | ||
) { | ||
if (!options.outDir) { | ||
const publicDir = nitroConfig.output?.publicDir ?? nuxt.options.nitro?.output?.publicDir | ||
options.outDir = publicDir ? resolve(publicDir) : resolve(nuxt.options.buildDir, '../.output/public') | ||
|
@@ -20,6 +26,7 @@ export function configurePWAOptions(options: ModuleOptions, nuxt: Nuxt, nitroCon | |
> | ||
|
||
if (options.strategies === 'injectManifest') { | ||
// TODO: handle enableSSR, we need to inject the routeRules with a custom recipe | ||
options.injectManifest = options.injectManifest ?? {} | ||
config = options.injectManifest | ||
} | ||
|
@@ -35,6 +42,40 @@ export function configurePWAOptions(options: ModuleOptions, nuxt: Nuxt, nitroCon | |
if (options.devOptions?.enabled && !options.devOptions.navigateFallbackAllowlist) | ||
options.devOptions.navigateFallbackAllowlist = [nuxt.options.app.baseURL ? new RegExp(nuxt.options.app.baseURL) : /\//] | ||
} | ||
else if (ssrPages?.length) { | ||
const { | ||
cache = false, | ||
cacheName = 'ssr-pages', | ||
offlinePage = `${nuxt.options.app.baseURL ?? '/'}error?offline`, | ||
} = options.enableSSR! | ||
// 1. filter prerender pages: will go to the sw precache manifest | ||
// what happens if prerender is enabled but prerender routes with ssr? | ||
// don't use nitroConfig, we have _nuxt and __nuxt_error | ||
const prerenderPages = nuxt.options.nitro.prerender?.routes ?? [] | ||
const rules = collectRules(ssrPages.filter(p => !prerenderPages.includes(p.path))) | ||
// 2. prepare runtime caching for ssr pages | ||
const routesInfo = Array.from(rules.keys()).map(r => createSSRHandler(r)) | ||
// 3. configure workbox properly: since we have two builds, we need to check if previously added | ||
if (routesInfo.length > 0) { | ||
const navigateFallbackDenylist = options.workbox.navigateFallbackDenylist ?? [] | ||
const runtimeCaching = options.workbox.runtimeCaching ?? [] | ||
routesInfo.forEach((r) => { | ||
const path = r.urlPattern as string | ||
const { exp, dynamic } = rules.get(path)! | ||
const source = exp.source | ||
if (!navigateFallbackDenylist.some(d => d.source === source)) | ||
navigateFallbackDenylist.push(exp) | ||
|
||
const index = runtimeCaching.findIndex(d => d.urlPattern === path) | ||
if (index > -1) | ||
runtimeCaching[index] = createSSRHandlerFunction(cache, cacheName, offlinePage, exp, dynamic) | ||
else | ||
runtimeCaching.push(r) | ||
}) | ||
options.workbox.navigateFallbackDenylist = navigateFallbackDenylist | ||
options.workbox.runtimeCaching = runtimeCaching | ||
} | ||
} | ||
config = options.workbox | ||
} | ||
if (!nuxt.options.dev) | ||
|
@@ -59,3 +100,88 @@ function createManifestTransform(base: string): import('workbox-build').Manifest | |
return { manifest: entries, warnings: [] } | ||
} | ||
} | ||
|
||
function createSSRHandler(route: string): RuntimeCaching { | ||
return { | ||
urlPattern: route, | ||
handler: 'NetworkOnly', | ||
} | ||
} | ||
|
||
function createSSRHandlerFunction( | ||
cache: boolean, | ||
cacheName: string, | ||
offlinePage: string, | ||
regex: RegExp, | ||
dynamic: boolean, | ||
): RuntimeCaching { | ||
return cache && !dynamic | ||
// eslint-disable-next-line no-eval | ||
? eval(`() => ({ | ||
urlPattern: ({ url, sameOrigin }) => sameOrigin && url.pathname.match(${regex}), | ||
handler: 'NetworkFirst', | ||
options: { | ||
cacheName: ${JSON.stringify(cacheName)}, | ||
cacheableResponse: { | ||
statuses: [200] | ||
}, | ||
matchOptions: { | ||
ignoreVary: true, | ||
ignoreSearch: true | ||
}, | ||
plugins: [{ | ||
handlerDidError: async () => Response.redirect(${JSON.stringify(offlinePage)}, 302), | ||
cacheWillUpdate: async ({ response }) => response.status === 200 ? response : null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. prevent error here (response can be null/undefined when offline)? |
||
}] | ||
} | ||
})`)() | ||
// eslint-disable-next-line no-eval | ||
: eval(`() => ({ | ||
urlPattern: ({ url, sameOrigin }) => sameOrigin && url.pathname.match(${regex}), | ||
handler: 'NetworkOnly', | ||
options: { | ||
matchOptions: { | ||
ignoreVary: true, | ||
ignoreSearch: true | ||
}, | ||
plugins: [{ | ||
handlerDidError: async () => Response.redirect(${JSON.stringify(offlinePage)}, 302), | ||
cacheWillUpdate: async () => null | ||
}] | ||
} | ||
})`)() | ||
} | ||
|
||
function createRouteRegex(path: string) { | ||
const dynamicRoute = path.indexOf(':') | ||
return dynamicRoute > -1 | ||
? { exp: new RegExp(`^${path.slice(0, dynamicRoute)}`), dynamic: true } | ||
: { exp: new RegExp(`^${path}$`), dynamic: false } | ||
} | ||
|
||
function traversePage(page: NuxtPage, rules: Map<string, { exp: RegExp; dynamic: boolean }>, parentRoute?: string) { | ||
const path = `${parentRoute ? `${parentRoute}/` : ''}${page.path}` | ||
const route = createRouteRegex(path) | ||
rules.set(path, route) | ||
if (!route.dynamic) { | ||
if (page.children?.length) { | ||
page.children?.filter(p => p.path !== '') | ||
.sort((a, b) => { | ||
if (a.path.startsWith(':')) | ||
return 1 | ||
|
||
if (b.path.startsWith(':')) | ||
return -1 | ||
|
||
return b.path.length - a.path.length | ||
}) | ||
.forEach(p => traversePage(p, rules, path)) | ||
} | ||
} | ||
} | ||
|
||
function collectRules(ssrPages: NuxtPage[]) { | ||
const rules = new Map<string, { exp: RegExp; dynamic: boolean }>() | ||
ssrPages.forEach(p => traversePage(p, rules)) | ||
return rules | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add purge on quota error