Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting headers in the local Angular development server

My application is a client side only app currently running on localhost. I am trying to use a Wasm library that requires access to SharedArrayBuffer. It is working on Chrome and Edge, however it seems Firefox has put restrictions in place resulting in an error: ReferenceError: SharedArrayBuffer is not defined

According to MDN Firefox requires the following headers to be set:

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

My app is running on localhost:4200. I am trying to get the ng serve development server to set the headers. I have attempted to do this with the following code:

// proxy.conf.js
module.exports = {
  "/": {
    logLevel: "debug",
    target: "http://localhost:4200",
    bypass: (req, res, proxyOptions) => {
      res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
      res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
    },
  },
};

However this does not work. Is there either a way to set the headers with the Angular server, or another workaround?

like image 540
MattMan569 Avatar asked Jun 10 '26 00:06

MattMan569


1 Answers

You can add the COEP and COOP headers to the Angular Dev-server in the angular.json to overcome this issue.

Eg.,

"serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "web-ui:build",
            "proxyConfig": "proxy.conf.json",
            "headers": {
              "Cross-Origin-Opener-Policy":"same-origin",
              "Cross-Origin-Embedder-Policy":"require-corp"
            }
           }
          }

This should get both the headers "Cross-Origin-Opener-Policy" and "Cross-Origin-Embedder-Policy" to appropriate values. Run ng serve to get these changes reflect in the response.

like image 173
Sasikumar Avatar answered Jun 11 '26 15:06

Sasikumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!