SvelteKit
SvelteKit 是 Svelte 的一个元框架。有关 SvelteKit 的更多信息,请访问 https://svelte.net.cn/。本指南在 SvelteKit 2.20.4 / Svelte 5.25.8 版本下准确无误。
- 通过
static-adapter
使用 SSG 和 SPA。Tauri 不支持基于服务器的解决方案。 - 如果使用 SSG 进行预渲染,请注意在您的应用程序构建过程中,
load
函数将无法访问 Tauri API。建议使用 SPA 模式(不预渲染),因为 load 函数只会在 webview 中运行,并可访问 Tauri API。 - 在
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,但它使得使用依赖全局窗口对象(如 Tauri API)的 API 成为可能,而无需进行客户端检查。此外,如果您更喜欢静态网站生成 (SSG) 而不是单页应用程序 (SPA) 模式,可以根据适配器文档更改适配器配置和
+layout.ts
。
© 2025 Tauri 贡献者。CC-BY / MIT