【问题标题】:nextjs custom serve directory in productionnextjs 生产中的自定义服务目录
【发布时间】:2022-10-07 22:28:00
【问题描述】:

问题:当我在生产服务器上运行next build 时,页面在构建时没有设置样式。 我想做的事:

  1. 将应用程序构建到build 目录
  2. build 移动到build-public
  3. build-public 提供应用程序。

    我不想为这个简单的任务设置自定义服务器。

    我的package.jsonscripts 部分:

    "scripts": {
        "dev": "next dev -p 3002",
        "build": "next build",
        "start": "next start -p 3002"
      },
    

    我的next.config.js

    module.exports = {
      basePath: '',
      distDir: 'build', // build to build
      dir: 'build-public', // serve from build-public. 'Dir' option is not working.
    }
    

【问题讨论】:

  • dir 选项不存在,您的意思是改用basePath 吗?
  • basePath 用于链接。如果dir 选项不存在,那么解决方案是什么?
  • 你能澄清一下为什么需要将构建的应用程序从build 移动到build-public 吗?为什么不直接将构建文件夹命名为build-public
  • 因为构建过程大约需要 2 分钟。在这 2 分钟内,网站内容未设置样式
  • @VictorGorban您找到解决方案了吗?我也在为同样的问题而苦苦挣扎。

标签: build next.js


【解决方案1】:

您可以在 next.config.js 中使用环境变量并在运行构建命令之前设置该环境变量:

    // next.config.js

    module.exports = {
      distDir: process.env.BUILD_DIR || 'build-public',
    }
    // package.json
    
    "scripts": {
      "build": "BUILD_DIR=build next build",
    }
    

【讨论】:

    猜你喜欢
    • 2020-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    • 2021-09-02
    • 2021-02-02
    • 1970-01-01
    • 2018-07-07
    相关资源
    最近更新 更多