Skip to content

Commit b1293f4

Browse files
committed
wip: set etag
1 parent fa00ddd commit b1293f4

File tree

3 files changed

+42
-24
lines changed

3 files changed

+42
-24
lines changed

packages/vite/src/node/server/environments/fullBundleEnvironment.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { RolldownBuild } from 'rolldown'
33
import { type DevEngine, dev } from 'rolldown/experimental'
44
import type { Update } from 'types/hmrPayload'
55
import colors from 'picocolors'
6+
import getEtag from 'etag'
67
import { ChunkMetadataMap, resolveRolldownOptions } from '../../build'
78
import { getHmrImplementation } from '../../plugins/clientInjections'
89
import { DevEnvironment, type DevEnvironmentContext } from '../environment'
@@ -19,17 +20,19 @@ type HmrOutput = Exclude<
1920
undefined
2021
>
2122

23+
type MemoryFile = {
24+
source: string | Uint8Array
25+
etag?: string
26+
}
27+
2228
export class MemoryFiles {
23-
private files = new Map<
24-
string,
25-
string | Uint8Array | (() => string | Uint8Array)
26-
>()
29+
private files = new Map<string, MemoryFile | (() => MemoryFile)>()
2730

2831
get size(): number {
2932
return this.files.size
3033
}
3134

32-
get(file: string): string | Uint8Array | undefined {
35+
get(file: string): MemoryFile | undefined {
3336
const result = this.files.get(file)
3437
if (result === undefined) {
3538
return undefined
@@ -42,10 +45,7 @@ export class MemoryFiles {
4245
return result
4346
}
4447

45-
set(
46-
file: string,
47-
content: string | Uint8Array | (() => string | Uint8Array),
48-
): void {
48+
set(file: string, content: MemoryFile | (() => MemoryFile)): void {
4949
this.files.set(file, content)
5050
}
5151

@@ -265,11 +265,16 @@ export class FullBundleDevEnvironment extends DevEnvironment {
265265
handler: (_, bundle) => {
266266
// NOTE: don't clear memoryFiles here as incremental build re-uses the files
267267
for (const outputFile of Object.values(bundle)) {
268-
this.memoryFiles.set(outputFile.fileName, () =>
269-
outputFile.type === 'chunk'
270-
? outputFile.code
271-
: outputFile.source,
272-
)
268+
this.memoryFiles.set(outputFile.fileName, () => {
269+
const source =
270+
outputFile.type === 'chunk'
271+
? outputFile.code
272+
: outputFile.source
273+
return {
274+
source,
275+
etag: getEtag(Buffer.from(source), { weak: true }),
276+
}
277+
})
273278
}
274279
},
275280
},
@@ -336,9 +341,11 @@ export class FullBundleDevEnvironment extends DevEnvironment {
336341
code: typeof hmrOutput.code === 'string' ? '[code]' : hmrOutput.code,
337342
})
338343

339-
this.memoryFiles.set(hmrOutput.filename, hmrOutput.code)
344+
this.memoryFiles.set(hmrOutput.filename, { source: hmrOutput.code })
340345
if (hmrOutput.sourcemapFilename && hmrOutput.sourcemap) {
341-
this.memoryFiles.set(hmrOutput.sourcemapFilename, hmrOutput.sourcemap)
346+
this.memoryFiles.set(hmrOutput.sourcemapFilename, {
347+
source: hmrOutput.sourcemap,
348+
})
342349
}
343350
const updates: Update[] = hmrOutput.hmrBoundaries.map((boundary: any) => {
344351
return {

packages/vite/src/node/server/middlewares/indexHtml.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,8 @@ export function indexHtmlMiddleware(
462462
const pathname = decodeURIComponent(url)
463463
const filePath = pathname.slice(1) // remove first /
464464

465-
let content = fullBundleEnv.memoryFiles.get(filePath)
466-
if (!content && fullBundleEnv.memoryFiles.size !== 0) {
465+
let file = fullBundleEnv.memoryFiles.get(filePath)
466+
if (!file && fullBundleEnv.memoryFiles.size !== 0) {
467467
return next()
468468
}
469469
const secFetchDest = req.headers['sec-fetch-dest']
@@ -477,20 +477,22 @@ export function indexHtmlMiddleware(
477477
undefined,
478478
].includes(secFetchDest) &&
479479
((await fullBundleEnv.triggerBundleRegenerationIfStale()) ||
480-
content === undefined)
480+
file === undefined)
481481
) {
482-
content = await generateFallbackHtml(server as ViteDevServer)
482+
file = { source: await generateFallbackHtml(server as ViteDevServer) }
483483
}
484-
if (!content) {
484+
if (!file) {
485485
return next()
486486
}
487487

488488
const html =
489-
typeof content === 'string' ? content : Buffer.from(content.buffer)
489+
typeof file.source === 'string'
490+
? file.source
491+
: Buffer.from(file.source)
490492
const headers = isDev
491493
? server.config.server.headers
492494
: server.config.preview.headers
493-
return send(req, res, html, 'html', { headers })
495+
return send(req, res, html, 'html', { headers, etag: file.etag })
494496
}
495497

496498
let filePath: string

packages/vite/src/node/server/middlewares/memoryFiles.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ export function memoryFilesMiddleware(
2727

2828
const file = memoryFiles.get(filePath)
2929
if (file) {
30+
if (file.etag) {
31+
if (req.headers['if-none-match'] === file.etag) {
32+
res.statusCode = 304
33+
res.end()
34+
return
35+
}
36+
res.setHeader('Etag', file.etag)
37+
}
38+
3039
const mime = mrmime.lookup(filePath)
3140
if (mime) {
3241
res.setHeader('Content-Type', mime)
@@ -36,7 +45,7 @@ export function memoryFilesMiddleware(
3645
res.setHeader(name, headers[name]!)
3746
}
3847

39-
return res.end(file)
48+
return res.end(file.source)
4049
}
4150
next()
4251
}

0 commit comments

Comments
 (0)