Option to add CORS headers to static assets
See original GitHub issueDiscussed in https://github.com/nuxt/framework/discussions/4003
<div type='discussions-op-text'>Originally posted by hubermat March 31, 2022 Hello,
our Nuxt-based project serves, besides other API routes, static files (i.e. files from ./public
). These static files will also be accessed from outside our web application, but browsers apply CORS policies. Up to now I could not find a way to add access-control-allow-origin
headers to the static files that are served by the Nuxt server.
I have created a pull request, that adds a new config option in nuxt.config.js:
render: {
static: {
/**
* Whether to add headers to allow access from any origin
*
* If set to true, all responses of static files from the `public/` directory will contain the following headers:
* Access-Control-Allow-Origin *
* Access-Control-Allow-Methods GET
* Access-Control-Allow-Headers Origin, X-Requested-With, Content-Type, Accept
* X-Frame-Options ALLOWALL
*/
allowFromAnyOrigin: false
},
}
If allowFromAnyOrigin is set to true, for development mode, a function setHeaders will be passed to serve-static to set these headers. For production mode, the public assets will be flagged and the headers will be set in the static middleware.
</div>Issue Analytics
- State:
- Created a year ago
- Reactions:4
- Comments:10 (5 by maintainers)
Top Results From Across the Web
Enabling CORS for StaticFiles in ASP.NET Core - bytefish.de
We can use the OnPrepareResponse to serve CORS headers, when configuring the Static Files Middleware. We are also injecting the ICorsService and ...
Read more >How to serve static files (for CORS / OPTION request) in IIS?
There I added a new header named "Access-Control-Allow-Origin" with value "*". From this moment, I can use static files in XHR.
Read more >Setting CORS & Cache Headers on Static Assets in Umbraco
In this latest Hack Make Do video by Warren, he shows how easy it is to add custom headers such as CORS and...
Read more >CORS headers in Passenger Standalone
Ensure you have your web server configured to serve your static files. In Standalone mode this is only nessesary if your static assets...
Read more >Set custom headers on static content
To allow cross origin requests, add a Access-Control-Allow-Origin header to responses. You can do so for specific origins or for all origins with...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@pi0 How can I use this solution when I’m trying to send requests with credentials and get:
Access to XMLHttpRequest at 'http://localhost:3000/api/v1/admin/auth/login' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
I have set nuxt.config/routeRules to
routeRules: { '/**': { headers: { 'Access-Control-Allow-Origin': 'http://localhost:8080' } }, },
and didn’t work out. I appreciate any guidance on this.@rezaseyedi91 This is likely an issue that only occurs in development when using Vite. You can fix this by explicitly setting your origin in
nuxt.config.ts
: