feat(gatsby): add assetPrefix to support deploying assets separate from html#12128
feat(gatsby): add assetPrefix to support deploying assets separate from html#12128DSchau merged 70 commits intogatsbyjs:masterfrom
Conversation
| } | ||
| return next() | ||
| }) | ||
| app.use(function(req, res, next) { |
There was a problem hiding this comment.
Could probably use cors here 🤷♂️
| @@ -1,10 +1,18 @@ | |||
| const Joi = require(`joi`) | |||
|
|
|||
| const stripTrailingSlash = chain => chain.replace(/(\w)\//, `$1`) | |||
There was a problem hiding this comment.
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.
Note: this should've been fine
|
Done in ad04a43 |
|
Almost done with the e2e tests. One test is failing for an unknown reason, so I need to fix/figure out why! |
|
Alrighty--should have finished up the last, outstanding bug and test failure! Will get this merged and shipped... today! |
|
Published as gatsby@2.4.0. I bumped the minor version for all packages in this PR, as well.
Soon enough, the documentation will be available at /docs/asset-prefix/ (waiting for the build to complete), but you can get a sneak peek here Thanks everyone for all the help testing this and getting it 🚢'd! ❤️ |
|
Thanks @DSchau and all those who pitched in to make this happen. This feature has saved our team a lot of headaches. 🙌 |
|
Thanks @DSchau - Fantastic work. |
|
My version is gatsby@2.3.4, then I got an error: It make me confused, but fixed in gatsby@2.4.0. |
|
@DSchau Huuuge thanks for releasing this feature! I found one small issue when using relative path. I'm using For now I fixed it with the following local plugin: // plugins/gatsby-remark-rewrite-links/index.js
const visit = require('unist-util-visit');
// pathPrefix has value of assetPrefix here
module.exports = ({ markdownAST, pathPrefix }) => {
visit(markdownAST, 'link', (node) => {
if (node.url.startsWith(pathPrefix)) {
node.url = node.url.replace(pathPrefix, '');
}
});
return markdownAST;
};which I then pass to {
resolve: 'gatsby-transformer-remark',
options: {
plugins: [
'gatsby-remark-rewrite-links',
...
]
}
} |
|
Hi @DSchau. We moved to version 2.4.2, and we are getting the following exceptions on build: You've mentioned in #12128 (comment) that the I can make it work if I install the package but I was just wondering if this has been an oversight due to the test repo using the resolutions feature, it could have slept through without noticing. Is anyone else experiencing this problem? |
Description
If at first you don't succeed, try, try again 😅
The first attempt at asset prefixing went OK, but I think there was a fair enough amount of complexity that trying something new and hopefully simpler is valuable. Shout out to @pieh for the simpler idea here.
This PR introduces the ability to add
assetPrefixto your gatsby-config.js. This should be a URL, and it is on the user of this functionality to ensure that assets are copied appropriately to this destination. It works withpathPrefix, as well, but again, slightly manual--for now!As someone using this feature, here's how you would use it:
assetPrefixoption to yourgatsby-config.jsLink,withPrefix, etc. (no code or plugin changes should be required)__PATH_PREFIX__will be the joined result ofassetPrefixandpathPrefixAlright, alright. Now let's build this.
At build time, you will:
gatsby build --prefix-paths(just like you did withpathPrefix) and this will be respected whether using either of pathPrefix or assetPrefixpathPrefix, you'll need to copy the files in public to a nested directory (e.g. if using /blog, will need to create move /public/ to /public/blog and deploy with your hosting provider of choice)publicdirectory is assets (HTML, CSS, JS, etc.) so this can be uploaded to wherever, e.g. a CDNThis will create a version of your site that has its assets (CSS, JS, images, etc.) prefixed with
${assetPrefix}${pathPrefix || ''}, so those assets will need to be deployed and available. If so--this works just fine!See gatsby-asset-prefix.netlify.com and the Github Repo for an example of this feature in use.
To do
Related Issues
Fixes #2335, Fixes #8833 (need to validate this one--but pretty sure it does!)