-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Changes from all 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 f070da7
chore: keep iterating
DSchau c32d7f1
chore: yadda
DSchau 4351700
chore: keep working
DSchau 5b8e714
keep doing stuff
DSchau 5e7516f
chore: get mostly done (let's see!)
DSchau 50c4841
chore: remove unused package
DSchau e6ad72f
chore: ensure url is normalized correctly
DSchau 23749b6
chore: try try again
DSchau 7f9e076
chore: fix for base path
DSchau 6d61a3d
test: tests are important; fix them
DSchau 9a33fa6
chore: remove a silly change
DSchau e8b6d71
chore: fix linter
DSchau 6687692
fix(gatsby-plugin-offline): hard fail if assetPrefix is used
DSchau ad04a43
refactor: add a publicPath helper
DSchau de7e73b
test: add some get public path tests
DSchau a665cc4
chore: use correct name
DSchau 9acb7cd
docs: add asset prefix doc, and tweak path prefix
DSchau 26c2f33
chore: allow relative url for assetPrefix
DSchau 7d27649
test: add a few more unit tests
DSchau 723bb82
Merge branch 'master' into gatsby/asset-prefix-improved
DSchau f58d9a3
test: clean up test
DSchau 6af9bad
Merge branch 'gatsby/asset-prefix-improved' of github.com:DSchau/gats…
DSchau 52d3e21
chore: fix e2e-test
DSchau b144bd2
fix: fall back to empty string, not slash
DSchau fe0f94a
Update docs/docs/asset-prefix.md
DSchau 20eae0b
Merge remote-tracking branch 'upstream/master' into gatsby/asset-pref…
DSchau 4ac9648
fix: handle relative paths
DSchau 8f25776
Merge branch 'gatsby/asset-prefix-improved' of github.com:DSchau/gats…
DSchau 7e3b1e2
feat: add withAssetPrefix helper for gatsby-link
DSchau da45256
fix: use withAssetPrefix (if available) for gatsby-plugin-manifest
DSchau c7c6859
Allow using gatsby-plugin-offline with assetPrefix
87c9815
Add docs for using offline-plugin with asset prefix
6110801
clarify docs
5ec39f7
Merge remote-tracking branch 'upstream/master' into gatsby/asset-pref…
DSchau 82be716
Merge branch 'gatsby/asset-prefix-improved' of github.com:DSchau/gats…
DSchau 7734c5e
feat(*): use withAssetPrefix helper from gatsby-link
DSchau 9234e33
Merge remote-tracking branch 'upstream/master' into gatsby/asset-pref…
DSchau b8a1c2c
test: get tests passing
DSchau 39403b8
test: add a test for assetPrefix with nesting
DSchau 933f148
Update docs/docs/path-prefix.md
muescha bbebb41
Merge remote-tracking branch 'upstream/master' into gatsby/asset-pref…
DSchau dff0e72
chore: fix up merge conflicts/get tests passing
DSchau f8cfef0
Merge branch 'gatsby/asset-prefix-improved' of github.com:DSchau/gats…
DSchau 560eb79
chore: tweak version
DSchau 37293e3
fix(gatsby-plugin-sitemap): work with asset prefix
DSchau 45ec7c8
fix(gatsby): disallow both relative assetPrefix and pathPrefix
DSchau b0fef6e
chore: fallback to withPathPrefix, bump peerDep
DSchau 0eb6c85
chore: remove caveat re: trailing slash
DSchau f0d9a91
fix: gatsby-plugin-sitemap regression
DSchau 0603c31
chore: revert peer dep
DSchau b22da6e
chore: use basePath if it's defined
DSchau 272551a
chore: remove eslint global comment
DSchau c3a1230
chore: ensure prefixPaths is set to enable pathPrefix
DSchau b88c2f8
chore: fix read-only error (can't reassign imports ya dingus)
DSchau 5521302
chore: actually fallback
DSchau 08ede03
Update docs/docs/asset-prefix.md
m-allanson d28d6f1
Update docs/docs/path-prefix.md
pieh d277729
Update docs/docs/asset-prefix.md
m-allanson 4d073da
chore: simply/merely remove the easy term ;)
DSchau b1241c2
Update docs/docs/asset-prefix.md
m-allanson 3b8c23a
Merge remote-tracking branch 'upstream/master' into gatsby/asset-pref…
DSchau a84aeb9
test: write e2e test for asset prefix
DSchau 78374b2
chore: fix package json and make isURL test stricter
DSchau d226f5e
chore: fix yarn and stuff hopefully
DSchau 983f010
chore: minor clean up
DSchau 60c2408
fix(gatsby): fix initial navigation not registering in history
DSchau ae9b34c
chore: remove unneccessary dep
DSchau 4075e95
fix: use __BASE_PATH__ in development runtime too; add a test
DSchau 2a761db
chore: fix @pieh nit before he finds it
DSchau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
--- | ||
title: Adding an Asset Prefix | ||
--- | ||
|
||
Gatsby produces static content that can be hosted _anywhere_ at scale in a cost-effective manner. This static content is comprised of HTML files, JavaScript, CSS, images, and more that power your great Gatsby application. | ||
|
||
In some circumstances you may want to deploy _assets_ (non-HTML resources such as JavaScript, CSS, etc.) to a separate domain. Typically this is when you're required to use a dedicated CDN for assets or need to follow company-specific hosting policies. | ||
|
||
This `assetPrefix` functionality is available starting in gatsby@2.4.0, so that you can seamlessly use Gatsby with assets hosted from a separate domain. To use this functionality, ensure that your version of `gatsby` specified in `package.json` is at least `2.4.0`. | ||
|
||
## Usage | ||
|
||
### Adding to `gatsby-config.js` | ||
|
||
```js:title=gatsby-config.js | ||
module.exports = { | ||
assetPrefix: `https://cdn.example.com`, | ||
} | ||
``` | ||
|
||
One more step - when we build out this application, we need to add a flag so that Gatsby picks up this option. | ||
|
||
### The `--prefix-paths` flag | ||
|
||
When building with an `assetPrefix`, we require a `--prefix-paths` flag. If this flag is not specified, the build will ignore this option, and build out content as if it was hosted on the same domain. To ensure we build out successfully, use the following command: | ||
|
||
```shell | ||
gatsby build --prefix-paths | ||
``` | ||
|
||
That's it! We now have an application that is ready to have its assets deployed from a CDN and its core files (e.g. HTML files) can be hosted on a separate domain. | ||
|
||
## Building / Deploying | ||
|
||
Once your application is built out, all assets will be automatically prefixed by this asset prefix. For example, if we have a JavaScript file `app-common-1234.js`, the script tag will look something like: | ||
|
||
```html | ||
<script src="https://cdn.example.com/app-common-1234.js"></script> | ||
``` | ||
|
||
However - if we were to deploy our application as-is, those assets would not be available! We can do this in a few ways, but the general approach will be to deploy the contents of the `public` folder to _both_ your core domain, and the CDN/asset prefix location. | ||
|
||
### Using `onPostBuild` | ||
|
||
We expose an [`onPostBuild`](/docs/node-apis/#onPostBuild) API hook. This can be used to deploy your content to the CDN, like so: | ||
|
||
```js:title=gatsby-node.js | ||
const assetsDirectory = `public` | ||
|
||
exports.onPostBuild = async function onPostBuild() { | ||
// do something with public | ||
// e.g. upload to S3 | ||
} | ||
``` | ||
|
||
### Using `package.json` scripts | ||
|
||
Additionally, we can use an npm script, which will let us use some command line interfaces/executables to perform some action, in this case, deploying our assets directory! | ||
|
||
In this example, I'll use the `aws-cli` and `s3` to sync the `public` folder (containing all our assets) to the `s3` bucket. | ||
|
||
```json:title=package.json | ||
{ | ||
"scripts": { | ||
"build": "gatsby build --prefix-paths", | ||
"postbuild": "aws s3 sync public s3://mybucket" | ||
} | ||
} | ||
``` | ||
|
||
Now whenever the `build` script is invoked, e.g. `npm run build`, the `postbuild` script will be invoked _after_ the build completes, therefore making our assets available on a _separate_ domain after we have finished building out our application with prefixed assets. | ||
|
||
## Additional Considerations | ||
|
||
### Usage with `pathPrefix` | ||
|
||
The [`pathPrefix`](/docs/path-prefix/) feature can be thought of as semi-related to this feature. That feature allows _all_ your website content to be prefixed with some constant prefix, for example you may want your blog to be hosted from `/blog` rather than the project root. | ||
|
||
This feature works seamlessly with `pathPrefix`. Build out your application with the `--prefix-paths` flag and you'll be well on your way to hosting an application with its assets hosted on a CDN, and its core functionality available behind a path prefix. | ||
|
||
### Usage with `gatsby-plugin-offline` | ||
|
||
When using a custom asset prefix with `gatsby-plugin-offline`, your assets can still be cached offline. However, to ensure the plugin works correctly, there are a few things you need to do. | ||
|
||
1. Your asset server needs to have the following headers set: | ||
|
||
``` | ||
Access-Control-Allow-Origin: <site origin> | ||
Access-Control-Allow-Credentials: true | ||
``` | ||
|
||
Note that the origin needs to be specific, rather than using `*` to allow all origins. This is because Gatsby makes requests to fetch resources with `withCredentials` set to `true`, which disallows using `*` to match all origins. This is also why the second header is required. For local testing, use `http://localhost:9000` as the origin. | ||
|
||
2. Certain essential resources need to be available on your content server (i.e. the one used to serve pages). This includes `sw.js`, as well as resources to precache: the Webpack bundle, the app bundle, the manifest (and any icons referenced), and the resources for the offline plugin app shell. | ||
|
||
You can find most of these by looking for the `self.__precacheManifest` variable in your generated `sw.js`. Remember to also include `sw.js` itself, and any icons referenced in your `manifest.webmanifest` if you have one. To check your service worker is functioning as expected, look in Application → Service Workers in your browser dev tools, and check for any failed resources in the Console/Network tabs. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const { assetPrefix } = require(`../../gatsby-config`) | ||
|
||
const assetPrefixExpression = new RegExp(`^${assetPrefix}`) | ||
|
||
const assetPrefixMatcher = (chain, attr = `href`) => | ||
chain.should(`have.attr`, attr).and(`matches`, assetPrefixExpression) | ||
|
||
describe(`assetPrefix`, () => { | ||
beforeEach(() => { | ||
cy.visit(`/`).waitForRouteChange() | ||
}) | ||
|
||
describe(`runtime`, () => { | ||
it(`prefixes preloads`, () => { | ||
assetPrefixMatcher(cy.get(`head link[rel="preload"]`)) | ||
}) | ||
|
||
it(`prefixes styles`, () => { | ||
assetPrefixMatcher(cy.get(`head style[data-href]`), `data-href`) | ||
}) | ||
|
||
it(`prefixes scripts`, () => { | ||
assetPrefixMatcher(cy.get(`body script[src]`), `src`) | ||
}) | ||
}) | ||
|
||
describe(`gatsby-plugin-manifest`, () => { | ||
it(`prefixes manifest`, () => { | ||
assetPrefixMatcher(cy.get(`head link[rel="manifest"]`)) | ||
}) | ||
|
||
it(`prefixes shortcut icon`, () => { | ||
assetPrefixMatcher(cy.get(`head link[rel="shortcut icon"]`)) | ||
}) | ||
|
||
it(`prefixes manifest icons`, () => { | ||
assetPrefixMatcher(cy.get(`head link[rel="apple-touch-icon"]`)) | ||
}) | ||
}) | ||
|
||
describe(`gatsby-plugin-sitemap`, () => { | ||
it(`prefixes sitemap`, () => { | ||
assetPrefixMatcher(cy.get(`head link[rel="sitemap"]`)) | ||
}) | ||
}) | ||
|
||
describe(`gatsby-plugin-feed`, () => { | ||
it(`prefixes RSS feed`, () => { | ||
assetPrefixMatcher(cy.get(`head link[type="application/rss+xml"]`)) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// *********************************************************** | ||
// This example plugins/index.js can be used to load plugins | ||
// | ||
// You can change the location of this file or turn off loading | ||
// the plugins file with the 'pluginsFile' configuration option. | ||
// | ||
// You can read more here: | ||
// https://on.cypress.io/plugins-guide | ||
// *********************************************************** | ||
|
||
// This function is called when a project is opened or re-opened (e.g. due to | ||
// the project's config changing) | ||
|
||
module.exports = (on, config) => { | ||
// `on` is used to hook into various events Cypress emits | ||
// `config` is the resolved Cypress config | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 for this example.