Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): support headers configurable #5580

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/playground/cli/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ const { defineConfig } = require('vite')

module.exports = defineConfig({
server: {
host: 'localhost'
host: 'localhost',
headers: {
'Cache-Control': 'no-store'
}
},
build: {
//speed up build
Expand Down
3 changes: 3 additions & 0 deletions packages/vite/src/node/__tests__/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ describe('preview config', () => {
host: true,
open: true,
https: true,
headers: {
'Cache-Control': 'no-store'
},
proxy: { '/foo': 'http://localhost:4567' },
cors: false
})
Expand Down
9 changes: 8 additions & 1 deletion packages/vite/src/node/http.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import fs, { promises as fsp } from 'fs'
import path from 'path'
import type { Server as HttpServer } from 'http'
import type {
OutgoingHttpHeaders as HttpServerHeaders,
Server as HttpServer
} from 'http'
import type { ServerOptions as HttpsServerOptions } from 'https'
import { isObject } from './utils'
import type { ProxyOptions } from './server/middlewares/proxy'
Expand Down Expand Up @@ -62,6 +65,10 @@ export interface CommonServerOptions {
* using an object.
*/
cors?: CorsOptions | boolean
/**
* Specify server response headers.
*/
headers?: HttpServerHeaders
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export type { ResolveOptions, InternalResolveOptions } from './plugins/resolve'
export type { WebSocketServer } from './server/ws'
export type { PluginContainer } from './server/pluginContainer'
export type { ModuleGraph, ModuleNode, ResolvedUrl } from './server/moduleGraph'
export type { SendOptions } from './server/send'
export type { ProxyOptions } from './server/middlewares/proxy'
export type {
TransformOptions,
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export function resolvePreviewOptions(
https: preview?.https ?? server.https,
open: preview?.open ?? server.open,
proxy: preview?.proxy ?? server.proxy,
cors: preview?.cors ?? server.cors
cors: preview?.cors ?? server.cors,
headers: preview?.headers ?? server.headers
}
}

Expand Down
4 changes: 3 additions & 1 deletion packages/vite/src/node/server/middlewares/indexHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,9 @@ export function indexHtmlMiddleware(
try {
let html = fs.readFileSync(filename, 'utf-8')
html = await server.transformIndexHtml(url, html, req.originalUrl)
return send(req, res, html, 'html')
return send(req, res, html, 'html', {
headers: server.config.server.headers
})
} catch (e) {
return next(e)
}
Expand Down
19 changes: 9 additions & 10 deletions packages/vite/src/node/server/middlewares/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ export function transformMiddleware(
const map = (await moduleGraph.getModuleByUrl(originalUrl))
?.transformResult?.map
if (map) {
return send(req, res, JSON.stringify(map), 'json')
return send(req, res, JSON.stringify(map), 'json', {
headers: server.config.server.headers
})
} else {
return next()
}
Expand Down Expand Up @@ -181,16 +183,13 @@ export function transformMiddleware(
const isDep =
DEP_VERSION_RE.test(url) ||
(cacheDirPrefix && url.startsWith(cacheDirPrefix))
return send(
req,
res,
result.code,
type,
result.etag,
return send(req, res, result.code, type, {
etag: result.etag,
// allow browser to cache npm deps!
isDep ? 'max-age=31536000,immutable' : 'no-cache',
result.map
)
cacheControl: isDep ? 'max-age=31536000,immutable' : 'no-cache',
headers: server.config.server.headers,
map: result.map
})
}
}
} catch (e) {
Expand Down
26 changes: 22 additions & 4 deletions packages/vite/src/node/server/send.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IncomingMessage, ServerResponse } from 'http'
import type { IncomingMessage, OutgoingHttpHeaders, ServerResponse } from 'http'
import getEtag from 'etag'
import type { SourceMap } from 'rollup'

Expand All @@ -11,15 +11,27 @@ const alias: Record<string, string | undefined> = {
json: 'application/json'
}

export interface SendOptions {
etag?: string
cacheControl?: string
headers?: OutgoingHttpHeaders
map?: SourceMap | null
}

export function send(
req: IncomingMessage,
res: ServerResponse,
content: string | Buffer,
type: string,
etag = getEtag(content, { weak: true }),
cacheControl = 'no-cache',
map?: SourceMap | null
options: SendOptions
): void {
const {
etag = getEtag(content, { weak: true }),
cacheControl = 'no-cache',
headers,
map
} = options

if (res.writableEnded) {
return
}
Expand All @@ -33,6 +45,12 @@ export function send(
res.setHeader('Cache-Control', cacheControl)
res.setHeader('Etag', etag)

if (headers) {
for (const name in headers) {
res.setHeader(name, headers[name]!)
}
}

// inject source map reference
if (map && map.mappings) {
if (isDebug) {
Expand Down