文章

next.js 解决 `x-forwarded-host` header does not match `origin` header with value from a forwarded Server Actions request

在 GitHub codespaces 上开发 next.js 项目,端口穿透出来后访问,结果报错:

1
`x-forwarded-host` header does not match `origin` header with value from a forwarded Server Actions request.

解决方案:

  1. 如果需要,首先更新 next.js 。参见 https://nextjs.org/docs/messages/version-staleness ,运行命令:
1
pnpm add next@latest

这应当是不会报错了。但如果您像笔者一样使用了 ppr: true ,就应当使用 canary 版本,不然后续可能会出错。

  1. 修改 next.config.js ,添加 experimental.serverActions.allowedOrigins

例如我这里:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import type { NextConfig } from 'next';

const nextConfig: NextConfig = {
  /* config options here */
  experimental: {
    serverActions: {
      allowedOrigins: [
        'localhost:3000', // localhost
        'super-duper-space-*****-*****-3000.app.github.dev', // Codespaces
      ],
    },
    ppr: true,
  },
  images: {
    remotePatterns: [
      {
        hostname: 'avatar.vercel.sh',
      },
    ],
  },
};

export default nextConfig;

然后重新启动后就能成功访问了。

本文由作者按照 CC BY 4.0 进行授权

© Dignite. 保留部分权利。 由  提供CDN加速。

浙ICP备2023032699号 | 使用 Jekyll 主题 Chirpy