Gatsby 项目在 webpack 问题的目录中

发布于 2025-01-12 03:57:16 字数 347 浏览 3 评论 0原文

我有一个 gatsby 项目,当我将它放在域的根目录中时,它工作得很好(例如 abc.com)。但如果我将它放在一个目录下(例如 abc.com/myproj),那么 webpack 等会抛出 404 错误,因为它们仍然引用项目的根目录。

是否可以修复此问题并让他们查找 myproj 目录中的所有 js 文件而不是查看根目录? 输入图片这里的描述

是否需要更改任何配置才能在根目录下的目录中工作?提前致谢。

I have a gatsby project and when I put it in the root of the domain it works just fine (e.g. abc.com). But if I place it under a directory (e.g. abc.com/myproj) then the webpack ect throwing 404 error as they are still referring to the root of the project.

Could this be fixed and make them look for all js files inside the myproj directory instead of looking at the root? enter image description here

is there any configurations I need to change it to work inside a directory under the root? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦巷 2025-01-19 03:57:16

我认为您正在寻找 路径前缀。只需在 gatsby-config.js 中添加以下内容:

module.exports = {
  pathPrefix: `/myproj`,
}

要启用完整配置,您需要将构建和服务命令调整为:

gatsby build --prefix-paths

并且:

gatsby serve --prefix-paths

在这两种情况下,如果未传递该标志,Gatsby将忽略pathPrefix

对于手动构建的路径名,有一个辅助函数 withPrefix 在生产环境中添加路径前缀(但在开发过程中不需要添加路径前缀)加上前缀)。

I think you are looking for path prefixing. Just add the following in your gatsby-config.js:

module.exports = {
  pathPrefix: `/myproj`,
}

To enable the full configuration you will need to tweak your build and serve command as:

gatsby build --prefix-paths

And:

gatsby serve --prefix-paths

In both cases, if the flag is not passed, Gatsby will ignore the pathPrefix.

For pathnames you construct manually, there’s a helper function, withPrefix that prepends your path prefix in production (but doesn’t during development where paths don’t need to be prefixed).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文