next.js: Can't remove x-powered-by header in Next 5

  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

The recent change in Next.js 5.0.0 removed the poweredByHeader options from next.config.js and instructs the user to remove it in their custom server.

Current Behavior

X-Powered-By header cannot be removed or overwritten in custom Express or Koa server.

Steps to Reproduce (for bugs)

Tested using Express & Koa examples.

Express

https://custom-server-express-nobxbpklyw.now.sh

const port = parseInt(process.env.PORT, 10) || 3000
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()

app.prepare()
.then(() => {
  const server = express()

  server.get('/a', (req, res) => {
    return app.render(req, res, '/b', req.query)
  })

  server.get('/b', (req, res) => {
    return app.render(req, res, '/a', req.query)
  })

  server.get('/posts/:id', (req, res) => {
    return app.render(req, res, '/posts', { id: req.params.id })
  })

  server.get('*', (req, res) => {
    res.removeHeader('x-powered-by')
    return handle(req, res)
  })

  server.listen(port, (err) => {
    if (err) throw err
    console.log(`> Ready on http://localhost:${port}`)
  })
})

Koa

https://custom-server-koa-wxksegmzhf.now.sh

const Koa = require('koa')
const next = require('next')
const Router = require('koa-router')

const port = parseInt(process.env.PORT, 10) || 3000
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()

app.prepare()
.then(() => {
  const server = new Koa()
  const router = new Router()

  router.get('/a', async ctx => {
    await app.render(ctx.req, ctx.res, '/b', ctx.query)
    ctx.respond = false
  })

  router.get('/b', async ctx => {
    await app.render(ctx.req, ctx.res, '/a', ctx.query)
    ctx.respond = false
  })

  router.get('*', async ctx => {
    await handle(ctx.req, ctx.res)
    ctx.respond = false
  })

  server.use(async (ctx, next) => {
    ctx.res.statusCode = 200
    ctx.response.remove('X-Powered-By')
    await next()
  })

  server.use(router.routes())
  server.listen(port, (err) => {
    if (err) throw err
    console.log(`> Ready on http://localhost:${port}`)
  })
})

Context

Your Environment

Tech Version
next 5.0.0
node 8.8.1
OS macOS 10.13.3
browser Chrome

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 4
  • Comments: 15

Commits related to this issue

Most upvoted comments