question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't remove x-powered-by header in Next 5

See original GitHub issue
  • 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

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:15 (10 by maintainers)

github_iconTop GitHub Comments

7reactions
rauchgcommented, Feb 8, 2018

How about:

const app = next({ dev, xPoweredBy: false })

and/or

xPoweredBy: false

in next.config.js ?

5reactions
timneutkenscommented, Feb 7, 2018

@timteeling we’re going to discuss this internally, either the option comes back or we remove it alltogether 🙏

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to remove X-Powered-By in ExpressJS - Stack Overflow
I just tested app.disable('custom1'); And it worked fine (it removed the header from server response). But then I commented out app.
Read more >
Getting rid of the X-Powered-By in Express JS Middle-ware ...
Completely removing the X-Powered-By headers ... First make sure which version of Express JS you are using right now. The reason being, different ......
Read more >
next.config.js: Disabling x-powered-by
Disabling x-powered-by. By default Next.js will add the x-powered-by header. To opt-out of it, open next.config.js and disable the poweredByHeader config:
Read more >
Disable the IIS web banner and other IIS headers in the Orion ...
Open the IIS Manager. · Select the Orion website. · Select "HTTP Response Headers" · Select the "X-Powered-By" HTTP Header and select "Remove"....
Read more >
Remove Unwanted HTTP Response Headers
Click on the X-Powered-By header and then click Remove on the Actions Pane to remove it from the response. thumbnail image 8 of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found