我可以将 NextJS 放在 localhost/next 等子文件夹中吗? [英] Can I put NextJS in subfolder like localhost/next?

查看:32
本文介绍了我可以将 NextJS 放在 localhost/next 等子文件夹中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从 Next.js 构建静态文件,但我想将它放在共享主机的子文件夹或我的本地主机(如 localhost/nextweb)中.

I try to build static files from Next.js, but I want to put it in subfolder of shared host or my localhost like localhost/nextweb.

我试图找到一些例子,但我发现只将 NextJS 放在 root 中.

I tried to find some example, but I found only putting NextJS in root.

我的 next.config.js 看起来像

My next.config.js looks like

const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')
const { ANALYZE } = process.env
module.exports = {
  webpack: function (config) {
    if (ANALYZE) {
      config.plugins.push(new BundleAnalyzerPlugin({
        analyzerMode: 'server',
        analyzerPort: 8888,
        openAnalyzer: true
      }))
    }

    return config
  },
  exportPathMap: () => ({
    "/": { page: "/" },
    "/about": { page: "/about" }
  }),
  assetPrefix: 'http://localhost/nextweb/'
}

当我打开某个页面时,它正在工作,但是当我单击一个链接时,它会显示一个网络请求错误:

When I open some page, it's working, but when I click a link it shows me an network request error:

http://localhost/nextweb/_next/a5126d9c-d338-4eee-86ff-f4e6e7dbafa6/page/nextweb/about/index.js 404 未找到.

但真正的文件包含在 .../page/about/index.js 而不是 /page/nextweb/about/index.js

but real file is contain in .../page/about/index.js not /page/nextweb/about/index.js

我该怎么办?

推荐答案

要在域的子路径下部署 Next.js 应用程序,您可以使用 basePath 配置选项.
basePath 允许您设置路径应用程序的前缀.例如,要使用 /docs 而不是 /(默认),打开 next.config.js 并添加 basePath 配置:

To deploy a Next.js application under a sub-path of a domain you can use the basePath config option.
basePath allows you to set a path prefix for the application. For example, to use /docs instead of / (the default), open next.config.js and add the basePath config:

module.exports = {
  basePath: '/docs',
}

来源:基本路径

这篇关于我可以将 NextJS 放在 localhost/next 等子文件夹中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆