Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby-link): Do not crash in unit tests when globals are undefined #25608

Merged
merged 5 commits into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/gatsby-link/src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ describe(`<Link />`, () => {
})

it(`does not fail with missing __BASE_PATH__`, () => {
global.__PATH_PREFIX__ = ``
global.__BASE_PATH__ = undefined
delete global.__PATH_PREFIX__
delete global.__BASE_PATH__

const source = createMemorySource(`/active`)

Expand Down
21 changes: 18 additions & 3 deletions packages/gatsby-link/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,44 @@ export { parsePath }

const isAbsolutePath = path => path?.startsWith(`/`)

export function withPrefix(path, prefix = __BASE_PATH__) {
export function withPrefix(path, prefix = getGlobalBasePrefix()) {
if (!isLocalLink(path)) {
return path
}

if (path.startsWith(`./`) || path.startsWith(`../`)) {
return path
}
const base = prefix ?? __PATH_PREFIX__ ?? `/`
const base = prefix ?? getGlobalPathPrefix() ?? `/`

return `${base?.endsWith(`/`) ? base.slice(0, -1) : base}${
path.startsWith(`/`) ? path : `/${path}`
}`
}

// These global values are wrapped in typeof clauses to ensure the values exist.
// This is especially problematic in unit testing of this component.
const getGlobalPathPrefix = () =>
process.env.NODE_ENV !== `production`
? typeof __PATH_PREFIX__ !== `undefined`
? __PATH_PREFIX__
: undefined
: __PATH_PREFIX__
Comment on lines +29 to +34
Copy link
Contributor

@ascorbic ascorbic Jul 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nested ternaries are really hard to read. How about

Edit: oops

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ironically, the ternary confused me so I misunderstood what was happening! I see what you're doing, but I'd still rather replafe the ternary with an early return.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the context of this discussion; I prefer to use === over !== if there is an else branch unless there's a clear reason to show the !== branch first. I think it reads better as inverting adds to the mental overhead of parsing code. I agree with Matt that this one is a little hairy. Sometimes it is what it is.

I would consider to ignore the production check (what purpose does that serve?) since it currently assumes the var exists in production anyways, so typeof __PATH_PREFIX__ === 'undefined' should be false regardless. So I'd suggest this instead.

Suggested change
const getGlobalPathPrefix = () =>
process.env.NODE_ENV !== `production`
? typeof __PATH_PREFIX__ !== `undefined`
? __PATH_PREFIX__
: undefined
: __PATH_PREFIX__
const getGlobalPathPrefix = () =>
// Prevent reference error; the `__PATH_PREFIX__` binding may not exist in tests
typeof __PATH_PREFIX__ === `undefined` ? undefined : __PATH_PREFIX__

const getGlobalBasePrefix = () =>
process.env.NODE_ENV !== `production`
? typeof __BASE_PATH__ !== `undefined`
? __BASE_PATH__
: undefined
: __BASE_PATH__

const isLocalLink = path =>
path &&
!path.startsWith(`http://`) &&
!path.startsWith(`https://`) &&
!path.startsWith(`//`)

export function withAssetPrefix(path) {
return withPrefix(path, __PATH_PREFIX__)
return withPrefix(path, getGlobalPathPrefix())
}

function absolutify(path, current) {
Expand Down