SvelteKit
SvelteKit 是一个用于 Svelte 的元框架。在 https://kit.svelte.net.cn/ 了解更多关于 SvelteKit 的信息。本指南截至 SvelteKit 2.20.4 / Svelte 5.25.8 是准确的。
检查清单
- 通过
static-adapter
使用 SSG 和 SPA。Tauri 不支持基于服务器的解决方案。 - 如果使用带预渲染的 SSG,请注意
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": "http://localhost:5173","frontendDist": "../build"}}tauri.conf.json {"build": {"beforeDevCommand": "yarn dev","beforeBuildCommand": "yarn build","devUrl": "http://localhost:5173","frontendDist": "../build"}}tauri.conf.json {"build": {"beforeDevCommand": "pnpm dev","beforeBuildCommand": "pnpm build","devUrl": "http://localhost:5173","frontendDist": "../build"}}tauri.conf.json {"build": {"beforeDevCommand": "deno task dev","beforeBuildCommand": "deno task build","devUrl": "http://localhost:5173","frontendDist": "../build"}} -
更新 SvelteKit 配置
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://kit.svelte.net.cn/docs/integrations#preprocessors// for more information about preprocessorspreprocess: vitePreprocess(),kit: {adapter: adapter({fallback: 'index.html',}),},};export default config; -
禁用 SSR
最后,我们需要通过添加一个根
+layout.ts
文件(如果您不使用 TypeScript,则为+layout.js
)并包含以下内容来禁用 SSRsrc/routes/+layout.ts export const ssr = false;请注意,
static-adapter
不要求您为整个应用程序禁用 SSR,但它使您可以使用依赖于全局 window 对象(如 Tauri 的 API)的 API,而无需客户端检查。此外,如果您更喜欢静态站点生成 (SSG) 而不是单页应用程序 (SPA) 模式,您可以根据 适配器文档更改适配器配置和
+layout.ts
。
© 2025 Tauri 贡献者。CC-BY / MIT