Skip to content

Commit

Permalink
feat(server): support headers configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
stygian-desolator committed Nov 8, 2021
1 parent a473570 commit a7210a0
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 14 deletions.
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 @@ -172,6 +172,9 @@ describe('preview config', () => {
host: true,
open: true,
https: true,
headers: {
'Cache-Control': 'no-store'
},
proxy: { '/foo': 'http://localhost:4567' },
cors: false
})
Expand Down
8 changes: 6 additions & 2 deletions packages/vite/src/node/http.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs, { promises as fsp } from 'fs'
import path from 'path'
import { Server as HttpServer } from 'http'
import { OutgoingHttpHeaders as HttpServerHeaders, Server as HttpServer } from 'http'
import { ServerOptions as HttpsServerOptions } from 'https'
import { isObject } from './utils'
import { ProxyOptions } from './server/middlewares/proxy'
Expand Down Expand Up @@ -61,7 +61,11 @@ export interface CommonServerOptions {
* Set to `true` to allow all methods from any origin, or configure separately
* using an object.
*/
cors?: CorsOptions | boolean
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 @@ -67,6 +67,7 @@ export type {
} from './plugins/resolve'
export type { WebSocketServer } from './server/ws'
export type { PluginContainer } from './server/pluginContainer'
export type { SendOptions } from './server/send';
export type { ModuleGraph, ModuleNode } from './server/moduleGraph'
export type { ProxyOptions } from './server/middlewares/proxy'
export type {
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 @@ -36,7 +36,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
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/middlewares/indexHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ 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
13 changes: 8 additions & 5 deletions packages/vite/src/node/server/middlewares/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ 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 @@ -180,10 +180,13 @@ export function transformMiddleware(
res,
result.code,
type,
result.etag,
// allow browser to cache npm deps!
isDep ? 'max-age=31536000,immutable' : 'no-cache',
result.map
{
etag: result.etag,
// allow browser to cache npm deps!
cacheControl: isDep ? 'max-age=31536000,immutable' : 'no-cache',
headers: server.config.server.headers,
map: result.map
}
)
}
}
Expand Down
21 changes: 17 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 { IncomingMessage, ServerResponse } from 'http'
import { IncomingMessage, OutgoingHttpHeaders, ServerResponse } from 'http'
import getEtag from 'etag'
import { SourceMap } from 'rollup'

Expand All @@ -11,15 +11,22 @@ 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 +40,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

0 comments on commit a7210a0

Please sign in to comment.