360 Secure SkillHub
360 安全技能中心

nextjs-to-tauri

面向可静态导出的 Next.js 16 App Router 项目,指导添加 Tauri 2 桌面壳、处理路径与路由、集成外部链接/语言/可选更新钩子,并生成版本同步、签名和 GitHub Actions 发布工作流;强调静态导出限制、签名密钥保护与未验证原生行为的交付边界。

分类:开发
rockbenben
rockbenben
热度 68.40
最近更新 2026-12-02 14:02:57

安装

发给 Agent
帮我安装这个技能:npx secure-skills-cli add rockbenben/nextjs-to-tauri
兼容性
通用兼容
联网要求
需要联网
数据出网
可选出网
Metadata
name
nextjs-to-tauri
description
Convert a static-exportable Next.js 16 App Router app to a Tauri 2 desktop shell with native packaging and optional updates. 将 Next.js 静态导出迁移到 Tauri 2。
metadata
{"version":"1.1.25","openclaw":{"homepage":"https://github.com/rockbenben/aishort-skills/tree/main/skills/nextjs-to-tauri","emoji":"🦀","requires":{"bins":["node"]},"files":["tauri-files.md","frontend-integration.md","desktop-build.yml","tauri-build.yml","release-workflow.md","release-assets.js","release-assets/**","ci/**"]}}

Next.js 16 → Tauri 2 Desktop App

Wrap a static-exportable Next.js 16 (App Router) app in a thin Tauri 2 native shell (.exe, .dmg, .AppImage, optional auto-update). Most migration friction comes from Tauri's embedded asset protocol (path/routing resolution), not React.

Guardrails & Scope

  • Static-export gate: Confirm the desktop routes work from output: "export". Keep build-time Server Components and static GET handlers. Request-time rendering, API handlers that need a running Next.js server, or Server Actions require a separate server design. Place desktop hooks inside a client component. Static-export support
  • Authorization & read-only mode: Read-only assessments stop at blockers and proposed checks without writing files. Migration tasks authorize necessary local edits (next.config.*, package.json, src-tauri/, desktop frontend boundary, icons, .gitignore, workflows) while preserving unrelated web behavior; do not ask approval before every file write. Signing secrets, OS code signing, and public release actions require explicit user scope.
  • Release-policy boundary: The bundled workflow builds and verifies a GitHub draft for manual publication (desktop-app-standard defaults to direct auto-publication). Preserve the user's requested release policy rather than mixing the two workflows.
  • Package manager: Commands and tauri-build.yml assume Yarn Classic. For npm or pnpm, substitute lockfile install/run commands (npm ci / pnpm install --frozen-lockfile, npm exec --no -- tauri / pnpm tauri) and update setup-node's cache: (cache: yarn fails without yarn.lock).

Procedure

  1. Verify target-compatible stable versions (Tauri 2):

    • npm & crates: Check published versions of @tauri-apps/cli, tauri, and each required plugin. Select compatible stable 2.x releases. In crates.io metadata, use the non-yanked entries in versions for the target major.
    • Actions & Node: Query latest stable Action majors (gh api repos/actions/checkout/releases/latest --jq .tag_name, plus setup-node, rust-cache, upload-artifact; check tauri-apps/tauri-action@v1 README). Pin the current Active LTS Node major from the Node schedule in both desktop-build.yml and tauri-build.yml. Set engines.node from actual Next.js/Tauri CLI floors, not as a copy of the CI major.
    • (Optional) If desktop-app-standard is installed, its audit script can supplement version/CI checks, though this migration does not require it.
  2. Gate static export on Tauri in next.config.* using TAURI_BUILD=1 (gotcha #1; retain existing reliable gating if present), preserving wrappers like next-intl:

    // Keep the NextConfig annotation so wrappers receive the literal "export" type.
    import type { NextConfig } from "next";
    
    const isTauri = process.env.TAURI_BUILD === "1"; // set by `yarn build:tauri`
    
    const nextConfig: NextConfig = {
      // ...your existing config...
      ...(isTauri ? { output: "export", trailingSlash: true } : {}),
      images: {
        // ...preserve existing image settings...
        ...(isTauri ? { unoptimized: true } : {}),
      },
    };
    
    export default nextConfig;
    

    If the project already wraps its config, retain the existing export, for example export default withNextIntl(nextConfig) in a next-intl app. Add cross-env (yarn add -D cross-env) and "build:tauri": "cross-env TAURI_BUILD=1 next build" in package.json so tauri.conf.json's beforeBuildCommand sets the flag.

  3. Scaffold Tauri & icons (JS CLI only; no local Rust needed yet):

    yarn add -D @tauri-apps/cli@^2
    yarn tauri init --ci --app-name "<app>" --window-title "<Title>" \
      --frontend-dist "../out" --dev-url "http://localhost:3000" \
      --before-dev-command "yarn dev" --before-build-command "yarn build:tauri"
    # Master icon must be square (1024x1024 PNG/SVG) to avoid upscaling blur.
    yarn tauri icon public/logo.png
    

    Keep mobile icon directories if present. Fix .gitignore immediately (gotcha #3) and confirm git check-ignore src-tauri/tauri.conf.json outputs nothing.

  4. Configure src-tauri/: Adapt tauri-files.md (tauri.conf.json with CSP, url, webviewInstallMode, mainBinaryName, optional updater; Cargo.toml; src/lib.rs with single-instance registered first + tray; capabilities/default.json). If shipping without auto-update, apply the full no-updater checklist.

  5. Integrate the selected frontend features: The client component handles external links. Add the update hook only for apps that support updates (it downloads before asking to install), and add the locale hook only when the app remembers language choices. Mount each hook under the providers it actually uses. For locale-prefixed routes, switch via soft navigation and guard the startup redirect across layout remounts (gotcha #11).

  6. Wire single-source versioning & release CI: Add src-tauri/update-version.cjs and "update-version" script from tauri-files.md, then install both workflow templates and helper scripts per release-workflow.md.

    • Tag as single version source: On tag push, the template runs npm pkg set version="${GITHUB_REF_NAME#v}" before update-version so installers, embedded version, and tagName: v__VERSION__ stay identical (never point tagName at github.ref_name, gotcha #13). If skipping injection, pushed tag must exactly equal package.json version.
    • Platform signing: Updater signing and Windows/macOS code signing/notarization are separate; report any unsigned/unverified target rather than assuming the template covers it.
  7. Configure updater signing keys (only if updater is enabled): Follow Updater signing keys. Never commit private keys or overwrite an existing key (-f).

  8. Verify static export & native build: Run yarn build:tauri and verify out/<default-locale>/index.html (or out/index.html for single-locale apps). If Rust and platform libraries are installed locally, build and exercise startup, tray, locale persistence, and updater; otherwise run CI and explicitly report unverified native checks.

Verification & Delivery

Report generated files, checks actually performed, offline/portable constraints, and measured package sizes (a frontend build or CI compile alone does not prove runtime desktop behavior). Hand off only unfinished authorized items (signing credentials, target-device testing, manual draft publication — releases/latest ignores drafts until published). Ensure the release tag commit contains the workflow files.

Gotchas

# Pitfall Fix
1 White screen / extensionless paths fail on Tauri's asset server. Enable trailingSlash: true under TAURI_BUILD=1 and match tauri.conf.json window url (e.g. /en/) to the default locale.
2 Runtime locale middleware unavailable in static export. Gate export via TAURI_BUILD so web keeps its config; verify exported locale directories (Next.js limits).
3 src-tauri/ ignored by git due to bare src-tauri in .gitignore. Replace with src-tauri/target, src-tauri/gen/schemas, src-tauri/*.key; verify with git check-ignore src-tauri/tauri.conf.json.
4 isTauri() false positives from importing @tauri-apps/api. Detect synchronously via window.__TAURI_INTERNALS__ / __TAURI__ / Tauri UA (same bundle ships to web + desktop).
5 Second launch fails to refocus main window. Register tauri-plugin-single-instance first in src/lib.rs, gated to #[cfg(desktop)].
6 Clients never see update after green CI. releases/latest ignores drafts/prereleases; publish the verified draft (never delete a published release to rerun — cut a new version).
7 Private signing key leaked in git history. Keep src-tauri/*.key gitignored and in GitHub secrets only (.pub is safe to commit); rotate immediately if leaked.
8 Portable exe treated as self-updating installer. Raw portable binary still requires WebView2 and lacks guaranteed self-update; catch install failures or disable updater via no-updater checklist.
9 Wrong OS-language auto-detection. Preserve the app's language policy. If it uses OS detection, prefer a saved choice, then a supported system locale, then the configured default.
10 External links navigate in-webview or opener denied. Intercept external URLs with @tauri-apps/plugin-opener and grant matching URL scopes (opener:default or explicit scoped rules).
11 Locale switch bounces back on layout remount. Use soft router.push, keep startup redirect guard in a module-level variable (not useRef), and save preference only in the switcher.
12 Linux EGL abort or blank AppImage. Isolate WebKit DMABUF vs bundled libwayland-* conflict first; see Linux diagnosis.
13 Tag workflow misses CI fixes or mismatches versions. Tag a commit containing the workflow; inject version from tag before update-version; for multi-app release hubs, preserve the full-SHA source map on reruns.