Gatsby: Best way to handle path prefix for layout component?

Created on 28 Sep 2017  Â·  5Comments  Â·  Source: gatsbyjs/gatsby

So like some of the example apps, my layout component displays different things to the user on different kind of pages. Currently, I'm just simply testing if the .location property passed into layout component is "/" or not.

However, I encountered problem when I'm in production build where the site has a pathPrefix.

Is the best way just to read pathPrefix variable by using a graphql and test on that prefix? It just feels strange though, having to deal with pathPrefix throughly in user code space. I'm kind of thinking that since it was supposed to be a config, it should get over all the labor required in an app..

question or discussion

All 5 comments

Checkout what gatsby-link does https://github.com/gatsbyjs/gatsby/blob/d51e96f0f3eff871c02ef51e4c9c16d9bf83224e/packages/gatsby-link/src/index.js#L6

It might make sense to add a helper function that we use in gatsby-link and elsewhere.

@KyleAMathews A helper function would definitely improve DX by a lot. Do we have to use the globals __PREFIX_PATHS__ and __PATH_PREFIX__ until there is a better solution?

Yeah — though also feel free to add a helper function :-) You have the PR power! :-D

@KyleAMathews I tried to find out where the location prop (which can be used in e.g. layouts) gets bound, but unfortunately, I couldn't find it. Anyway, my idea was to add a method called withPrefix to locaton objects.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

magicly picture magicly  Â·  3Comments

rossPatton picture rossPatton  Â·  3Comments

Oppenheimer1 picture Oppenheimer1  Â·  3Comments

mikestopcontinues picture mikestopcontinues  Â·  3Comments

kalinchernev picture kalinchernev  Â·  3Comments