From 1a1784d22e7fb7131ffe2258a1a1403e15a2ac55 Mon Sep 17 00:00:00 2001 From: Dustin Schau Date: Fri, 3 May 2019 08:15:35 -0500 Subject: [PATCH 1/2] fix(gatsby-link): provide fallback for __BASE_PATH__ being missing --- packages/gatsby-link/src/__tests__/index.js | 37 +++++++++++++++++++-- packages/gatsby-link/src/index.js | 7 +++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/packages/gatsby-link/src/__tests__/index.js b/packages/gatsby-link/src/__tests__/index.js index 88793411025da..a17c4190965e0 100644 --- a/packages/gatsby-link/src/__tests__/index.js +++ b/packages/gatsby-link/src/__tests__/index.js @@ -8,11 +8,13 @@ import { } from "@reach/router" import Link, { navigate, push, replace, withPrefix, withAssetPrefix } from "../" -afterEach(() => { +beforeEach(() => { global.__BASE_PATH__ = `` - cleanup() + global.__PATH_PREFIX__ = `` }) +afterEach(cleanup) + const getInstance = (props, pathPrefix = ``) => { getWithPrefix()(pathPrefix) return @@ -92,6 +94,28 @@ describe(``, () => { }).not.toThrow() }) + it(`does not fail with missing __BASE_PATH__`, () => { + global.__PATH_PREFIX__ = `` + delete global.__BASE_PATH__ + const source = createMemorySource(`/active`) + + expect(() => + render( + + + link + + + ) + ).not.toThrow() + }) + describe(`the location to link to`, () => { global.___loader = { enqueue: jest.fn(), @@ -150,6 +174,15 @@ describe(`withPrefix`, () => { const root = getWithPrefix(pathPrefix)(to) expect(root).toEqual(`${pathPrefix}${to}`) }) + + it(`falls back to __PATH_PREFIX__ if __BASE_PATH__ is undefined`, () => { + delete global.__BASE_PATH__ + global.__PATH_PREFIX__ = `/blog` + + const to = `/abc/` + + expect(withPrefix(to)).toBe(`${global.__PATH_PREFIX__}${to}`) + }) }) }) diff --git a/packages/gatsby-link/src/index.js b/packages/gatsby-link/src/index.js index c27af7a39a9e2..2412220cb1800 100644 --- a/packages/gatsby-link/src/index.js +++ b/packages/gatsby-link/src/index.js @@ -7,7 +7,12 @@ import { parsePath } from "./parse-path" export { parsePath } export function withPrefix(path) { - return normalizePath(`${__BASE_PATH__}/${path}`) + return normalizePath( + [ + typeof __BASE_PATH__ !== `undefined` ? __BASE_PATH__ : __PATH_PREFIX__, + path, + ].join(`/`) + ) } export function withAssetPrefix(path) { From 1b157174f90709fb46a9441252fde2192ef18c52 Mon Sep 17 00:00:00 2001 From: Dustin Schau Date: Fri, 3 May 2019 10:16:21 -0500 Subject: [PATCH 2/2] chore: fingers crossed emoji pass node 6 tests --- packages/gatsby-link/src/__tests__/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/gatsby-link/src/__tests__/index.js b/packages/gatsby-link/src/__tests__/index.js index a17c4190965e0..83524fb424474 100644 --- a/packages/gatsby-link/src/__tests__/index.js +++ b/packages/gatsby-link/src/__tests__/index.js @@ -96,7 +96,8 @@ describe(``, () => { it(`does not fail with missing __BASE_PATH__`, () => { global.__PATH_PREFIX__ = `` - delete global.__BASE_PATH__ + global.__BASE_PATH__ = undefined + const source = createMemorySource(`/active`) expect(() => @@ -176,7 +177,7 @@ describe(`withPrefix`, () => { }) it(`falls back to __PATH_PREFIX__ if __BASE_PATH__ is undefined`, () => { - delete global.__BASE_PATH__ + global.__BASE_PATH__ = undefined global.__PATH_PREFIX__ = `/blog` const to = `/abc/`