From c4f4d0b7fbcb1872b936eecea8f0d24eba39b97a Mon Sep 17 00:00:00 2001 From: PalmDevs Date: Sat, 18 Oct 2025 19:18:29 +0700 Subject: [PATCH] feat(scripts/dev): add ETag matching --- scripts/dev.ts | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/scripts/dev.ts b/scripts/dev.ts index f27a5fe..3e7ad1e 100644 --- a/scripts/dev.ts +++ b/scripts/dev.ts @@ -55,8 +55,31 @@ const server = Bun.serve({ ) const file = Bun.file('./dist/revenge.bundle') - if (await file.exists()) return new Response(file) - else throw new Error('Could not serve the bundle! No file found.') + + if (await file.exists()) { + const hash = Bun.hash + .crc32(await file.arrayBuffer()) + .toString(16) + + if (req.headers.get('If-None-Match') === hash) { + console.debug( + chalk.gray( + '\u{1F4BE} ETag matched, responding with 304', + ), + ) + + return new Response(null, { + status: 304, + }) + } + + return new Response(file, { + status: 200, + headers: { + ETag: hash, + }, + }) + } else throw new Error('Could not serve the bundle! No file found.') } catch (e) { console.error(e) throw new Error('Build failed. Check console for details.')