Skip to content

Commit

Permalink
different approach
Browse files Browse the repository at this point in the history
  • Loading branch information
blainekasten committed Jul 9, 2020
1 parent 572e68a commit 91a894b
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions packages/gatsby-link/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,40 @@ import { parsePath } from "./parse-path"

export { parsePath }

if (process.env.NODE_ENV !== `production`) {
if (typeof __BASE_PATH__ === `undefined`) {
var __BASE_PATH__ = ``
}
if (typeof __PATH_PREFIX__ === `undefined`) {
var __PATH_PREFIX__ = ``
}
}

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

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

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

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__
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, getGlobalPathPrefix())
return withPrefix(path, __PATH_PREFIX__)
}

function absolutify(path, current) {
Expand Down

0 comments on commit 91a894b

Please sign in to comment.