From c92bc858f7665b6ea1a2e2d80455e47faf1aefec Mon Sep 17 00:00:00 2001 From: Jerome Fitzgerald Date: Wed, 14 Feb 2018 12:02:48 -0500 Subject: [PATCH] [refactor] poweredByHeader (#3716) (#3807) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts `fb7c862` per @timneutkens - Removes the errors/*.md associated - Puts back `poweredByHeader` for `next.config.js` - Reincorporates test: X-Powered-By header ✓ should set it by default (3ms) ✓ should not set it when poweredByHeader==false (5ms) Also tested with `yarn link` and verified. --- errors/powered-by-header-option-removed.md | 15 -------------- server/config.js | 4 +--- server/index.js | 4 +++- .../integration/production/test/index.test.js | 20 +++++++++++++++++++ 4 files changed, 24 insertions(+), 19 deletions(-) delete mode 100644 errors/powered-by-header-option-removed.md diff --git a/errors/powered-by-header-option-removed.md b/errors/powered-by-header-option-removed.md deleted file mode 100644 index 216fbda8..00000000 --- a/errors/powered-by-header-option-removed.md +++ /dev/null @@ -1,15 +0,0 @@ -# The poweredByHeader has been removed - -#### Why This Error Occurred - -Starting at Next.js version 5.0.0 the `poweredByHeader` option has been removed. - -#### Possible Ways to Fix It - -If you still want to remove `x-powered-by` you can use one of the custom-server examples. - -And then manually remove the header using `res.removeHeader('x-powered-by')` - -### Useful Links - -- [Custom Server documentation + examples](https://github.com/zeit/next.js#custom-server-and-routing) diff --git a/server/config.js b/server/config.js index 4ea6bc23..7ed863c0 100644 --- a/server/config.js +++ b/server/config.js @@ -5,6 +5,7 @@ const cache = new Map() const defaultConfig = { webpack: null, webpackDevMiddleware: null, + poweredByHeader: true, distDir: '.next', assetPrefix: '', configOrigin: 'default', @@ -33,9 +34,6 @@ function loadConfig (dir, customConfig) { if (path && path.length) { const userConfigModule = require(path) userConfig = userConfigModule.default || userConfigModule - if (userConfig.poweredByHeader === true || userConfig.poweredByHeader === false) { - console.warn('> the `poweredByHeader` option has been removed https://err.sh/zeit/next.js/powered-by-header-option-removed') - } userConfig.configOrigin = 'next.config.js' } diff --git a/server/index.js b/server/index.js index 9fe619b9..1b1e546b 100644 --- a/server/index.js +++ b/server/index.js @@ -323,7 +323,9 @@ export default class Server { return } - res.setHeader('X-Powered-By', `Next.js ${pkg.version}`) + if (this.config.poweredByHeader) { + res.setHeader('X-Powered-By', `Next.js ${pkg.version}`) + } return sendHTML(req, res, html, req.method, this.renderOpts) } diff --git a/test/integration/production/test/index.test.js b/test/integration/production/test/index.test.js index ac58096d..47da09c8 100644 --- a/test/integration/production/test/index.test.js +++ b/test/integration/production/test/index.test.js @@ -134,6 +134,26 @@ describe('Production Usage', () => { await app.render(req, res, req.url) expect(headers['X-Powered-By']).toEqual(`Next.js ${pkg.version}`) }) + + it('should not set it when poweredByHeader==false', async () => { + const req = { url: '/stateless', headers: {} } + const originalConfigValue = app.config.poweredByHeader + app.config.poweredByHeader = false + const res = { + getHeader () { + return false + }, + setHeader (key, value) { + if (key === 'XPoweredBy') { + throw new Error('Should not set the XPoweredBy header') + } + }, + end () {} + } + + await app.render(req, res, req.url) + app.config.poweredByHeader = originalConfigValue + }) }) dynamicImportTests(context, (p, q) => renderViaHTTP(context.appPort, p, q))