【问题标题】:NextJS config: Move redirects to it's own fileNextJS 配置:将重定向移动到它自己的文件
【发布时间】:2022-07-19 16:10:36
【问题描述】:

我正在从一个 wordpress 网站转移到一个新的、闪亮的 nextjs 网站。但是,我有数百个重定向,我不想将它们全部放在 nextjs 配置文件中。我喜欢干净整洁的东西。我搜索了一段时间,没有找到有关如何执行此操作的任何结果。有什么建议吗?

【问题讨论】:

  • 您不一定需要在配置文件中包含它们。 next.config.js 中的 redirects 属性是一个异步函数,您可以将生成重定向的逻辑移动到帮助程序并在配置中调用该函数。
  • @juliomalves 您能否更具体地说明如何从 next.config.js 中调用该函数?我尝试导入辅助函数并将其作为 const 要求,但都没有工作
  • @juliomalves 嘿,所以我尝试将其添加到我的 nextjs.config:import { redirects } from '@lib/helpers' 并收到以下错误:SyntaxError: Cannot use import statement outside a module
  • 改用require,即const redirects = require('@lib/helpers').redirects。或者将您的配置文件转换为 ESM,以便您可以使用import,请参阅stackoverflow.com/a/69781269/1870780

标签: next.js


【解决方案1】:

你需要通过require来导入它,记住你还需要将它作为模块导出,例如:

const redirectsArray = [
  { source: "/about", destination: "/", permanent: true },
];

async function redirects() {
  return redirectsArray;
}

module.exports = {
  redirects,
};

然后

const { redirects } = require("./redirects.js");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-06
    • 2020-08-03
    • 1970-01-01
    • 1970-01-01
    • 2018-11-15
    • 1970-01-01
    • 2010-11-27
    • 2020-06-09
    相关资源
    最近更新 更多