From 528044db7ddd845d79ff35abf31aa709bd49121f Mon Sep 17 00:00:00 2001 From: eric Date: Thu, 12 Mar 2026 04:11:29 -0500 Subject: [PATCH] 's' --- README.md | 2 +- app/page.tsx | 12 +++++----- config/server-actions.key | 1 + docs/DEPLOYMENT.md | 35 +++--------------------------- next.config.ts | 10 +++++++++ scripts/generate-encryption-key.js | 13 ----------- 6 files changed, 21 insertions(+), 52 deletions(-) create mode 100644 config/server-actions.key delete mode 100644 scripts/generate-encryption-key.js diff --git a/README.md b/README.md index 112b229..a3371a9 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ ZPAY、PocketBase、MinIO 配置与 `C:\project\digital` 保持一致,复制 ` | 变量 | 说明 | 默认值 | |------|------|--------| -| `NEXT_SERVER_ACTIONS_ENCRYPTION_KEY` | Server Actions 加密密钥(**生产部署必设**,避免 "Failed to find Server Action") | 无,需用 `node scripts/generate-encryption-key.js` 生成 | +| `NEXT_SERVER_ACTIONS_ENCRYPTION_KEY` | Server Actions 加密密钥(可选,未设时自动从 `config/server-actions.key` 加载) | `config/server-actions.key` | | `NEXT_PUBLIC_POCKETBASE_URL` | PocketBase 地址 | `https://pocketbase.hackrobot.cn` | | `NEXT_PUBLIC_API_URL` / `PAYMENT_API_URL` | payjsapi 地址 | `https://api.hackrobot.cn` | | `PAYMENT_PROVIDER` | 支付提供商(payjsapi 环境变量) | `xorpay` / `zpay` | diff --git a/app/page.tsx b/app/page.tsx index 6d32cfa..bf1346f 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -424,6 +424,12 @@ export default function Home() { } }, [paidType]); + const handleLogout = useCallback(async () => { + const { pbLogout } = await import("@/app/lib/pocketbase"); + await pbLogout(); + clearAllStorage(); + }, [clearAllStorage]); + const isLoggedIn = paidType === "vip"; if (loading) { @@ -435,12 +441,6 @@ export default function Home() { ); } - const handleLogout = useCallback(async () => { - const { pbLogout } = await import("@/app/lib/pocketbase"); - await pbLogout(); - clearAllStorage(); - }, [clearAllStorage]); - return ( <> diff --git a/config/server-actions.key b/config/server-actions.key new file mode 100644 index 0000000..6664d59 --- /dev/null +++ b/config/server-actions.key @@ -0,0 +1 @@ +hF2KLr00cW2L1KqktKFzzOiwcSS1D/b4zEkhuMWAsQs= diff --git a/docs/DEPLOYMENT.md b/docs/DEPLOYMENT.md index 951e8ea..5f0543c 100644 --- a/docs/DEPLOYMENT.md +++ b/docs/DEPLOYMENT.md @@ -10,37 +10,9 @@ Error: Failed to find Server Action. This request might be from an older or newe **原因**:Next.js 为 Server Actions 使用加密密钥,每次构建会重新生成。部署后客户端可能仍缓存旧版本 JS,导致请求的 action ID 与服务器不匹配。 -**解决方案**:在**构建时**设置固定加密密钥 `NEXT_SERVER_ACTIONS_ENCRYPTION_KEY`,使多次部署使用相同密钥。 +**解决方案**:项目已内置 `config/server-actions.key`,构建时 `next.config.ts` 会自动加载,无需手动配置。直接执行 `pnpm build` 即可。 -### 步骤 - -1. **生成密钥**(仅需执行一次,之后复用同一密钥): - - ```bash - node scripts/generate-encryption-key.js - ``` - -2. **在构建前设置环境变量**: - - ```bash - export NEXT_SERVER_ACTIONS_ENCRYPTION_KEY="<上一步输出的 base64 字符串>" - pnpm build - ``` - - 或在 `.env` / `.env.production` 中添加: - - ``` - NEXT_SERVER_ACTIONS_ENCRYPTION_KEY= - ``` - -3. **重新构建并重启**: - - ```bash - pnpm build - pm2 restart vip - ``` - -4. **建议用户清除缓存**:部署后若仍有问题,可让用户执行硬刷新(Ctrl+Shift+R)或清除浏览器缓存。 +部署后若仍有问题,可让用户执行硬刷新(Ctrl+Shift+R)或清除浏览器缓存。 --- @@ -50,8 +22,7 @@ Error: Failed to find Server Action. This request might be from an older or newe # 1. 安装依赖 pnpm install -# 2. 设置 NEXT_SERVER_ACTIONS_ENCRYPTION_KEY 后构建 -export NEXT_SERVER_ACTIONS_ENCRYPTION_KEY="你的密钥" +# 2. 构建(自动加载 config/server-actions.key) pnpm build # 3. 启动 diff --git a/next.config.ts b/next.config.ts index 73521d7..2ff6a2b 100644 --- a/next.config.ts +++ b/next.config.ts @@ -1,4 +1,14 @@ import type { NextConfig } from "next"; +import { readFileSync, existsSync } from "fs"; +import { join } from "path"; + +// 构建时自动加载 Server Actions 加密密钥,避免 "Failed to find Server Action" 部署错误 +if (!process.env.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY) { + const keyPath = join(__dirname, "config", "server-actions.key"); + if (existsSync(keyPath)) { + process.env.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY = readFileSync(keyPath, "utf8").trim(); + } +} const nextConfig: NextConfig = { // 局域网访问(如手机 192.168.x.x)时允许跨域请求 _next 资源 diff --git a/scripts/generate-encryption-key.js b/scripts/generate-encryption-key.js deleted file mode 100644 index 059136b..0000000 --- a/scripts/generate-encryption-key.js +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env node -/** - * 生成 Next.js Server Actions 加密密钥 - * 用于解决 "Failed to find Server Action" 部署错误 - * - * 用法: node scripts/generate-encryption-key.js - * 将输出的 base64 字符串设置为环境变量 NEXT_SERVER_ACTIONS_ENCRYPTION_KEY - * 在构建时设置(pnpm build 前 export 或 .env 中配置) - */ -const crypto = require("crypto"); -const key = crypto.randomBytes(32).toString("base64"); -console.log("将以下值设置为 NEXT_SERVER_ACTIONS_ENCRYPTION_KEY:\n"); -console.log(key);