Gatsby:basepath和路径前缀之间有什么区别? [英] Gatsby: what's the difference between basepath and path prefix?
问题描述
我不了解盖茨比中basepath
和path prefix
之间的区别,以及何时使用每个功能
I don't understand the difference between basepath
and path prefix
in Gatsby, and when to use each feature
基本路径: https://www.gatsbyjs.org/tutorial/part-seven/
路径前缀: https://www.gatsbyjs.org/docs/path-prefix /
推荐答案
TL:DR
pathPrefix
对您的网站影响更大-它在所有页面和资产的生成的url中添加前缀. basePath
只是用于从文件系统生成信息的助手-根据我对Gatsby的经验,我很少使用它.
pathPrefix
has much more impact on your site — it add a prefix to the generated url of all your pages and assets. basePath
is just a helper for generating slug from your filesystem — In my experience with Gatsby I rarely use it at all.
它将前缀添加到所有页面的网址&资产.这样,您可以将Gatsby网站部署为另一个网站的子目录.
It appends a prefix to url of all your pages & assets. This way, you can deploy Gatsby site as a sub directory of another site.
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
basePath
它是createFilePath
的一个选项,可帮助您从项目结构中创建段.不管您是否从根本上为Gatsby服务,都是如此.
basePath
It's an option of createFilePath
which helps you create slug from your project structure. It doesn't affect whether you're serving your Gatsby from root or not.
如果您的项目目录是这样的:
If your project directory is like this:
<root>
|--content
| |--writing
| | `--page1.md
| `--images
| `--cat.md
|
|--gatsby-config.js
...
然后您在gatsby-config.js中设置它
And you set this in your gatsby-config.js
module.exports = {
plugins: [
{
resolve: 'gatsby-source-filesystem',
options: {
path: `${__dirname}/content`,
name: 'content'
}
}
]
}
然后,createFilePath
将为您的文件返回以下提示:writing/page1/
,可能不是您想要的.也许您希望它是/page1/
.在这种情况下,将basePath
设置为writing
将返回/page1/
.
Then createFilePath
will return this slug for your file: writing/page1/
, which maybe not what you want. Maybe you want it to be /page1/
. In this case, setting basePath
to writing
will return /page1/
.
这篇关于Gatsby:basepath和路径前缀之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!