Skip to content
Permalink
Browse files
fix(preview): send configured headers (#9976)
  • Loading branch information
bluwy committed Sep 5, 2022
1 parent 44dbcbe commit 0d20eae2236231aebdfe5cf8fc1794226873d779
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
@@ -75,3 +75,9 @@ Uses [`http-proxy`](https://github.com/http-party/node-http-proxy). Full options
- **Default:** [`server.cors`](./server-options#server-cors)

Configure CORS for the preview server. This is enabled by default and allows any origin. Pass an [options object](https://github.com/expressjs/cors) to fine tune the behavior or `false` to disable.

## preview.headers

- **Type:** `OutgoingHttpHeaders`

Specify server response headers.
@@ -111,12 +111,20 @@ export async function preview(

// static assets
const distDir = path.resolve(config.root, config.build.outDir)
const headers = config.preview.headers
app.use(
previewBase,
sirv(distDir, {
etag: true,
dev: true,
single: config.appType === 'spa'
single: config.appType === 'spa',
setHeaders(res) {
if (headers) {
for (const name in headers) {
res.setHeader(name, headers[name]!)
}
}
}
})
)

@@ -1,3 +1,4 @@
import fetch from 'node-fetch'
import { beforeAll, describe, expect, test } from 'vitest'
import testJSON from '../safe.json'
import { isServe, page, viteTestUrl } from '~utils'
@@ -97,3 +98,11 @@ describe.runIf(isServe)('main', () => {
expect(await page.textContent('.unsafe-dotenv')).toBe('404')
})
})

describe('fetch', () => {
// Note: this should pass in build too, but the test setup doesn't use Vite preview
test.runIf(isServe)('serve with configured headers', async () => {
const res = await fetch(viteTestUrl + '/src/')
expect(res.headers.get('x-served-by')).toBe('vite')
})
})
@@ -6,6 +6,6 @@
"dev": "vite root",
"build": "vite build root",
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
"preview": "vite preview"
"preview": "vite preview root"
}
}
@@ -18,6 +18,14 @@ module.exports = {
},
hmr: {
overlay: false
},
headers: {
'x-served-by': 'vite'
}
},
preview: {
headers: {
'x-served-by': 'vite'
}
},
define: {

0 comments on commit 0d20eae

Please sign in to comment.