How to set HTTP headers for WebWorker files in Gatsby dev environment? #34995
-
I'm using WebWorkers in a Gatsby-based website (by injecting the Webpack worker-loader into the Webpack configuration of Gatsby). I want to use Is there a way to add specific HTTP response headers for specific files in the Gatsby development environment? Regarding the documentation, I also tried to inject the The code to reproduce the problem and my approaches can be found here: |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
@jens-duttke did you find a solution? |
Beta Was this translation helpful? Give feedback.
-
@ospfranco Back when I was using Gatsby v5.12.12, I solved this using the custom plugin below. I've since migrated to Next.js, where enabling this behavior only requires a few lines in
{
"name": "gatsby-plugin-dev-server-sharedarraybuffer-header",
"private": true,
"sideEffects": true,
"engines": {
"node": ">=14.0.0"
}
}
/** @type {import('gatsby').GatsbyConfig} */
module.exports = {
/** @param {import('express').Application} app */
developMiddleware: (app) => {
app.use((req, res, next) => {
res.set('Cross-Origin-Embedder-Policy', 'require-corp');
res.set('Cross-Origin-Opener-Policy', 'same-origin');
next();
});
// This is a workaround that manipulates Express's middleware stack. It may break with future versions of Gatsby or Express.
/** @type {{ name: string; }[]} */
const stack = app._router.stack;
stack.splice(stack.findIndex((layer) => layer.name === 'middleware') + 1, 0, stack.splice(-1)[0]);
}
};
module.exports = {
plugins: [
'gatsby-plugin-dev-server-sharedarraybuffer-header'
]
}; |
Beta Was this translation helpful? Give feedback.
@ospfranco Back when I was using Gatsby v5.12.12, I solved this using the custom plugin below.
I've since migrated to Next.js, where enabling this behavior only requires a few lines in
next.config.js
. I can't guarantee that this solution still works with the latest Gatsby versions.Create a
plugins
folder in your project root, if it doesn't already existInside
plugins
, create a folder for the custom plugin calledgatsby-plugin-dev-server-sharedarraybuffer-header
Add a
package.json
file with the following content to that folder: