Skip to content

Instantly share code, notes, and snippets.

@joeypy
Created April 18, 2022 21:35
Show Gist options
  • Save joeypy/d65c462f79b47d24acf96f79a32fa889 to your computer and use it in GitHub Desktop.
Save joeypy/d65c462f79b47d24acf96f79a32fa889 to your computer and use it in GitHub Desktop.
NextJS export cache issue fix. Update cache on every build
/* the trick is to build unique build ID on every build and add it to static files on build time.
* This way the name and URL of static files will change on every build.
*/
/* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path');
// Used to set folders as alias to directly use in nextjs
const nextConfiguration = ({
webpack: (config, { buildId, dev }) => {
const newConfig = config;
if (!dev && newConfig.output.filename.startsWith('static')) {
newConfig.output.filename = newConfig.output.filename.replace('[name]', `[name]-${buildId}`);
newConfig.output.chunkFilename = newConfig.output.chunkFilename.replace('[name]', `[name]-${buildId}`);
}
return newConfig;
},
generateBuildId: async () => {
const timestamp = Math.floor(Date.now() / 1000);
return `build-${timestamp}`;
},
});
module.exports = nextConfiguration;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment