Skip to content

Commit

Permalink
[v2] fix build with pathPrefix (#5691)
Browse files Browse the repository at this point in the history
* [v2] fix build with pathPrefix

* update test

* removed __PREFIX_PATHS__

* fix test

* update lock file

* address comments

* clean

* clean
  • Loading branch information
nihgwu authored and m-allanson committed Jun 6, 2018
1 parent dacc6b5 commit 456c932
Show file tree
Hide file tree
Showing 14 changed files with 450 additions and 599 deletions.
7 changes: 1 addition & 6 deletions examples/using-remark-copy-linked-files/src/layouts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ class Template extends React.Component {
const { location, children } = this.props
let header

let rootPath = `/`
if (typeof __PREFIX_PATHS__ !== `undefined` && __PREFIX_PATHS__) {
rootPath = __PATH_PREFIX__ + `/`
}

if (location.pathname === rootPath) {
if (location.pathname === `/`) {
header = (
<h1
style={{
Expand Down
1 change: 0 additions & 1 deletion flow-typed/globals.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
declare var __PREFIX_PATHS__: bool;
declare var __PATH_PREFIX__: string;
declare module 'gatsby-helpers' {
declare function prefixLink(link: string): string;
Expand Down
35 changes: 8 additions & 27 deletions packages/gatsby-link/src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { MemoryRouter } from "react-router-dom"

const getInstance = (props, pathPrefix = ``) => {
Object.assign(global.window, {
__PREFIX_PATHS__: pathPrefix ? true : false,
__PATH_PREFIX__: pathPrefix,
})

Expand All @@ -24,44 +23,30 @@ const getNavigateTo = () => {

const getWithPrefix = (pathPrefix = ``) => {
Object.assign(global.window, {
__PREFIX_PATHS__: pathPrefix ? true : false,
__PATH_PREFIX__: pathPrefix,
})
return require(`../`).withPrefix
}

describe(`<Link />`, () => {
it(`does not fail to initialize when __PREFIX_PATHS__ is not defined`, () => {
it(`does not fail to initialize without --prefix-paths`, () => {
expect(() => {
const context = { router: { history: {} } }
const Link = require(`../`).default
const link = new Link({}, context) //eslint-disable-line no-unused-vars
getInstance({})
}).not.toThrow()
})

describe(`path prefixing`, () => {
it(`does not include path prefix by default`, () => {
const to = `/path`
const instance = getInstance({
to,
})

expect(instance.state.to.pathname).toEqual(to)
})

/*
* Running _both_ of these tests causes the globals to be cached or something
*/
it.skip(`will use __PATH_PREFIX__ if __PREFIX_PATHS__ defined`, () => {
it(`does not include path prefix`, () => {
const to = `/path`
const pathPrefix = `/blog`
const instance = getInstance({ to }, pathPrefix)
expect(instance.state.to).toEqual(`${pathPrefix}${to}`)

expect(instance.state.to.pathname).toEqual(to)
})
})

describe(`the location to link to`, () => {
global.___loader = {
global.window.___loader = {
enqueue: jest.fn(),
}

Expand Down Expand Up @@ -135,19 +120,15 @@ describe(`<Link />`, () => {
})
})

describe(`withRouter`, () => {
describe(`withPrefix`, () => {
describe(`works with default prefix`, () => {
it(`default prefix does not return "//"`, () => {
const to = `/`
const root = getWithPrefix()(to)
expect(root).toEqual(to)
})

/*
* Same as above, setting a path prefix does not work because the
* link module sets variables on first import
*/
it.skip(`respects path prefix`, () => {
it(`respects path prefix`, () => {
const to = `/abc/`
const pathPrefix = `/blog`
const root = getWithPrefix(pathPrefix)(to)
Expand Down
11 changes: 3 additions & 8 deletions packages/gatsby-link/src/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
/*global __PREFIX_PATHS__, __PATH_PREFIX__ */
/*global __PATH_PREFIX__ */
import PropTypes from "prop-types"
import React from "react"
import { Link, NavLink } from "react-router-dom"
import { polyfill } from "react-lifecycles-compat"
import { createLocation, createPath } from "history"

let pathPrefix = `/`
if (typeof __PREFIX_PATHS__ !== `undefined` && __PREFIX_PATHS__) {
pathPrefix = __PATH_PREFIX__
}

export function withPrefix(path) {
return normalizePath(pathPrefix + path)
return normalizePath(`${__PATH_PREFIX__}/${path}`)
}

function normalizePath(path) {
return path.replace(/^\/\//g, `/`)
return path.replace(/\/+/g, `/`)
}

const NavLinkPropTypes = {
Expand Down
1 change: 0 additions & 1 deletion packages/gatsby/src/cache-dir/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"browser": true
},
"globals": {
"__PREFIX_PATHS__": false,
"__PATH_PREFIX__": false,
"___emitter": false
}
Expand Down
37 changes: 3 additions & 34 deletions packages/gatsby/src/cache-dir/__tests__/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@ import loader from "../loader.js"
describe(`Loader`, () => {
beforeEach(() => {
global.__PATH_PREFIX__ = ``
global.__PREFIX_PATHS__ = false

// Workaround for Node 6 issue: https://github.com/facebook/jest/issues/5159
if (global.hasOwnProperty(`__PATH_PREFIX__`))
global.__PATH_PREFIX__ = ``
if (global.hasOwnProperty(`__PREFIX_PATHS__`))
global.__PREFIX_PATHS__ = false

loader.empty()
loader.addPagesArray([
Expand Down Expand Up @@ -95,14 +88,7 @@ describe(`Loader path prefixing`, () => {
let pagesArray

beforeEach(() => {
delete global.__PATH_PREFIX__
delete global.__PREFIX_PATHS__

// Workaround for Node 6 issue: https://github.com/facebook/jest/issues/5159
if (global.hasOwnProperty(`__PATH_PREFIX__`))
global.__PATH_PREFIX__ = undefined
if (global.hasOwnProperty(`__PREFIX_PATHS__`))
global.__PREFIX_PATHS__ = undefined
global.__PATH_PREFIX__ = ``

pagesArray = [
{
Expand All @@ -122,36 +108,19 @@ describe(`Loader path prefixing`, () => {

test(`Path prefix present and enabled`, () => {
global.__PATH_PREFIX__ = `/foo`
global.__PREFIX_PATHS__ = true
loader.addPagesArray(pagesArray)
loader.enqueue(`/foo/about/`)

expect(loader.___resources()).toEqual([
`about.json`,
`page-component---src-pages-test-js`,
])
})

test(`Path prefix present but not enabled`, () => {
global.__PATH_PREFIX__ = `/foo`
delete global.__PREFIX_PATHS__
loader.addPagesArray(pagesArray)

// do not enqueue prefixed paths
loader.enqueue(`/foo/about/`)
expect(loader.___resources()).toEqual([])

// do enqueue unprefixed paths
loader.enqueue(`/about/`)
expect(loader.___resources()).toEqual([
`about.json`,
`page-component---src-pages-test-js`,
])
})

test(`Path prefix missing but enabled`, () => {
delete global.__PATH_PREFIX__
global.__PREFIX_PATHS__ = true
global.__PATH_PREFIX__ = ``

loader.addPagesArray(pagesArray)

// don't enqueue prefixed paths
Expand Down
6 changes: 1 addition & 5 deletions packages/gatsby/src/cache-dir/history.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import createHistory from "history/createBrowserHistory"
import { apiRunner } from "./api-runner-browser"

let pathPrefix = `/`
if (__PREFIX_PATHS__) {
pathPrefix = `${__PATH_PREFIX__}/`
}
const basename = pathPrefix.slice(0, -1)
const basename = __PATH_PREFIX__

const pluginResponses = apiRunner(`replaceHistory`, { basename })
const replacementHistory = pluginResponses[0]
Expand Down
13 changes: 4 additions & 9 deletions packages/gatsby/src/cache-dir/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ let hasFetched = Object.create(null)
let syncRequires = {}
let asyncRequires = {}
let jsonDataPaths = {}
let pathPrefix = `/`
let fetchHistory = []
let fetchingPageResourceMapPromise = null
let fetchedPageResourceMap = false
Expand Down Expand Up @@ -53,7 +52,7 @@ const fetchResource = resourceName => {
if (resourceName in jsonStore) {
resolve(jsonStore[resourceName])
} else {
const url = `${pathPrefix}static/d/${
const url = `${__PATH_PREFIX__}/static/d/${
jsonDataPaths[resourceName]
}.json`
var req = new XMLHttpRequest()
Expand Down Expand Up @@ -170,14 +169,10 @@ const queue = {
empty: () => {
resourcesCount = Object.create(null)
resourcesArray = []
pathPrefix = `/`
},

addPagesArray: newPages => {
if (typeof __PREFIX_PATHS__ !== `undefined`) {
pathPrefix = typeof __PATH_PREFIX__ !== `undefined` ? `${__PATH_PREFIX__}/` : `/`
}
findPage = pageFinderFactory(newPages, pathPrefix.slice(0, -1))
findPage = pageFinderFactory(newPages, __PATH_PREFIX__)
},
addDevRequires: devRequires => {
syncRequires = devRequires
Expand All @@ -193,11 +188,11 @@ const queue = {
// click on it soon so let's start prefetching resources for this
// pathname.
hovering: rawPath => {
const path = stripPrefix(rawPath, pathPrefix.slice(0, -1))
const path = stripPrefix(rawPath, __PATH_PREFIX__)
queue.getResourcesForPathname(path)
},
enqueue: rawPath => {
const path = stripPrefix(rawPath, pathPrefix.slice(0, -1))
const path = stripPrefix(rawPath, __PATH_PREFIX__)

// Tell plugins with custom prefetching logic that they should start
// prefetching this path.
Expand Down
7 changes: 1 addition & 6 deletions packages/gatsby/src/cache-dir/production-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,11 @@ apiRunnerAsync(`onClientEntry`).then(() => {
const AltRouter = apiRunner(`replaceRouterComponent`, { history })[0]

loader.getResourcesForPathname(window.location.pathname, () => {
let pathPrefix = `/`
if (__PREFIX_PATHS__) {
pathPrefix = `${__PATH_PREFIX__}/`
}

const Root = () =>
createElement(
AltRouter ? AltRouter : Router,
{
basename: pathPrefix.slice(0, -1),
basename: __PATH_PREFIX__,
history: !AltRouter ? history : undefined,
},
createElement(
Expand Down
7 changes: 1 addition & 6 deletions packages/gatsby/src/cache-dir/register-service-worker.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import emitter from "./emitter"

let pathPrefix = `/`
if (__PREFIX_PATHS__) {
pathPrefix = `${__PATH_PREFIX__}/`
}

if (`serviceWorker` in navigator) {
navigator.serviceWorker
.register(`${pathPrefix}sw.js`)
.register(`${__PATH_PREFIX__}/sw.js`)
.then(function(reg) {
reg.addEventListener(`updatefound`, () => {
// The updatefound event implies that reg.installing is set; see
Expand Down
7 changes: 1 addition & 6 deletions packages/gatsby/src/cache-dir/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,13 @@ const navigateTo = to => {

window.___navigateTo = navigateTo

let pathPrefix = `/`
if (__PREFIX_PATHS__) {
pathPrefix = `${__PATH_PREFIX__}/`
}

const AltRouter = apiRunner(`replaceRouterComponent`, { history })[0]

const Root = () =>
createElement(
AltRouter ? AltRouter : Router,
{
basename: pathPrefix.slice(0, -1),
basename: __PATH_PREFIX__,
history: !AltRouter ? history : undefined,
},
createElement(
Expand Down
5 changes: 1 addition & 4 deletions packages/gatsby/src/cache-dir/static-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ const getPage = path => pages.find(page => page.path === path)
const createElement = React.createElement

export default (pagePath, callback) => {
let pathPrefix = `/`
if (__PREFIX_PATHS__) {
pathPrefix = `${__PATH_PREFIX__}/`
}
const pathPrefix = `${__PATH_PREFIX__}/`

let bodyHtml = ``
let headComponents = []
Expand Down
8 changes: 4 additions & 4 deletions packages/gatsby/src/utils/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,12 @@ module.exports = async (
plugins.moment(),

// Add a few global variables. Set NODE_ENV to production (enables
// optimizations for React) and whether prefixing links is enabled
// (__PREFIX_PATHS__) and what the link prefix is (__PATH_PREFIX__).
// optimizations for React) and what the link prefix is (__PATH_PREFIX__).
plugins.define({
"process.env": processEnv(stage, `development`),
__PREFIX_PATHS__: program.prefixPaths,
__PATH_PREFIX__: JSON.stringify(store.getState().config.pathPrefix),
__PATH_PREFIX__: JSON.stringify(
program.prefixPaths ? store.getState().config.pathPrefix : ``
),
}),
]

Expand Down
Loading

0 comments on commit 456c932

Please sign in to comment.