跳到内容
Tauri

SvelteKit

SvelteKit 是一个用于 Svelte 的元框架。在 https://kit.svelte.net.cn/ 了解更多关于 SvelteKit 的信息。本指南截至 SvelteKit 2.20.4 / Svelte 5.25.8 是准确的。

检查清单

  • 通过 static-adapter 使用 SSGSPA。Tauri 不支持基于服务器的解决方案。
  • 如果使用带预渲染的 SSG,请注意 load 函数在应用的构建过程中将无法访问 tauri API。建议使用 SPA 模式(不带预渲染),因为 load 函数将仅在具有访问 tauri API 权限的 webview 中运行。
  • tauri.conf.json 中使用 build/ 作为 frontendDist

配置示例

  1. npm install --save-dev @sveltejs/adapter-static
  2. tauri.conf.json
    {
    "build": {
    "beforeDevCommand": "npm run dev",
    "beforeBuildCommand": "npm run build",
    "devUrl": "http://localhost:5173",
    "frontendDist": "../build"
    }
    }
  3. 更新 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 preprocessors
    preprocess: vitePreprocess(),
    kit: {
    adapter: adapter({
    fallback: 'index.html',
    }),
    },
    };
    export default config;
  4. 禁用 SSR

    最后,我们需要通过添加一个根 +layout.ts 文件(如果您不使用 TypeScript,则为 +layout.js)并包含以下内容来禁用 SSR

    src/routes/+layout.ts
    export const ssr = false;

    请注意,static-adapter 不要求您为整个应用程序禁用 SSR,但它使您可以使用依赖于全局 window 对象(如 Tauri 的 API)的 API,而无需客户端检查

    此外,如果您更喜欢静态站点生成 (SSG) 而不是单页应用程序 (SPA) 模式,您可以根据 适配器文档更改适配器配置和 +layout.ts


© 2025 Tauri 贡献者。CC-BY / MIT