Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gatsby pathPrefix without hardcoding

Tags:

gatsby

The Gatsby docs for path prefix show I can define something like pathPrefix: "/blog", and indeed I tried it and it just worked. However, I'd rather not hardcode the path where the app will live. I'd like to be able to deploy the build to multiple paths and have it just work from all of them. I'm hoping there's a way I can make the build work from any arbitrary path. Is there?

like image 381
Jeff M Avatar asked Jul 08 '18 18:07

Jeff M


1 Answers

You can use gatsby-link to help you with that. It functions like React-Router's Link component, but helps you out with path prefixing. For example, if you set your pathPrefix: "/myBlog"...

import Link from 'gatsby-link'

/* later down in the code... */

<nav>
  <Link to='/about'>About The Blog</Link>
</nav>

Which would output:

<a href="/myBlog/about">About The Blog</a>
like image 77
cjmlgrto Avatar answered Oct 16 '22 06:10

cjmlgrto