|
| 1 | +import type { ServerResponse } from 'node:http' |
1 | 2 | import type { HtmlTagDescriptor, UserConfig, UserConfig as ViteUserConfig } from 'vite' |
2 | 3 | import type { BrowserCommand, BrowserProviderOption, BrowserServerContribution, BrowserServerFactory, PluginHarness, ResolvedConfig } from 'vitest/node' |
3 | 4 | import { createReadStream, readFileSync } from 'node:fs' |
@@ -36,6 +37,18 @@ export { assertBrowserApiWrite, assertBrowserFileAccess, parseKeyDef, resolveScr |
36 | 37 |
|
37 | 38 | const versionRegexp = /(?:\?|&)v=\w{8}/ |
38 | 39 |
|
| 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 | + |
39 | 52 | /** |
40 | 53 | * The browser provider's `serverFactory`. Returns a `BrowserServerContribution` |
41 | 54 | * that Vitest core uses to build the SINGLE Vite server shared by `project.vite` |
@@ -219,20 +232,28 @@ body { |
219 | 232 | ) |
220 | 233 | } |
221 | 234 |
|
| 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 | + |
222 | 244 | 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') |
236 | 257 | } |
237 | 258 | } |
238 | 259 | next() |
|
0 commit comments