Skip to content

Think gatsby core should fulfill webpack with some cache aspects #291

@cusspvz

Description

@cusspvz

@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 (older build) environment. Example: ${__dirname}/public/assets/${hash}/

Got some issues since build-css and build-html (old static) weren't using the newly hash folder, so I've set ASSETS_BASE as a bash environment generated on every npm run build on our project and set the same publicPath for them but only set the output.path for the build-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

  if ( env === 'build-css' || env === 'build-html' || env === 'build-javascript' ) {
    config.merge({
      output: {
        publicPath: `http://${BUILD_DOMAIN}/${ASSETS_BASE}/`
      }
    })
  }

  if ( env === 'build-javascript' ) {
    config.merge({
      output: {
        path: `${__dirname}/public/${ASSETS_BASE}/`,
      }
    })
  }

package.json - scripts

    "build": "export ASSETS_BASE=assets/$(date | md5sum | cut -c -10); for x in clean gatsby sync symlinks minify-images scramble-js; do npm run build-$x || exit 1; done;",
    "build-clean": "if [ -d public/ ]; then rm -fR public/; fi;",
    "build-gatsby": "gatsby build",
    "build-sync": "cd public; for file in $(ls -p | grep -v / | grep -v .html | grep -v .js); do cp $file ./$ASSETS_BASE/$file; done;",
    "build-symlinks": "cd public; for file in $(ls ./$ASSETS_BASE/*); do basefile=./$(basename $file); [ -f $basefile ] && rm $basefile; echo \"Symlinking $basefile from root to assets.\"; ln -s $file $basefile; done;",

Now that the story is told and you've somehow understood my point of view, here's my proposal:

  • Automatically generate a sitemap (know it could be achieved over postBuild but I think it should be implemented on a the core for a better SEO for everyone using this.
  • Addition of public-endpoint configuration on config.toml (Needed for link's base and sitemap generation)
  • Addition of cdn-endpoint configuration on config.toml (This would default to public-endpoint when undefined, needed for assets publicPath but for me it seems more practical and semantic to have both public-endpoint and cdn-endpoint instead of public-path only)
  • Output all assets into a different hashed folder on each non-dev build for caching purposes.
  • Symlink assets dir, assets files or both from the root

Semi-related questions:

  • Does gatsby allows overlapping config.toml configurations over Environment Variables?
  • If not, can I implement it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    stale?Issue that may be closed soon due to the original author not responding any more.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions