Skip to content

Commit 7af8755

Browse files
authored
perf(browser): serve framework assets as immutable (#10729)
1 parent 1216b5a commit 7af8755

1 file changed

Lines changed: 34 additions & 13 deletions

File tree

‎packages/browser/src/node/index.ts‎

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { ServerResponse } from 'node:http'
12
import type { HtmlTagDescriptor, UserConfig, UserConfig as ViteUserConfig } from 'vite'
23
import type { BrowserCommand, BrowserProviderOption, BrowserServerContribution, BrowserServerFactory, PluginHarness, ResolvedConfig } from 'vitest/node'
34
import { createReadStream, readFileSync } from 'node:fs'
@@ -36,6 +37,18 @@ export { assertBrowserApiWrite, assertBrowserFileAccess, parseKeyDef, resolveScr
3637

3738
const versionRegexp = /(?:\?|&)v=\w{8}/
3839

40+
// pin a Cache-Control value and stop Vite's later middleware from overriding it
41+
function pinCacheControl(res: ServerResponse, value: string): void {
42+
res.setHeader('Cache-Control', value)
43+
const setHeader = res.setHeader.bind(res)
44+
res.setHeader = function (name, ...args) {
45+
if (name === 'Cache-Control') {
46+
return res
47+
}
48+
return (setHeader as (...a: unknown[]) => ServerResponse)(name, ...args)
49+
}
50+
}
51+
3952
/**
4053
* The browser provider's `serverFactory`. Returns a `BrowserServerContribution`
4154
* that Vitest core uses to build the SINGLE Vite server shared by `project.vite`
@@ -219,20 +232,28 @@ body {
219232
)
220233
}
221234

235+
// vitest's own dist files only change with the vitest version and
236+
// browser contexts don't outlive the process, so there is no reason
237+
// to revalidate them in every tester iframe. Skipped for persistent
238+
// contexts and preview providers — their disk cache would survive a vitest upgrade
239+
const persistentContext = (parentServer.config.browser.provider?.options as { persistentContext?: unknown } | undefined)?.persistentContext
240+
const immutablePrefixes = persistentContext || parentServer.config.browser.provider?.name === 'preview'
241+
? []
242+
: ['/__vitest_browser__/', `/@fs${vitestDist}`, `/@fs${distRoot}`]
243+
222244
server.middlewares.use((req, res, next) => {
223-
// 9000 mega head move
224-
// Vite always caches optimized dependencies, but users might mock
225-
// them in _some_ tests, while keeping original modules in others
226-
// there is no way to configure that in Vite, so we patch it here
227-
// to always ignore the cache-control set by Vite in the next middleware
228-
if (req.url && versionRegexp.test(req.url) && !req.url.includes('chunk-')) {
229-
res.setHeader('Cache-Control', 'no-cache')
230-
const setHeader = res.setHeader.bind(res)
231-
res.setHeader = function (name, value) {
232-
if (name === 'Cache-Control') {
233-
return res
234-
}
235-
return setHeader(name, value)
245+
const url = req.url
246+
if (url) {
247+
if (immutablePrefixes.some(prefix => url.startsWith(prefix))) {
248+
pinCacheControl(res, 'public,max-age=31536000,immutable')
249+
}
250+
// 9000 mega head move
251+
// Vite always caches optimized dependencies, but users might mock
252+
// them in _some_ tests, while keeping original modules in others;
253+
// there is no way to configure that in Vite, so we pin no-cache
254+
// and ignore the cache-control Vite sets in the next middleware
255+
else if (versionRegexp.test(url) && !url.includes('chunk-')) {
256+
pinCacheControl(res, 'no-cache')
236257
}
237258
}
238259
next()

0 commit comments

Comments
 (0)