fix(browser): support vite config server.headers (#4890)

This commit is contained in:
Hiroshi Ogawa 2024-01-08 19:31:31 +09:00 committed by GitHub
parent aade7826c5
commit 55f5349fa8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 2 deletions

View File

@ -28,6 +28,13 @@ export default (project: WorkspaceProject, base = '/'): Plugin[] => {
sirv(resolve(distRoot, 'client'), {
single: false,
dev: true,
setHeaders(res, _pathname, _stats) {
const headers = server.config.server.headers
if (headers) {
for (const name in headers)
res.setHeader(name, headers[name]!)
}
},
}),
)
},

View File

@ -11,8 +11,8 @@ const {
} = await runVitest()
await test('tests are actually running', async () => {
assert.ok(browserResultJson.testResults.length === 10, 'Not all the tests have been run')
assert.ok(passedTests.length === 8, 'Some tests failed')
assert.ok(browserResultJson.testResults.length === 11, 'Not all the tests have been run')
assert.ok(passedTests.length === 9, 'Some tests failed')
assert.ok(failedTests.length === 2, 'Some tests have passed but should fail')
assert.doesNotMatch(stderr, /Unhandled Error/, 'doesn\'t have any unhandled errors')

View File

@ -0,0 +1,7 @@
import { expect, it } from 'vitest'
it('server.headers', async () => {
const res = await fetch('/')
expect(res.ok)
expect(res.headers.get('x-custom')).toBe('hello')
})

View File

@ -7,6 +7,11 @@ const dir = dirname(fileURLToPath(import.meta.url))
function noop() {}
export default defineConfig({
server: {
headers: {
'x-custom': 'hello',
},
},
optimizeDeps: {
include: ['@vitest/cjs-lib'],
},