Skip to content

Commit

Permalink
[gatsby-link] Add helper withPrefix method (#2329)
Browse files Browse the repository at this point in the history
* [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
kripod authored and KyleAMathews committed Oct 18, 2017
1 parent c7d92e2 commit 644f131
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
21 changes: 21 additions & 0 deletions packages/gatsby-link/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Copy link
@mcallistersean

mcallistersean Oct 19, 2017

Contributor

Not sure if the documentation is wrong or the withPrefix function is broken, but calling this exact code returns '//' which can't be right

This comment has been minimized.

Copy link
@KyleAMathews

KyleAMathews Oct 19, 2017

Contributor

oh right. Yeah the function is a bit naive. Would you like to make a PR for gatsby-link adding a test for withPrefix('/') and making it work for that?


return (
<div>
<h1>Welcome {isHomepage ? 'home' : 'aboard'}!</h1>
{children()}
</div>
)
}
```
10 changes: 7 additions & 3 deletions packages/gatsby-link/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ if (typeof __PREFIX_PATHS__ !== `undefined` && __PREFIX_PATHS__) {
pathPrefix = __PATH_PREFIX__
}

export function withPrefix(path) {
return pathPrefix + path
}

function normalizePath(path) {
return path.replace(/^\/\//g, `/`)
}
Expand Down Expand Up @@ -49,7 +53,7 @@ class GatsbyLink extends React.Component {
}

this.state = {
to: normalizePath(pathPrefix + props.to),
to: normalizePath(withPrefix(props.to)),
IOSupported,
}
this.handleRef = this.handleRef.bind(this)
Expand All @@ -58,7 +62,7 @@ class GatsbyLink extends React.Component {
componentWillReceiveProps(nextProps) {
if (this.props.to !== nextProps.to) {
this.setState({
to: normalizePath(pathPrefix + nextProps.to),
to: normalizePath(withPrefix(nextProps.to)),
})
// Preserve non IO functionality if no support
if (!this.state.IOSupported) {
Expand Down Expand Up @@ -159,5 +163,5 @@ GatsbyLink.contextTypes = {
export default GatsbyLink

export const navigateTo = pathname => {
window.___navigateTo(normalizePath(pathPrefix + pathname))
window.___navigateTo(normalizePath(withPrefix(pathname)))
}

0 comments on commit 644f131

Please sign in to comment.