-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[gatsby-link] Add helper
withPrefix
method (#2329)
* [gatsby-link] Add helper `withPrefix` method Resolves #2254 * [gatsby-link] Fix linter-detected issue * [gatsby-link] Add docs for `withPrefix` * Tweak copy
- Loading branch information
1 parent
c7d92e2
commit 644f131
Showing
2 changed files
with
28 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,3 +52,24 @@ render () { | |
</div> | ||
} | ||
``` | ||
|
||
## Prefixed paths helper | ||
|
||
Gatsby allows you to [automatically prefix links](/docs/path-prefix/) for sites hosted on Github Pages or other places where your site isn't at the root of the domain. | ||
|
||
This can create problems during development as pathnames won't be prefixed. To handle both, gatsby-link exports a helper function `withPrefix` that prepends the prefix during production but doesn't in development. | ||
|
||
```jsx | ||
import { withPrefix } from "gatsby-link" | ||
|
||
const IndexLayout = ({ children, location }) => { | ||
const isHomepage = location.pathname === withPrefix('/'); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
KyleAMathews
Contributor
|
||
|
||
return ( | ||
<div> | ||
<h1>Welcome {isHomepage ? 'home' : 'aboard'}!</h1> | ||
{children()} | ||
</div> | ||
) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Not sure if the documentation is wrong or the withPrefix function is broken, but calling this exact code returns '//' which can't be right