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

Add proper headers to responses in web server #34723

Merged
merged 5 commits into from Feb 24, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/next/server/next-server.ts
Expand Up @@ -9,6 +9,7 @@ import type { PrerenderManifest } from '../build'
import type { Rewrite } from '../lib/load-custom-routes'
import type { BaseNextRequest, BaseNextResponse } from './base-http'
import type { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'
import type { PayloadOptions } from './send-payload'

import { execOnce } from '../shared/lib/utils'
import {
Expand Down Expand Up @@ -43,7 +44,7 @@ import { route } from './router'
import { run } from './web/sandbox'

import { NodeNextRequest, NodeNextResponse } from './base-http/node'
import { PayloadOptions, sendRenderResult } from './send-payload'
import { sendRenderResult } from './send-payload'
import { getExtension, serveStatic } from './serve-static'
import { ParsedUrlQuery } from 'querystring'
import { apiResolver } from './api-utils/node'
Expand Down Expand Up @@ -584,6 +585,12 @@ export default class NextNodeServer extends BaseServer {

protected streamResponseChunk(res: NodeNextResponse, chunk: any) {
res.originalResponse.write(chunk)

// When both compression and streaming are enabled, we need to explicitly
// flush the response to avoid it being buffered by gzip.
if (this.compression && 'flush' in res.originalResponse) {
;(res.originalResponse as any).flush()
}
}

protected async imageOptimizer(
Expand Down
14 changes: 14 additions & 0 deletions packages/next/server/web-server.ts
Expand Up @@ -148,6 +148,20 @@ export default class NextWebServer extends BaseServer {
options?: PayloadOptions | undefined
}
): Promise<void> {
// Add necessary headers.
// @TODO: Share the isomorphic logic with server/send-payload.ts.
if (options.poweredByHeader && options.type === 'html') {
res.setHeader('X-Powered-By', 'Next.js')
}
if (!res.getHeader('Content-Type')) {
res.setHeader(
'Content-Type',
options.type === 'json'
? 'application/json'
: 'text/html; charset=utf-8'
)
}

// @TODO
const writer = res.transformStream.writable.getWriter()

Expand Down
Expand Up @@ -177,6 +177,12 @@ describe('Edge runtime - dev', () => {
expect(content).toMatchInlineSnapshot('"foo.client"')
})

it('should have content-type and content-encoding headers', async () => {
const res = await fetchViaHTTP(context.appPort, '/')
expect(res.headers.get('content-type')).toBe('text/html; charset=utf-8')
expect(res.headers.get('content-encoding')).toBe('gzip')
})

basic(context, { env: 'dev' })
streaming(context)
rsc(context, { runtime: 'edge', env: 'dev' })
Expand Down