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

feat(gatsby): add assetPrefix to support deploying assets separate from html #12128

Merged
merged 70 commits into from
May 2, 2019
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
860f920
chore: add validation schema, start tweaking webpack config
DSchau Feb 25, 2019
f070da7
chore: keep iterating
DSchau Feb 25, 2019
c32d7f1
chore: yadda
DSchau Feb 25, 2019
4351700
chore: keep working
DSchau Feb 25, 2019
5b8e714
keep doing stuff
DSchau Feb 25, 2019
5e7516f
chore: get mostly done (let's see!)
DSchau Feb 26, 2019
50c4841
chore: remove unused package
DSchau Feb 26, 2019
e6ad72f
chore: ensure url is normalized correctly
DSchau Feb 26, 2019
23749b6
chore: try try again
DSchau Feb 26, 2019
7f9e076
chore: fix for base path
DSchau Feb 26, 2019
6d61a3d
test: tests are important; fix them
DSchau Feb 27, 2019
9a33fa6
chore: remove a silly change
DSchau Feb 27, 2019
e8b6d71
chore: fix linter
DSchau Feb 27, 2019
6687692
fix(gatsby-plugin-offline): hard fail if assetPrefix is used
DSchau Feb 27, 2019
ad04a43
refactor: add a publicPath helper
DSchau Feb 27, 2019
de7e73b
test: add some get public path tests
DSchau Feb 27, 2019
a665cc4
chore: use correct name
DSchau Feb 27, 2019
9acb7cd
docs: add asset prefix doc, and tweak path prefix
DSchau Feb 27, 2019
26c2f33
chore: allow relative url for assetPrefix
DSchau Feb 27, 2019
7d27649
test: add a few more unit tests
DSchau Feb 27, 2019
723bb82
Merge branch 'master' into gatsby/asset-prefix-improved
DSchau Feb 27, 2019
f58d9a3
test: clean up test
DSchau Feb 27, 2019
6af9bad
Merge branch 'gatsby/asset-prefix-improved' of github.com:DSchau/gats…
DSchau Feb 27, 2019
52d3e21
chore: fix e2e-test
DSchau Feb 27, 2019
b144bd2
fix: fall back to empty string, not slash
DSchau Feb 27, 2019
fe0f94a
Update docs/docs/asset-prefix.md
DSchau Feb 28, 2019
20eae0b
Merge remote-tracking branch 'upstream/master' into gatsby/asset-pref…
DSchau Mar 8, 2019
4ac9648
fix: handle relative paths
DSchau Mar 8, 2019
8f25776
Merge branch 'gatsby/asset-prefix-improved' of github.com:DSchau/gats…
DSchau Mar 8, 2019
7e3b1e2
feat: add withAssetPrefix helper for gatsby-link
DSchau Mar 8, 2019
da45256
fix: use withAssetPrefix (if available) for gatsby-plugin-manifest
DSchau Mar 8, 2019
c7c6859
Allow using gatsby-plugin-offline with assetPrefix
Mar 18, 2019
87c9815
Add docs for using offline-plugin with asset prefix
Mar 18, 2019
6110801
clarify docs
Mar 18, 2019
5ec39f7
Merge remote-tracking branch 'upstream/master' into gatsby/asset-pref…
DSchau Mar 26, 2019
82be716
Merge branch 'gatsby/asset-prefix-improved' of github.com:DSchau/gats…
DSchau Mar 26, 2019
7734c5e
feat(*): use withAssetPrefix helper from gatsby-link
DSchau Mar 26, 2019
9234e33
Merge remote-tracking branch 'upstream/master' into gatsby/asset-pref…
DSchau Mar 28, 2019
b8a1c2c
test: get tests passing
DSchau Mar 28, 2019
39403b8
test: add a test for assetPrefix with nesting
DSchau Mar 28, 2019
933f148
Update docs/docs/path-prefix.md
muescha Apr 30, 2019
bbebb41
Merge remote-tracking branch 'upstream/master' into gatsby/asset-pref…
DSchau Apr 30, 2019
dff0e72
chore: fix up merge conflicts/get tests passing
DSchau Apr 30, 2019
f8cfef0
Merge branch 'gatsby/asset-prefix-improved' of github.com:DSchau/gats…
DSchau Apr 30, 2019
560eb79
chore: tweak version
DSchau Apr 30, 2019
37293e3
fix(gatsby-plugin-sitemap): work with asset prefix
DSchau Apr 30, 2019
45ec7c8
fix(gatsby): disallow both relative assetPrefix and pathPrefix
DSchau Apr 30, 2019
b0fef6e
chore: fallback to withPathPrefix, bump peerDep
DSchau Apr 30, 2019
0eb6c85
chore: remove caveat re: trailing slash
DSchau Apr 30, 2019
f0d9a91
fix: gatsby-plugin-sitemap regression
DSchau Apr 30, 2019
0603c31
chore: revert peer dep
DSchau Apr 30, 2019
b22da6e
chore: use basePath if it's defined
DSchau Apr 30, 2019
272551a
chore: remove eslint global comment
DSchau Apr 30, 2019
c3a1230
chore: ensure prefixPaths is set to enable pathPrefix
DSchau Apr 30, 2019
b88c2f8
chore: fix read-only error (can't reassign imports ya dingus)
DSchau Apr 30, 2019
5521302
chore: actually fallback
DSchau Apr 30, 2019
08ede03
Update docs/docs/asset-prefix.md
m-allanson May 1, 2019
d28d6f1
Update docs/docs/path-prefix.md
pieh May 1, 2019
d277729
Update docs/docs/asset-prefix.md
m-allanson May 1, 2019
4d073da
chore: simply/merely remove the easy term ;)
DSchau May 1, 2019
b1241c2
Update docs/docs/asset-prefix.md
m-allanson May 1, 2019
3b8c23a
Merge remote-tracking branch 'upstream/master' into gatsby/asset-pref…
DSchau May 1, 2019
a84aeb9
test: write e2e test for asset prefix
DSchau May 1, 2019
78374b2
chore: fix package json and make isURL test stricter
DSchau May 1, 2019
d226f5e
chore: fix yarn and stuff hopefully
DSchau May 1, 2019
983f010
chore: minor clean up
DSchau May 2, 2019
60c2408
fix(gatsby): fix initial navigation not registering in history
DSchau May 2, 2019
ae9b34c
chore: remove unneccessary dep
DSchau May 2, 2019
4075e95
fix: use __BASE_PATH__ in development runtime too; add a test
DSchau May 2, 2019
2a761db
chore: fix @pieh nit before he finds it
DSchau May 2, 2019
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: 3 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
"globals": {
"before": true,
"spyOn": true,
"__PATH_PREFIX__": true
"__PATH_PREFIX__": true,
"__BASE_PATH__": true,
"__ASSET_PREFIX__": true
},
"rules": {
"arrow-body-style": [
Expand Down
10 changes: 5 additions & 5 deletions packages/gatsby-link/src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import Link, { navigate, push, replace, withPrefix } from "../"

afterEach(() => {
global.__PATH_PREFIX__ = ``
global.__BASE_PATH__ = ``
Copy link
Contributor Author

Choose a reason for hiding this comment

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

BASE_PATH is the new PATH_PREFIX. PATH_PREFIX is now overloaded with assetPrefix + pathPrefix.

cleanup()
})

Expand All @@ -34,12 +34,12 @@ const getReplace = () => {
}

const getWithPrefix = (pathPrefix = ``) => {
global.__PATH_PREFIX__ = pathPrefix
global.__BASE_PATH__ = pathPrefix
return withPrefix
}

const setup = ({ sourcePath = `/active`, linkProps, pathPrefix = `` } = {}) => {
global.__PATH_PREFIX__ = pathPrefix
global.__BASE_PATH__ = pathPrefix
const source = createMemorySource(sourcePath)
const history = createHistory(source)

Expand Down Expand Up @@ -151,11 +151,11 @@ describe(`navigate`, () => {

it(`respects pathPrefix`, () => {
const to = `/some-path`
global.__PATH_PREFIX__ = `/blog`
global.__BASE_PATH__ = `/blog`
getNavigate()(to)

expect(global.___navigate).toHaveBeenCalledWith(
`${global.__PATH_PREFIX__}${to}`,
`${global.__BASE_PATH__}${to}`,
undefined
)
})
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-link/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*global __PATH_PREFIX__ */
/*global __BASE_PATH__ */
import PropTypes from "prop-types"
import React from "react"
import { Link } from "@reach/router"
Expand All @@ -8,7 +8,7 @@ import { parsePath } from "./parse-path"
export { parsePath }

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

function normalizePath(path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { navigate } from "gatsby"
import * as catchLinks from "../catch-links"

beforeAll(() => {
global.__PATH_PREFIX__ = ``
global.__BASE_PATH__ = ``
// Set the base URL we will be testing against to http://localhost:8000/blog
window.history.pushState({}, `APP Url`, `${pathPrefix}`)
})
Expand Down Expand Up @@ -365,13 +365,13 @@ describe(`pathPrefix is handled if catched link to ${pathPrefix}/article navigat
})

afterAll(() => {
global.__PATH_PREFIX__ = ``
global.__BASE_PATH__ = ``
eventDestroyer()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

🔥

})

test(`on sites with pathPrefix '${pathPrefix}'`, done => {
// simulate case with --prefix-paths and prefix /blog
global.__PATH_PREFIX__ = pathPrefix
global.__BASE_PATH__ = pathPrefix

// create the element with href /blog/article
const clickElement = document.createElement(`a`)
Expand Down Expand Up @@ -408,7 +408,7 @@ describe(`pathPrefix is handled if catched link to ${pathPrefix}/article navigat

test(`on sites without pathPrefix`, done => {
// simulate default case without --prefix-paths
global.__PATH_PREFIX__ = ``
global.__BASE_PATH__ = ``

// create the element with href /blog/article
const clickElement = document.createElement(`a`)
Expand Down
11 changes: 5 additions & 6 deletions packages/gatsby-plugin-feed/src/__tests__/gatsby-ssr.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
const { onRenderBody } = require(`../gatsby-ssr`)

const defaultPathPrefix = global.__PATH_PREFIX__

describe(`Adds <Link> for feed to head`, () => {
const prefix = global.__BASE_PATH__
beforeEach(() => {
global.__PATH_PREFIX__ = ``
global.__BASE_PATH__ = ``
})

afterEach(() => {
global.__PATH_PREFIX__ = defaultPathPrefix
afterAll(() => {
global.__BASE_PATH__ = prefix
})

it(`creates Link if feeds does exist`, async () => {
Expand Down Expand Up @@ -55,7 +54,7 @@ describe(`Adds <Link> for feed to head`, () => {
expect(setHeadComponents).toHaveBeenCalledTimes(1)
})
it(`creates Link href with path prefix when __PATH_PREFIX__ sets`, async () => {
global.__PATH_PREFIX__ = `/hogwarts`
global.__BASE_PATH__ = `/hogwarts`

const pluginOptions = {
feeds: [
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-plugin-manifest/src/__tests__/gatsby-ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const ssrArgs = {

describe(`gatsby-plugin-manifest`, () => {
beforeEach(() => {
global.__PATH_PREFIX__ = ``
global.__BASE_PATH__ = ``
headComponents = []
})

Expand Down Expand Up @@ -130,7 +130,7 @@ describe(`gatsby-plugin-manifest`, () => {
})

it(`Creates href attributes using pathPrefix`, () => {
global.__PATH_PREFIX__ = `/path-prefix`
global.__BASE_PATH__ = `/path-prefix`

onRenderBody(ssrArgs, {
icon: true,
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby/cache-dir/__tests__/static-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ describe(`develop-static-entry`, () => {
describe(`static-entry`, () => {
beforeEach(() => {
global.__PATH_PREFIX__ = ``
global.__BASE_PATH__ = ``
})

test(`onPreRenderHTML can be used to replace headComponents`, done => {
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/cache-dir/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ let disableCorePrefetching = false

const queue = {
addPagesArray: newPages => {
findPage = pageFinderFactory(newPages, __PATH_PREFIX__)
findPage = pageFinderFactory(newPages, __BASE_PATH__)
},
addDevRequires: devRequires => {
syncRequires = devRequires
Expand Down
9 changes: 4 additions & 5 deletions packages/gatsby/cache-dir/production-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,17 @@ apiRunnerAsync(`onClientEntry`).then(() => {
// pages have this canonical path)
!page.path.match(/^\/offline-plugin-app-shell-fallback\/?$/)
) {
navigate(
__PATH_PREFIX__ + page.path + browserLoc.search + browserLoc.hash,
{ replace: true }
)
navigate(__BASE_PATH__ + page.path + browserLoc.search + browserLoc.hash, {
replace: true,
})
}

loader.getResourcesForPathname(browserLoc.pathname).then(() => {
const Root = () =>
createElement(
Router,
{
basepath: __PATH_PREFIX__,
basepath: __BASE_PATH__,
},
createElement(RouteHandler, { path: `/*` })
)
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby/cache-dir/static-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@ export default (pagePath, callback) => {

const routerElement = createElement(
ServerLocation,
{ url: `${__PATH_PREFIX__}${pagePath}` },
{ url: `${__BASE_PATH__}${pagePath}` },
createElement(
Router,
{
baseuri: `${__PATH_PREFIX__}`,
baseuri: `${__BASE_PATH__}`,
},
createElement(RouteHandler, { path: `/*` })
)
Expand Down
9 changes: 9 additions & 0 deletions packages/gatsby/src/commands/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ module.exports = async program => {
}
return next()
})
app.use(function(req, res, next) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could probably use cors here 🤷‍♂️

res.header(`Access-Control-Allow-Origin`, `http://${host}:${port}`)
res.header(`Access-Control-Allow-Credentials`, true)
res.header(
`Access-Control-Allow-Headers`,
`Origin, X-Requested-With, Content-Type, Accept`
)
next()
})
app.use(pathPrefix, router)

const server = app.listen(port, host, () => {
Expand Down
10 changes: 9 additions & 1 deletion packages/gatsby/src/joi-schemas/joi.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
const Joi = require(`joi`)

const stripTrailingSlash = chain => chain.replace(/(\w)\//, `$1`)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a nice addition - rather than requiring documentation (e.g. don't add a trailing slash), this will just remove it for us.

It only impacts a string with content, e.g. / is left alone.


export const gatsbyConfigSchema = Joi.object().keys({
__experimentalThemes: Joi.array(),
polyfill: Joi.boolean(),
siteMetadata: Joi.object(),
pathPrefix: Joi.string(),
assetPrefix: stripTrailingSlash(Joi.string().uri()),
pathPrefix: stripTrailingSlash(
Joi.string().uri({
allowRelative: true,
relativeOnly: true,
})
),
mapping: Joi.object(),
plugins: Joi.array(),
proxy: Joi.object().keys({
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby/src/utils/eslint-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = schema => {
globals: {
graphql: true,
__PATH_PREFIX__: true,
__BASE_PATH__: true, // this will rarely, if ever, be used by consumers
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For more context here--it's used in the internal linking (e.g. with just pathPrefix) because pathPrefix itself is overloaded.

},
extends: `react-app`,
plugins: [`graphql`],
Expand Down
4 changes: 4 additions & 0 deletions packages/gatsby/src/utils/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ export function joinPath(...paths) {
export function withBasePath(basePath) {
return (...paths) => joinPath(basePath, ...paths)
}

export function withTrailingSlash(basePath) {
return basePath.replace(/([\/]+)?$/, `/`)
}
35 changes: 19 additions & 16 deletions packages/gatsby/src/utils/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ require(`v8-compile-cache`)

const fs = require(`fs-extra`)
const path = require(`path`)
const url = require(`url`)
const dotenv = require(`dotenv`)
const FriendlyErrorsWebpackPlugin = require(`friendly-errors-webpack-plugin`)
const { store } = require(`../redux`)
const { actions } = require(`../redux/actions`)
const debug = require(`debug`)(`gatsby:webpack-config`)
const report = require(`gatsby-cli/lib/reporter`)
const { withBasePath } = require(`./path`)
const { withBasePath, withTrailingSlash } = require(`./path`)

const apiRunnerNode = require(`./api-runner-node`)
const createUtils = require(`./webpack-utils`)
Expand All @@ -20,12 +21,7 @@ const hasLocalEslint = require(`./local-eslint-config-finder`)
// 3) build-javascript: Build JS and CSS chunks for production
// 4) build-html: build all HTML files

module.exports = async (
program,
directory,
suppliedStage,
webpackPort = 1500
) => {
module.exports = async (program, directory, suppliedStage) => {
const directoryPath = withBasePath(directory)

process.env.GATSBY_BUILD_STAGE = suppliedStage
Expand All @@ -35,6 +31,15 @@ module.exports = async (
const stage = suppliedStage
const { rules, loaders, plugins } = await createUtils({ stage, program })

const { assetPrefix, pathPrefix } = store.getState().config

let publicPath = `/`
if (program.prefixPaths && (pathPrefix || assetPrefix)) {
publicPath = url.resolve(
...[assetPrefix, pathPrefix].map(part => part || ``)
)
}

function processEnv(stage, defaultNodeEnv) {
debug(`Building env for "${stage}"`)
const env = process.env.NODE_ENV
Expand Down Expand Up @@ -91,7 +96,7 @@ module.exports = async (
if (pubPath.substr(-1) === `/`) {
hmrBasePath = pubPath
} else {
hmrBasePath = `${pubPath}/`
hmrBasePath = withTrailingSlash(pubPath)
}
}

Expand Down Expand Up @@ -126,18 +131,14 @@ module.exports = async (
library: `lib`,
umdNamedDefine: true,
globalObject: `this`,
publicPath: program.prefixPaths
? `${store.getState().config.pathPrefix}/`
: `/`,
publicPath: withTrailingSlash(publicPath),
}
case `build-javascript`:
return {
filename: `[name]-[contenthash].js`,
chunkFilename: `[name]-[contenthash].js`,
path: directoryPath(`public`),
publicPath: program.prefixPaths
? `${store.getState().config.pathPrefix}/`
: `/`,
publicPath: withTrailingSlash(publicPath),
}
default:
throw new Error(`The state requested ${stage} doesn't exist.`)
Expand Down Expand Up @@ -181,8 +182,10 @@ module.exports = async (
// optimizations for React) and what the link prefix is (__PATH_PREFIX__).
plugins.define({
...processEnv(stage, `development`),
__PATH_PREFIX__: JSON.stringify(
program.prefixPaths ? store.getState().config.pathPrefix : ``
__BASE_PATH__: JSON.stringify(program.prefixPaths ? pathPrefix : ``),
Copy link
Contributor

Choose a reason for hiding this comment

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

Worth adding a note to explain why base_path = pathPrefix and path_prefix = publicPath? Pulling from your earlier comment, something like:

BASE_PATH is the new PATH_PREFIX. PATH_PREFIX is now overloaded with assetPrefix + pathPrefix. More info at #12128

__PATH_PREFIX__: JSON.stringify(program.prefixPaths ? publicPath : ``),
__ASSET_PREFIX__: JSON.stringify(
program.prefixPaths ? assetPrefix : ``
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure where to document this, but I feel like it would be useful to define differences between those. To my understanding plugins (for now) should only ever use __PATH_PREFIX__? __ASSET_PREFIX__ seems not used ATM anywhere - would it make sense not to define it at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct! __BASE_PATH__ should be used for what was __PATH_PREFIX__, and __ASSET_PREFIX__ isn't really used. I'll look for a best place to document this!

),
}),
]
Expand Down
1 change: 1 addition & 0 deletions www/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"author": "Kyle Mathews <mathews.kyle@gmail.com>",
"dependencies": {
"@reach/skip-nav": "^0.1.1",
"@reach/visually-hidden": "^0.1.2",
"axios": "^0.18.0",
"bluebird": "^3.5.1",
"dotenv": "^6.0.0",
Expand Down