Skip to content

Commit 1a53ca7

Browse files
authored
Add x-powered-by (vercel#7029)
* Add x-powered-by * Remove ampEnabled type
1 parent e4ceb90 commit 1a53ca7

File tree

7 files changed

+27
-8
lines changed

7 files changed

+27
-8
lines changed

examples/custom-server-typescript/server/index.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,5 @@ app.prepare()
2121
handle(req, res, parsedUrl)
2222
}
2323
})
24-
.listen(port, () => {
25-
console.log(`> Ready on http://localhost:${port}`)
26-
})
24+
.listen(port)
2725
})

packages/next-server/server/config.js

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const defaultConfig = {
1616
generateEtags: true,
1717
pageExtensions: ['jsx', 'js'],
1818
target: 'server',
19+
poweredByHeader: true,
1920
onDemandEntries: {
2021
maxInactiveAge: 60 * 1000,
2122
pagesBufferLength: 2

packages/next-server/server/next-server.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,14 @@ type ServerConstructor = {
2828
conf?: NextConfig,
2929
}
3030

31-
const ENDING_IN_JSON_REGEX = /\.json$/
32-
3331
export default class Server {
3432
dir: string
3533
quiet: boolean
3634
nextConfig: NextConfig
3735
distDir: string
3836
buildId: string
3937
renderOpts: {
38+
poweredByHeader: boolean
4039
noDirtyAmp: boolean
4140
ampBindInitData: boolean
4241
staticMarkup: boolean
@@ -78,6 +77,7 @@ export default class Server {
7877
this.renderOpts = {
7978
noDirtyAmp: this.nextConfig.experimental.noDirtyAmp,
8079
ampBindInitData: this.nextConfig.experimental.ampBindInitData,
80+
poweredByHeader: this.nextConfig.poweredByHeader,
8181
staticMarkup,
8282
buildId: this.buildId,
8383
generateEtags,
@@ -248,8 +248,8 @@ export default class Server {
248248
res: ServerResponse,
249249
html: string,
250250
) {
251-
const { generateEtags } = this.renderOpts
252-
return sendHTML(req, res, html, { generateEtags })
251+
const { generateEtags, poweredByHeader } = this.renderOpts
252+
return sendHTML(req, res, html, { generateEtags, poweredByHeader })
253253
}
254254

255255
public async render(

packages/next-server/server/send-html.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ import generateETag from 'etag'
33
import fresh from 'fresh'
44
import { isResSent } from '../lib/utils'
55

6-
export function sendHTML(req: IncomingMessage, res: ServerResponse, html: string, { generateEtags }: {generateEtags: boolean}) {
6+
export function sendHTML(req: IncomingMessage, res: ServerResponse, html: string, { generateEtags, poweredByHeader }: {generateEtags: boolean, poweredByHeader: boolean}) {
77
if (isResSent(res)) return
88
const etag = generateEtags ? generateETag(html) : undefined
99

10+
if (poweredByHeader) {
11+
res.setHeader('X-Powered-By', 'Next.js')
12+
}
13+
1014
if (fresh(req.headers, { etag })) {
1115
res.statusCode = 304
1216
res.end()

test/integration/config/next.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module.exports = withCSS(withSass({
77
// Make sure entries are not getting disposed.
88
maxInactiveAge: 1000 * 60 * 60
99
},
10+
poweredByHeader: false,
1011
cssModules: true,
1112
serverRuntimeConfig: {
1213
mySecret: 'secret'

test/integration/config/test/index.test.js

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
launchApp,
99
killApp
1010
} from 'next-test-utils'
11+
import fetch from 'node-fetch'
1112

1213
// test suits
1314
import rendering from './rendering'
@@ -29,6 +30,13 @@ describe('Configuration', () => {
2930
renderViaHTTP(context.appPort, '/module-only-component')
3031
])
3132
})
33+
34+
it('should disable X-Powered-By header support', async () => {
35+
const url = `http://localhost:${context.appPort}/`
36+
const header = (await fetch(url)).headers.get('X-Powered-By')
37+
expect(header).not.toBe('Next.js')
38+
})
39+
3240
afterAll(() => {
3341
killApp(context.server)
3442
})

test/integration/production/test/index.test.js

+7
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ describe('Production Usage', () => {
5555
expect(res2.status).toBe(304)
5656
})
5757

58+
it('should have X-Powered-By header support', async () => {
59+
const url = `http://localhost:${appPort}/`
60+
const header = (await fetch(url)).headers.get('X-Powered-By')
61+
62+
expect(header).toBe('Next.js')
63+
})
64+
5865
it('should render 404 for routes that do not exist', async () => {
5966
const url = `http://localhost:${appPort}/abcdefghijklmno`
6067
const res = await fetch(url)

0 commit comments

Comments
 (0)