Vercel 与 Next.js 边缘部署快速修复

问题描述 投票:0回答:1

我有一个正在尝试部署的 Vercel 项目。我正在使用 Next.js。这是我的

next.config.js
文件:

/* eslint-disable */

/** @type {import('next').NextConfig} */
const nextConfig = {
    async headers() {
        return [
            {
                source: "/api/:path*",
                headers: [
                    { key: "Access-Control-Allow-Credentials", value: "true" },
                    { key: "Access-Control-Allow-Origin", value: "*" },
                    {
                        key: "Access-Control-Allow-Methods",
                        value: "GET,DELETE,PATCH,POST,PUT,OPTIONS",
                    },
                    {
                        key: "Access-Control-Allow-Headers",
                        value:
                            "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version, Authorization, id, filters",
                    },
                ],
            },
        ];
    },
    eslint: {
        ignoreDuringBuilds: true,
    },
    typescript: {
        ignoreBuildErrors: true,
    },
    unstable_allowDynamic: [
        '/node_modules/**',
      ],
};
module.exports = nextConfig;

这是我的部署日志:

[15:45:51.865] Running build in Washington, D.C., USA (East) – iad1
[15:45:51.948] Cloning github.com/eTech-Source/commerce-karma-prod (Branch: main, Commit: 6a51ce8)
[15:45:52.788] Cloning completed: 839.84ms
[15:45:56.810] Restored build cache
[15:45:56.891] Running "vercel build"
[15:45:57.519] Vercel CLI 32.7.1
[15:45:57.979] Installing dependencies...
[15:45:59.433] 
[15:45:59.433] up to date in 926ms
[15:45:59.434] 
[15:45:59.435] 137 packages are looking for funding
[15:45:59.435]   run `npm fund` for details
[15:45:59.445] Detected Next.js version: 14.0.4
[15:45:59.448] Detected `package-lock.json` generated by npm 7+
[15:45:59.449] Running "npm run build"
[15:46:00.289] 
[15:46:00.289] > commerce-karma@dev_0.2.0 build
[15:46:00.290] > next build
[15:46:00.290] 
[15:46:00.859]  ⚠ Invalid next.config.js options detected: 
[15:46:00.860]  ⚠     Unrecognized key(s) in object: 'unstable_allowDynamic'
[15:46:00.861]  ⚠ See more info here: https://nextjs.org/docs/messages/invalid-next-config
[15:46:00.971]    ▲ Next.js 14.0.4
[15:46:00.971]    - Environments: .env.local
[15:46:00.972] 
[15:46:00.972]    Creating an optimized production build ...
[15:46:06.399]  ✓ Compiled successfully
[15:46:06.400]    Skipping validation of types
[15:46:06.401]    Skipping linting
[15:46:06.714]    Collecting page data ...
[15:46:08.576]    Generating static pages (0/23) ...
[15:46:08.837] 
   Generating static pages (5/23) 
[15:46:08.949] 
   Generating static pages (11/23) 
[15:46:09.493] 
   Generating static pages (17/23) 
[15:46:09.935] 
 ✓ Generating static pages (23/23) 
[15:46:10.109]    Finalizing page optimization ...
[15:46:10.110]    Collecting build traces ...
[15:46:18.282] 
[15:46:18.389] Route (app)                              Size     First Load JS
[15:46:18.390] ┌ λ /                                    213 B          97.8 kB
[15:46:18.390] ├ λ /_not-found                          875 B          83.1 kB
[15:46:18.390] ├ λ /api/bio                             0 B                0 B
[15:46:18.390] ├ λ /api/business                        0 B                0 B
[15:46:18.390] ├ λ /api/currentUser                     0 B                0 B
[15:46:18.390] ├ λ /api/location                        0 B                0 B
[15:46:18.390] ├ λ /api/reviews/add                     0 B                0 B
[15:46:18.391] ├ λ /api/reviews/comment                 0 B                0 B
[15:46:18.391] ├ λ /api/reviews/react                   0 B                0 B
[15:46:18.391] ├ ○ /api/test                            0 B                0 B
[15:46:18.391] ├ λ /api/user                            0 B                0 B
[15:46:18.391] ├ λ /api/webhooks/user                   0 B                0 B
[15:46:18.391] ├ λ /app                                 151 B           130 kB
[15:46:18.391] ├ λ /app/afterThirdPartySignIn           137 B          82.3 kB
[15:46:18.391] ├ λ /app/business                        3.62 kB         130 kB
[15:46:18.391] ├ λ /app/customer                        151 B           130 kB
[15:46:18.391] ├ λ /app/live                            179 B          82.4 kB
[15:46:18.391] ├ λ /app/reviews/[slug]/[id]             5.54 kB         132 kB
[15:46:18.392] ├ λ /app/settings                        1.46 kB        88.6 kB
[15:46:18.392] ├ λ /contact                             1.37 kB        83.6 kB
[15:46:18.392] ├ λ /pricing                             285 B          97.9 kB
[15:46:18.392] └ λ /sign-in/[[...sign-in]]              617 B            95 kB
[15:46:18.392] + First Load JS shared by all            82.2 kB
[15:46:18.392]   ├ chunks/938-aa7cae7c7d670e74.js       26.7 kB
[15:46:18.392]   ├ chunks/fd9d1056-900860ef98d0b934.js  53.3 kB
[15:46:18.392]   ├ chunks/main-app-01dcb8892b176af1.js  220 B
[15:46:18.392]   └ chunks/webpack-9dece1407892bb3e.js   2.02 kB
[15:46:18.392] 
[15:46:18.393] 
[15:46:18.393] ƒ Middleware                             202 kB
[15:46:18.393] 
[15:46:18.393] ○  (Static)   prerendered as static content
[15:46:18.393] λ  (Dynamic)  server-rendered on demand using Node.js
[15:46:18.393] 
[15:46:18.532] Traced Next.js server files in: 58.975ms
[15:46:22.408] Created all serverless functions in: 3.876s
[15:46:22.541] Collected static files (public/, static/, .next/static): 7.373ms
[15:46:23.037] Build Completed in /vercel/output [25s]
[15:46:23.270] Deploying outputs...
[15:46:29.953] 
[15:46:30.841] Deployment completed
[15:46:36.968] Uploading build cache [105.53 MB]...
[15:46:40.169] Build cache uploaded: 3.201s

没有错误。但在客户端我收到一个错误:

这个问题似乎来自node_modules。我可以实施什么快速修复来解决此问题,而无需选择不同的

npm
包。

node.js next.js deployment vercel edge-runtime
1个回答
0
投票

您的问题是在错误的位置声明了

unstable_allowDynamic
。您应该在中间件中声明它(https://nextjs.org/docs/app/api-reference/file-conventions/middleware

您的日志中存在的错误:

[15:46:00.860]  ⚠     Unrecognized key(s) in object: 'unstable_allowDynamic'

你应该做的就是删除它,并在中间件中添加声明,例如

src/middleware.ts
文件:

export const config = {
  runtime: 'edge',
  unstable_allowDynamic: [
    // example of blog
    '/node_modules/foobar/**',
  ],
}

要了解更多信息,请访问 https://nextjs.org/docs/app/api-reference/edge#unsupported-apis

© www.soinside.com 2019 - 2024. All rights reserved.