-
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
Think gatsby core should fulfill webpack with some cache aspects #291
Comments
I'm working on asset hashes for js/css right now. I'm not sold on a public API yet, but the implementation seems good and working right now. I'm have a lot of restructuring in the build stage to put css and js before html, and ideally in separate processes before this is ready for merge. I have a strong feeling the callback hell might become a problem in Would the following implementation of the A quick summary of how it works: Assets get hashed and stored in the main node process in an 'Asset Manager' after webpack returns the stats object from compilation. Webpack config requests the assets as json from the Asset Manager and injects them into the build-html stage of the build process. An example of this json by named chunk looks like: {
gatsby: [
'gatsby-ebd1d09457bbf6196c377b93ebaf8274.css',
'gatsby-8b4fed66cae0a2499e30.js'
]
} Then the user would request the assets like: import { assets } from 'gatsby-helpers'
render() {
return (
<html>
<head>
{/* defaults to an inline style tag */}
{assets.css()}
{/* link rel=stylesheet tag */}
{assets.css({ inline: false })}
{/* link rel=stylesheet tag of a custom named chunk */}
{assets.css({ inline: false, chunk: 'vendor' })}
</head>
<body>
{/* script tag with hash for production, no hash for development */}
{assets.js()}
{/* script tag of named custom chunk */}
{assets.js({ chunk: 'vendor' })}
</body>
</html>
)
} Basically all of the if process.env.NODE_ENV and prefixLinks stuff that goes on in the starters' |
@benstepp thanks for commenting this!
I've solved already my cache problems, webpack has mechanisms to apply hashes to invalidate caches but they won't work since there are 3 different build stages instead of one. My solution was to create an hash solution based on an Environment Variable, my proposal is to insert a mechanism on the core that generates an hash on each
Thanks for explaining me but I know how the webpack assets plugin works. Webpack generates an hash per build which would generate three different paths on one gatsby build, and thats why I've used an external environment variable to hash the paths and code instead of using As the hash is injected on the code via Webpack's Define, I have access to the path on the Although I've liked your API, but for example I have my head as a string because I need to inject comments for IE, so I think it should be also capable to return the path only. Gonna rewrite the resume of my proposal: These are the changes I'm proposing to apply to gatsby:
|
As things can get pretty complex while explaining, gonna apply here things proposed over this branch.
@benstepp I've created a branch to apply things I've proposed over here. Want to discuss the |
This was the problem I was aiming on solving. Basically we know the only place we need the hashed assets are in the I would prefer to use the webpack built in
This is the same solution I came to, but I opted to build the css and js before the html. Ideally we would run the css and js at the same time in separate processes, and that is one change I would like to tackle before merging asset hashes.
My proposed solution would have all assets available via the Does Gatsby plan on supporting non-react {assets.css({ inline: false }).toComponent()} const html = `
<html>
<head>
${assets.css({ inline: false }).toString()}
</head>
<body>
${assets.js().toString()}
</body>
</html>
` |
This issue has been automatically closed due to inactivity. |
@KyleAMathews we already talked on a PR about the pluggable API system, I'm waiting anxiously for it because I got some ideas that could really push up gatsby fowards.
Until that happens, I came across an issue relative with cache (I'm setting up a large cache for our assets).
Webpack has the feature of prepending an hash to bypass browser cache (suffixing a query after the script's public path its a bad idea it doesn't affect some proxies).
I came across a nice solution for having cache working flawless on gatsby, think this could be part of its core.
First I've tried to assemble a different build path on each build just for the
build-javascript
(olderbuild
) environment. Example:${__dirname}/public/assets/${hash}/
Got some issues since
build-css
andbuild-html
(oldstatic
) weren't using the newly hash folder, so I've setASSETS_BASE
as a bash environment generated on everynpm run build
on our project and set the samepublicPath
for them but only set theoutput.path
for thebuild-javascript
.Unfortunately some of the requests were being using correctly the assets folder and others not because of the CDN usage vs our staging environment.
I've written a few of bash files and told nginx to use non-caching redirects in case it had an asset request from the root folder, just in case, and thinks worked out.
Here's our current config regarding those changes:
gatsby-node.js
package.json - scripts
Now that the story is told and you've somehow understood my point of view, here's my proposal:
postBuild
but I think it should be implemented on a the core for a better SEO for everyone using this.public-endpoint
configuration onconfig.toml
(Needed for link's base and sitemap generation)cdn-endpoint
configuration onconfig.toml
(This would default topublic-endpoint
whenundefined
, needed for assetspublicPath
but for me it seems more practical and semantic to have bothpublic-endpoint
andcdn-endpoint
instead ofpublic-path
only)Semi-related questions:
gatsby
allows overlappingconfig.toml
configurations over Environment Variables?The text was updated successfully, but these errors were encountered: