我不明白 basepath
之间的区别和 path prefix
在 Gatsby ,以及何时使用每个功能
基本路径:https://www.gatsbyjs.org/tutorial/part-seven/
路径前缀:https://www.gatsbyjs.org/docs/path-prefix/
最佳答案
电话:DR pathPrefix
对您的网站有更大的影响——它为所有页面和 Assets 的生成 url 添加前缀。 basePath
只是从你的文件系统生成 slug 的一个助手——根据我对 Gatsby 的经验,我很少使用它。
路径前缀
它将前缀附加到您所有页面和 Assets 的 url。这样,您可以将 Gatsby 站点部署为另一个站点的子目录。
Without Prefix | With 'blog' Prefix
---------------------------|--------------------------------
myblog.com/ | myblog.com/blog
myblog.com/hello/ | myblog.com/blog/hello
myblog.com/image-123.png | myblog.com/blog/image-123.png
基本路径
这是
createFilePath
的一个选项这有助于您从项目结构中创建 slug。它不会影响您是否从 root 为 Gatsby 提供服务。如果你的项目目录是这样的:
<root>
|--content
| |--writing
| | `--page1.md
| `--images
| `--cat.md
|
|--gatsby-config.js
...
你在 gatsby-config.js 中设置了这个
module.exports = {
plugins: [
{
resolve: 'gatsby-source-filesystem',
options: {
path: `${__dirname}/content`,
name: 'content'
}
}
]
}
然后
createFilePath
将为您的文件返回此 slug:writing/page1/
,这可能不是您想要的。也许你希望它是 /page1/
.在这种情况下,设置 basePath
至 writing
将返回 /page1/
.
关于 Gatsby :基本路径和路径前缀有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60245845/