feat(scripts/dev): add ETag matching
This commit is contained in:
parent
e6e3acb818
commit
c4f4d0b7fb
1 changed files with 25 additions and 2 deletions
|
|
@ -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.')
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue