SvelteKit
SvelteKit 是 Svelte 的元框架。在 https://svelte.net.cn/ 了解更多关于 SvelteKit 的信息。本指南适用于 SvelteKit 2.20.4 / Svelte 5.25.8 版本。
- 通过
static-adapter使用 SSG 和 SPA 模式。Tauri 不支持基于服务器的解决方案。 - 如果在使用 SSG 且开启预渲染(prerendering)时,请注意
load函数在应用构建过程中将无法访问 Tauri API。建议使用 SPA 模式(不开启预渲染),因为此时 load 函数仅在具备 Tauri API 访问权限的 webview 中运行。 - 在
tauri.conf.json中将build/设置为frontendDist。
-
npm install --save-dev @sveltejs/adapter-staticyarn add -D @sveltejs/adapter-staticpnpm add -D @sveltejs/adapter-staticdeno add -D npm:@sveltejs/adapter-static
-
tauri.conf.json {"build": {"beforeDevCommand": "npm run dev","beforeBuildCommand": "npm run build","devUrl": "https://:5173","frontendDist": "../build"}}tauri.conf.json {"build": {"beforeDevCommand": "yarn dev","beforeBuildCommand": "yarn build","devUrl": "https://:5173","frontendDist": "../build"}}tauri.conf.json {"build": {"beforeDevCommand": "pnpm dev","beforeBuildCommand": "pnpm build","devUrl": "https://:5173","frontendDist": "../build"}}tauri.conf.json {"build": {"beforeDevCommand": "deno task dev","beforeBuildCommand": "deno task build","devUrl": "https://:5173","frontendDist": "../build"}} -
svelte.config.js import adapter from '@sveltejs/adapter-static';import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';/** @type {import('@sveltejs/kit').Config} */const config = {// Consult https://svelte.net.cn/docs/kit/integrations#preprocessors// for more information about preprocessorspreprocess: vitePreprocess(),kit: {adapter: adapter({fallback: 'index.html',}),},};export default config; -
最后,我们需要通过添加一个根目录下的
+layout.ts文件(如果你不使用 TypeScript,则为+layout.js)来禁用 SSR,文件内容如下:src/routes/+layout.ts export const ssr = false;请注意,
static-adapter并不强制要求你为整个应用禁用 SSR,但这样做可以让你使用依赖于全局 window 对象(如 Tauri 的 API)的接口,而无需进行 客户端检查。此外,如果你更倾向于使用静态网站生成(SSG)而非单页应用(SPA)模式,可以根据 适配器文档 修改适配器配置和
+layout.ts。
© 2026 Tauri 贡献者。CC-BY / MIT