Gatsby:basepath和路径前缀之间有什么区别? [英] Gatsby: what's the difference between basepath and path prefix?

查看:220
本文介绍了Gatsby:basepath和路径前缀之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不了解盖茨比basepathpath 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屋!

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