Skip to content
Tauri 中文网

SvelteKit

SvelteKit 是 Svelte 的元框架。在 https://kit.svelte.dev/ 了解有关 SvelteKit 的更多信息。本指南适用于 SvelteKit 2.20.4 / Svelte 5.25.8 及更高版本。

¥SvelteKit is a meta framework for Svelte. Learn more about SvelteKit at https://kit.svelte.dev/. This guide is accurate as of SvelteKit 2.20.4 / Svelte 5.25.8.

检查清单

¥Checklist

  • 通过 static-adapter 使用 SSGSPA。Tauri 不支持基于服务器的解决方案。

    ¥Use SSG and SPA via static-adapter. Tauri doesn’t support server-based solutions.

  • 如果你使用带预渲染功能的 SSG,请注意,在应用构建过程中,load 函数将无法访问 tauri API。建议使用 SPA 模式(不带预渲染),因为加载函数仅在能够访问 tauri API 的 Web 视图中运行。

    ¥If using SSG with prerendering, be aware that load functions will not have access to tauri APIs during the build process of your app. Using SPA mode (without prerendering) is recommended since the load functions will only run in the webview with access to tauri APIs.

  • tauri.conf.json 中将 build/ 用作 frontendDist

    ¥Use build/ as frontendDist in tauri.conf.json.

示例配置

¥Example Configuration

  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. Update SvelteKit configuration:
    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.dev/docs/integrations#preprocessors
    // for more information about preprocessors
    preprocess: vitePreprocess(),
    kit: {
    adapter: adapter({
    fallback: 'index.html',
    }),
    },
    };
    export default config;
  4. Disable SSR

    Lastly, we need to disable SSR by adding a root +layout.ts file (or +layout.js if you are not using TypeScript) with these contents:

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

    Note that static-adapter doesn’t require you to disable SSR for the whole app but it makes it possible to use APIs that depend on the global window object (like Tauri’s API) without Client-side checks.

    Furthermore, if you prefer Static Site Generation (SSG) over Single-Page Application (SPA) mode, you can change the adapter configurations and +layout.ts according to the adapter docs.


Tauri v2.6 中文网 - 粤ICP备13048890号
Nodejs.cn 旗下网站