Unverified Commit 1a53ca74 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Add x-powered-by (#7029)

* Add x-powered-by

* Remove ampEnabled type
parent e4ceb903
...@@ -21,7 +21,5 @@ app.prepare() ...@@ -21,7 +21,5 @@ app.prepare()
handle(req, res, parsedUrl) handle(req, res, parsedUrl)
} }
}) })
.listen(port, () => { .listen(port)
console.log(`> Ready on http://localhost:${port}`)
})
}) })
...@@ -16,6 +16,7 @@ const defaultConfig = { ...@@ -16,6 +16,7 @@ const defaultConfig = {
generateEtags: true, generateEtags: true,
pageExtensions: ['jsx', 'js'], pageExtensions: ['jsx', 'js'],
target: 'server', target: 'server',
poweredByHeader: true,
onDemandEntries: { onDemandEntries: {
maxInactiveAge: 60 * 1000, maxInactiveAge: 60 * 1000,
pagesBufferLength: 2 pagesBufferLength: 2
......
...@@ -28,8 +28,6 @@ type ServerConstructor = { ...@@ -28,8 +28,6 @@ type ServerConstructor = {
conf?: NextConfig, conf?: NextConfig,
} }
const ENDING_IN_JSON_REGEX = /\.json$/
export default class Server { export default class Server {
dir: string dir: string
quiet: boolean quiet: boolean
...@@ -37,6 +35,7 @@ export default class Server { ...@@ -37,6 +35,7 @@ export default class Server {
distDir: string distDir: string
buildId: string buildId: string
renderOpts: { renderOpts: {
poweredByHeader: boolean
noDirtyAmp: boolean noDirtyAmp: boolean
ampBindInitData: boolean ampBindInitData: boolean
staticMarkup: boolean staticMarkup: boolean
...@@ -78,6 +77,7 @@ export default class Server { ...@@ -78,6 +77,7 @@ export default class Server {
this.renderOpts = { this.renderOpts = {
noDirtyAmp: this.nextConfig.experimental.noDirtyAmp, noDirtyAmp: this.nextConfig.experimental.noDirtyAmp,
ampBindInitData: this.nextConfig.experimental.ampBindInitData, ampBindInitData: this.nextConfig.experimental.ampBindInitData,
poweredByHeader: this.nextConfig.poweredByHeader,
staticMarkup, staticMarkup,
buildId: this.buildId, buildId: this.buildId,
generateEtags, generateEtags,
...@@ -248,8 +248,8 @@ export default class Server { ...@@ -248,8 +248,8 @@ export default class Server {
res: ServerResponse, res: ServerResponse,
html: string, html: string,
) { ) {
const { generateEtags } = this.renderOpts const { generateEtags, poweredByHeader } = this.renderOpts
return sendHTML(req, res, html, { generateEtags }) return sendHTML(req, res, html, { generateEtags, poweredByHeader })
} }
public async render( public async render(
......
...@@ -3,10 +3,14 @@ import generateETag from 'etag' ...@@ -3,10 +3,14 @@ import generateETag from 'etag'
import fresh from 'fresh' import fresh from 'fresh'
import { isResSent } from '../lib/utils' import { isResSent } from '../lib/utils'
export function sendHTML(req: IncomingMessage, res: ServerResponse, html: string, { generateEtags }: {generateEtags: boolean}) { export function sendHTML(req: IncomingMessage, res: ServerResponse, html: string, { generateEtags, poweredByHeader }: {generateEtags: boolean, poweredByHeader: boolean}) {
if (isResSent(res)) return if (isResSent(res)) return
const etag = generateEtags ? generateETag(html) : undefined const etag = generateEtags ? generateETag(html) : undefined
if (poweredByHeader) {
res.setHeader('X-Powered-By', 'Next.js')
}
if (fresh(req.headers, { etag })) { if (fresh(req.headers, { etag })) {
res.statusCode = 304 res.statusCode = 304
res.end() res.end()
......
...@@ -7,6 +7,7 @@ module.exports = withCSS(withSass({ ...@@ -7,6 +7,7 @@ module.exports = withCSS(withSass({
// Make sure entries are not getting disposed. // Make sure entries are not getting disposed.
maxInactiveAge: 1000 * 60 * 60 maxInactiveAge: 1000 * 60 * 60
}, },
poweredByHeader: false,
cssModules: true, cssModules: true,
serverRuntimeConfig: { serverRuntimeConfig: {
mySecret: 'secret' mySecret: 'secret'
......
...@@ -8,6 +8,7 @@ import { ...@@ -8,6 +8,7 @@ import {
launchApp, launchApp,
killApp killApp
} from 'next-test-utils' } from 'next-test-utils'
import fetch from 'node-fetch'
// test suits // test suits
import rendering from './rendering' import rendering from './rendering'
...@@ -29,6 +30,13 @@ describe('Configuration', () => { ...@@ -29,6 +30,13 @@ describe('Configuration', () => {
renderViaHTTP(context.appPort, '/module-only-component') renderViaHTTP(context.appPort, '/module-only-component')
]) ])
}) })
it('should disable X-Powered-By header support', async () => {
const url = `http://localhost:${context.appPort}/`
const header = (await fetch(url)).headers.get('X-Powered-By')
expect(header).not.toBe('Next.js')
})
afterAll(() => { afterAll(() => {
killApp(context.server) killApp(context.server)
}) })
......
...@@ -55,6 +55,13 @@ describe('Production Usage', () => { ...@@ -55,6 +55,13 @@ describe('Production Usage', () => {
expect(res2.status).toBe(304) expect(res2.status).toBe(304)
}) })
it('should have X-Powered-By header support', async () => {
const url = `http://localhost:${appPort}/`
const header = (await fetch(url)).headers.get('X-Powered-By')
expect(header).toBe('Next.js')
})
it('should render 404 for routes that do not exist', async () => { it('should render 404 for routes that do not exist', async () => {
const url = `http://localhost:${appPort}/abcdefghijklmno` const url = `http://localhost:${appPort}/abcdefghijklmno`
const res = await fetch(url) const res = await fetch(url)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment