【问题标题】:NextJS rewrites not taking effectNextJS 重写不生效
【发布时间】:2022-11-01 17:49:11
【问题描述】:

尝试使用 NextJS 重写,目标是将 localhost 请求重定向到我的服务器。 例如从 http://localhost:3000/api/itemshttp://serverurl.com:port/api/items

我的配置如下所示:

const moduleExports = withAntdLess({
  async rewrites() {
    return [
      {
        source: '/api/:path*',
        destination: 'http://localhost:8000/:path*' // Proxy to Backend
      }
    ]
  },
  cssLoaderOptions: {},
  images: {
    domains: ['firebasestorage.googleapis.com'],
  },
  webpack(config) {
    config.module.rules.push({
      test: /\.svg$/i,
      issuer: /\.[jt]sx?$/,
      use: ['@svgr/webpack'],
    })
    return config;
  },
  reactStrictMode: true,
});

const sentryWebpackPluginOptions = {
  // Additional config options for the Sentry Webpack plugin. Keep in mind that
  // the following options are set automatically, and overriding them is not
  // recommended:
  //   release, url, org, project, authToken, configFile, stripPrefix,
  //   urlPrefix, include, ignore

  silent: true, // Suppresses all logs
  // For all available options, see:
  // https://github.com/getsentry/sentry-webpack-plugin#options.
};

// Make sure adding Sentry options is the last code to run before exporting, to
// ensure that your source maps include changes from all other Webpack plugins
module.exports = withSentryConfig(moduleExports, sentryWebpackPluginOptions);

重新启动 nextjs,没有任何反应 - 它仍然从 localhost 获取。

知道有什么问题吗?是with 插件吗?

"next": "12.0.7",

【问题讨论】:

    标签: javascript next.js


    【解决方案1】:

    重写仅适用于带有链接 (next/link) 组件的客户端。

    看看文档https://nextjs.org/docs/api-reference/next.config.js/rewrites

    【讨论】:

      猜你喜欢
      • 2021-01-28
      • 1970-01-01
      • 2021-11-03
      • 2014-06-02
      • 2021-07-02
      • 2020-10-19
      • 2021-06-10
      • 1970-01-01
      • 2021-05-01
      相关资源
      最近更新 更多