Skip to content

Commit

Permalink
refactor(gatsby): remove parsePath from gatsby core, and move to gats…
Browse files Browse the repository at this point in the history
…by-link (#9957)

* refactor: remove parsePath from gatsby core, and move to gatsby-link

Moving a utility function (that was not used!) in gatsby core into where
it belongs, gatsby-link. This was discovered via a community member
(@travi), as he was attempting to use storybook to test a Link component
(outside of Gatsby), e.g. `import Link from 'gatsby-link'`, which was
then requiring `gatsby` core.

This also generally solves some weirdness that the cyclical require was
kinda weird, e.g. gatsby -> gatsby-link -> gatsby

I do not believe this is a breaking change, since the version required
of gatsby-link will just be bumped by gatsby, and the export is still
exported from the top level gatsby module

* test(gatsby-link): add additional unit tests for parse path

* chore: use test blocks

* chore: fix crlf

* chore: import parsePath from gatsby-link

* chore: export parsePath (whooops)

* chore: remove compiled file
  • Loading branch information
DSchau authored and sidharthachatterjee committed Feb 19, 2019
1 parent 4a13026 commit 3d77520
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/gatsby-link/.gitignore
@@ -1,2 +1,2 @@
/index.js
/*.js
/__tests__
55 changes: 55 additions & 0 deletions packages/gatsby-link/src/__tests__/parse-path.js
@@ -0,0 +1,55 @@
import { parsePath } from "../parse-path"

test(`it defaults to root if undefined`, () => {
expect(parsePath()).toEqual({
pathname: `/`,
search: ``,
hash: ``,
})
})

test(`it uses passed path, if defined`, () => {
const path = `/admin`
expect(parsePath(path)).toEqual({
pathname: path,
search: ``,
hash: ``,
})
})

test(`it returns query string`, () => {
const search = `?some-thing=true&other-thing=false`
const pathname = `/admin`
const path = `${pathname}${search}`

expect(parsePath(path)).toEqual({
pathname,
search,
hash: ``,
})
})

test(`it returns hash`, () => {
const hash = `#some-id`
const pathname = `/admin`
const path = `${pathname}${hash}`

expect(parsePath(path)).toEqual({
pathname,
hash,
search: ``,
})
})

test(`it returns hash, search, and pathname if all defined`, () => {
const hash = `#some-id`
const pathname = `/admin`
const search = `?this-thing=true&other-thing=false`
const path = `${pathname}${search}${hash}`

expect(parsePath(path)).toEqual({
pathname,
search,
hash,
})
})
5 changes: 4 additions & 1 deletion packages/gatsby-link/src/index.js
Expand Up @@ -2,7 +2,10 @@
import PropTypes from "prop-types"
import React from "react"
import { Link } from "@reach/router"
import { parsePath } from "gatsby"

import { parsePath } from "./parse-path"

export { parsePath }

export function withPrefix(path) {
return normalizePath(`${__PATH_PREFIX__}/${path}`)
Expand Down
@@ -1,4 +1,4 @@
export default function parsePath(path) {
export function parsePath(path) {
var pathname = path || `/`
var search = ``
var hash = ``
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/cache-dir/gatsby-browser-entry.js
Expand Up @@ -6,9 +6,9 @@ import Link, {
push,
replace,
navigateTo,
parsePath,
} from "gatsby-link"
import PageRenderer from "./public-page-renderer"
import parsePath from "./parse-path"

const StaticQueryContext = React.createContext({})

Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/cache-dir/navigation.js
Expand Up @@ -5,7 +5,7 @@ import redirects from "./redirects.json"
import { apiRunner } from "./api-runner-browser"
import emitter from "./emitter"
import { navigate as reachNavigate } from "@reach/router"
import parsePath from "./parse-path"
import { parsePath } from "gatsby-link"

// Convert to a map for faster lookup in maybeRedirect()
const redirectMap = redirects.reduce((map, redirect) => {
Expand Down

0 comments on commit 3d77520

Please sign in to comment.