fix(pwa): rename serwist route from [[...slug]] to [path] to match Serwist's hardcoded param
The Serwist createSerwistRoute factory returns a GET handler with
`params: Promise<{ path: string }>` hardcoded. Next.js infers the
param shape from the directory name — so the directory MUST be
named `[path]` (single segment) for the type to match.
`[[...slug]]` (catch-all) was wrong on two counts:
1. The segment name is `slug`, not `path` (Serwist expects
exactly `path`).
2. The shape is `string[]` (array), not `string` (Serwist
expects a single string).
Per the Serwist source comment: 'Asset and chunk names must be at
the top, as our path is /serwist/[path], not /serwist/[...path]'
— all SW output files (sw.js, workbox-XXX.js) are flat, single
segment is sufficient.
The fix is a directory rename only — the route.ts content
(createSerwistRoute call) is unchanged.
Verification:
- pnpm build now succeeds (TypeScript check passes)
- The Serwist route appears in the build output as `● /serwist/[path]`
prerendered with `sw.js` and `sw.js.map` static pages
- `pnpm start` + Chrome: `/serwist/sw.js` will be served by the
route handler and registered by <SerwistProvider>
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { createSerwistRoute } from "@serwist/turbopack";
|
||||
|
||||
/**
|
||||
* Cozsweet Serwist Service Worker Route Handler
|
||||
*
|
||||
* 路径:捕获 `/serwist/*` 所有子路径(典型是 `/serwist/sw.js` 主 SW + 一些 Serwist 内部 chunk)
|
||||
* 因此 `[[...slug]]` 是必须的 —— 仅 `app/serwist/sw.js` 一条不够,Serwist 还会
|
||||
* 拉取 `/serwist/<chunk>.js` 等辅助资源。
|
||||
*
|
||||
* `createSerwistRoute` 在 build 阶段用 esbuild 把 `src/app/sw.ts` 编译成真实 SW,
|
||||
* 并在 runtime 把编译产物通过 GET handler 吐出去。
|
||||
*
|
||||
* Revision 用 `git rev-parse HEAD` —— 每次 commit 变化让 precache 失效,强制客户端
|
||||
* 拉新 SW。fallback 到 randomUUID(罕见的非 git 环境兜底)。
|
||||
*/
|
||||
const revision =
|
||||
spawnSync("git", ["rev-parse", "HEAD"], { encoding: "utf-8" }).stdout ??
|
||||
crypto.randomUUID();
|
||||
|
||||
export const { dynamic, dynamicParams, revalidate, generateStaticParams, GET } =
|
||||
createSerwistRoute({
|
||||
additionalPrecacheEntries: [{ url: "/~offline", revision }],
|
||||
swSrc: "src/app/sw.ts",
|
||||
useNativeEsbuild: true,
|
||||
});
|
||||
Reference in New Issue
Block a user