Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set custom response headers for NuxtJS's SSR server?

I need to set these two custom response headers to enable certain features in my site:

Cross-Origin-Embedder-Policy: require-corp
Cross-Origin-Opener-Policy: same-origin

How can I do this in a NuxtJS app? I would like to do it for both the dev and production servers.

like image 913
Leo Avatar asked Nov 14 '25 18:11

Leo


1 Answers

Thanks Mani for the sharing. I copy the idea to add the below response header on my nuxt, and i test my docker img local and aws ecs's dev and prod env both can work.

X-Frame-Options: SAMEORIGIN

X-Content-Type-Options: nosniff

First create a new js e.g. headers.js, i put under server-middleware because i also have logging file.


headers.js

module.exports = function (req, res, next) {
  res.setHeader('X-Frame-Options', 'SAMEORIGIN')
  res.setHeader('X-Content-Type-Options', 'nosniff')
  next()
}

nuxt.config.js

  export default {
    serverMiddleware: [
      '~/server-middleware/headers'
    ]
  }
like image 54
Ricson Goo Avatar answered Nov 17 '25 07:11

Ricson Goo