Skip to content

Commit

Permalink
Implement naive cache-busting
Browse files Browse the repository at this point in the history
  • Loading branch information
hubol committed Dec 7, 2023
1 parent cbc1e86 commit c4fa77f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-and-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- uses: actions/upload-artifact@v2
with:
name: build
path: public
path: dist

deploy-heroku:
needs: build
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
!.vscode/**/*.default
!.vscode/**/README.md

dist/

public/**/*
!public/index.html
!public/index.css
Expand Down
32 changes: 27 additions & 5 deletions esbuild.mjs
Original file line number Diff line number Diff line change
@@ -1,31 +1,53 @@
import { build, context } from 'esbuild';
import { copyFile, readFile, writeFile } from 'fs/promises';

const serve = process.argv[2] === 'serve';

function options(define = {}) {
/**
* @param {import('esbuild').BuildOptions} overrides
* @returns {import('esbuild').BuildOptions}
*/
function options(overrides) {
/** @type {import('esbuild').BuildOptions} */
const options = {
entryPoints: ['src/index.ts'],
outdir: 'public',
assetNames: 'assets/[name]-[hash]',
define,
bundle: true,
sourcemap: true,
loader: {
'.png': 'file',
'.zip': 'file',
},
logLevel: 'info',
...overrides,
}

return options;
}

if (!serve) {
await build(options({ IS_PRODUCTION: 'true' }));
// Naive cache busting
const signature = Date.now();

await build(options({
outdir: 'dist',
assetNames: `assets/[name]-${signature}`,
entryNames: `[name]-${signature}`,
define: { IS_PRODUCTION: 'true' }
}));

const indexHtmlSource = (await readFile('public/index.html', 'utf8'))
.replace(/index.css/gm, `index-${signature}.css`)
.replace(/index.js/gm, `index-${signature}.js`);

await writeFile('dist/index.html', indexHtmlSource);
await copyFile('public/index.css', `dist/index-${signature}.css`);
}
else {
const ctx = await context(options({ IS_PRODUCTION: 'false' }));
const ctx = await context(options({
outdir: 'public',
define: { IS_PRODUCTION: 'false' }
}));

await ctx.watch();
await ctx.serve({ servedir: 'public' });
Expand Down

0 comments on commit c4fa77f

Please sign in to comment.