我有一个 SvelteKit/Tauri 项目,所有项目都需要预渲染,因为它将是一个桌面应用程序。在我的 SvelteKit 项目中,我有以下路由,其中包含一个 slug:
/edit/calculator/[calculator_id]
slug 参数是一个
id
,用于使用给定的 id
查询计算器,然后显示计算器属性。我事先不知道嵌入式数据库中的 calculator_id
参数,这使得在编译/构建时路由未知。
构建项目时,出现以下错误:
node:internal/event_target:1033
process.nextTick(() => { throw err; });
^
Error: The following routes were marked as prerenderable, but were not prerendered because they were not found while crawling your app:
- /edit/calculator/[calculator_id]
See https://kit.svelte.dev/docs/page-options#prerender-troubleshooting for info on how to solve this
at prerender (file:///C:/Users/ono/Documents/Programmering/versa-flow/node_modules/@sveltejs/kit/src/core/postbuild/prerender.js:475:9)
at async MessagePort.<anonymous> (file:///C:/Users/ono/Documents/Programmering/versa-flow/node_modules/@sveltejs/kit/src/utils/fork.js:22:16)
Emitted 'error' event on Worker instance at:
at [kOnErrorMessage] (node:internal/worker:300:10)
at [kOnMessage] (node:internal/worker:311:37)
at MessagePort.<anonymous> (node:internal/worker:212:57)
at [nodejs.internal.kHybridDispatch] (node:internal/event_target:757:20)
at exports.emitMessage (node:internal/per_context/messageport:23:28)
我尝试查看错误消息中提到的文档,它谈到将特定页面指定为可预渲染,但我没有正确理解它,也不知道如何通过阅读文档来解决此问题。
解释指出,只有存在指向具有特定参数值的所述路由的链接时,才可以预渲染动态参数。如果参数值未知,则无法预渲染动态路线。
换句话说,您不可能预渲染所有可能的 URL 组合。您可以按照文档中的说明在
entries
选项中列出显式案例,或者忘记预渲染。
总结:如果不先为路线参数赋值,就无法预渲染。