Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

How to configure Gatsby to use relative links throughout?

The Gatsby static site tool by default assumes the final built artifact will be hosted at the root of the domain. The built-in Link component honors the pathPrefix configuration value, but that configuration parameter needs to be set as a static value.

What I want is the ability to trigger a build by Gatsby where all the links are relative, such that the artifact could then be hosted at any depth in any domain (e.g. could copy the public folder to be served at example.com/my-site/ or at my-example.org/mirror/my-site/ and have both work).

To do this, it would need to not prefix every link with a /, and use ../ for directory traversal as needed as part of the links.

Example

By default Gatsby renders a link from /index/ to /blog/08/25/my-lunch/ as <a href="/blog/08/25/my-lunch/">, and a link from /blog/08/25/my-lunch/ back to /index/ renders as <a href="/">.

The desired effect is a link from /index/ to /blog/08/25/my-lunch/ would render as <a href="blog/08/25/my-lunch/"> (note no leading /), and a link from /blog/08/25/my-lunch/ back to /index/ would render as <a href="../../../../">.

This would also be true for head assets (e.g. to link to the /favicon.ico file in the root of the site, on the /index/ page it would be href="favicon.ico" and on /blog/08/25/my-lunch/ it would be href="../../../../favicon.ico", rather than href="/favicon.ico" as it is for both by default).

question from:https://stackoverflow.com/questions/66066537/how-to-configure-gatsby-to-use-relative-links-throughout

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...