admin管理员组文章数量:1287800
My current Nextjs app is pulling its static files from Cloudfront. (I upload the /static folder to S3 during the deployment)
Since updating to version 9 I am facing a weird issue where some of my CSS files are getting the following CORS error:
Access to fetch at '.css' from origin '' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
After updating Nextjs to version 10.1.4, all the links of the app stopped working. This is related to the CORS issue because when I deployed without CDN everything worked fine.
Both S3 and Cloudfront are set up to accept CORS requests.
Any help will be appreciated.
My current Nextjs app is pulling its static files from Cloudfront. (I upload the /static folder to S3 during the deployment)
Since updating to version 9 I am facing a weird issue where some of my CSS files are getting the following CORS error:
Access to fetch at 'https://xxx.cloudfront/assets/_next/static/css/b1a6e8370451b02b15e6.css' from origin 'https://example.' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
After updating Nextjs to version 10.1.4, all the links of the app stopped working. This is related to the CORS issue because when I deployed without CDN everything worked fine.
Both S3 and Cloudfront are set up to accept CORS requests.
Any help will be appreciated.
Share Improve this question edited Jun 24, 2021 at 21:55 guyyug asked Jun 24, 2021 at 21:41 guyyugguyyug 1,0871 gold badge12 silver badges23 bronze badges2 Answers
Reset to default 9After a lot of research I discovered the following Nextjs prop which makes sure all the tags have the cross-origin
attribute.
// next.config.js
module.exports = {
crossOrigin: 'anonymous'
}
Also, make sure you set S3 CORS permissions to:
[
{
"AllowedHeaders": [
"*"
],
"AllowedMethods": [
"GET",
"HEAD"
],
"AllowedOrigins": [
"*"
],
"ExposeHeaders": [],
"MaxAgeSeconds": 3000
}
]
And add Cloudfront Behavior Settings to whitelist the following headers: (Under Behaviors -> Edit -> Whitelist Headers)
Access-Control-Request-Headers
Access-Control-Request-Method
Origin
Feature blog post: https://nextjs/blog/next-8#new-crossorigin-config-option
Interesting discussion about this topic: Caching effect on CORS: No 'Access-Control-Allow-Origin' header is present on the requested resource
What worked for me in the latest AWS with Cloudfront is to choose the "CORS-with-preflight" response header policy.
enter image description here
本文标签: javascriptNextjs static files CORS issueCausing links to break on version 1014Stack Overflow
版权声明:本文标题:javascript - Nextjs static files CORS issue - Causing links to break on version 10.1.4 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741328255a2372615.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论