Skip to content

Commit

Permalink
fix(browser): support vite config server.headers (#4890)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Jan 8, 2024
1 parent aade782 commit 55f5349
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
7 changes: 7 additions & 0 deletions packages/browser/src/node/index.ts
Expand Up @@ -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]!)
}
},
}),
)
},
Expand Down
4 changes: 2 additions & 2 deletions test/browser/specs/runner.test.mjs
Expand Up @@ -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')
Expand Down
7 changes: 7 additions & 0 deletions test/browser/test/server-headers.test.ts
@@ -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')
})
5 changes: 5 additions & 0 deletions test/browser/vitest.config.mts
Expand Up @@ -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'],
},
Expand Down

0 comments on commit 55f5349

Please sign in to comment.