admin管理员组文章数量:1380017
So I've been reading that you can't access cssRules for external stylesheets because it runs into CORS policy issues.
I decided to take a different approach but I'm still running into issues.
My Approach:
- Download the css files on the backend and upload them to S3 Bucket
- Return back a existing link and new link for match purposes
- Delete existing
link
tag and add in a new tag that will point to my CDN - Access
document.styleSheets
- Tadaaaa (but this fails)
What I'm trying to figure out is why am I still running into issues if my CDN allows access from any origin?
export default () => {
const payload = [...document.styleSheets].filter(s => s.href).map(s => s.href);
axios.post('SOME ENDPOINT', { css: payload }).then(({ status, data: { data: newLinks } }) => {
if (status === 200) {
for (const i in newLinks) {
document.querySelector(`link[href="${newLinks[i].source}"]`).remove()
const stylesheet = document.createElement('link');
stylesheet.rel = 'stylesheet';
stylesheet.href = newLinks[i].downloaded;
document.getElementsByTagName('head')[0].appendChild(stylesheet);
}
}
}).then(() => {
let delay = 250
setTimeout(() => {
console.log('Stylesheets with Removed Links', [...document.styleSheets]);
}, delay)
}).then(() => {
console.log([...document.styleSheets])
})
}
Error on Safari SecurityError: Not allowed to access cross-origin stylesheet
I have seen this link Cannot access cssRules from local css file in Chrome 64
Result From Network Tab
So I've been reading that you can't access cssRules for external stylesheets because it runs into CORS policy issues.
I decided to take a different approach but I'm still running into issues.
My Approach:
- Download the css files on the backend and upload them to S3 Bucket
- Return back a existing link and new link for match purposes
- Delete existing
link
tag and add in a new tag that will point to my CDN - Access
document.styleSheets
- Tadaaaa (but this fails)
What I'm trying to figure out is why am I still running into issues if my CDN allows access from any origin?
export default () => {
const payload = [...document.styleSheets].filter(s => s.href).map(s => s.href);
axios.post('SOME ENDPOINT', { css: payload }).then(({ status, data: { data: newLinks } }) => {
if (status === 200) {
for (const i in newLinks) {
document.querySelector(`link[href="${newLinks[i].source}"]`).remove()
const stylesheet = document.createElement('link');
stylesheet.rel = 'stylesheet';
stylesheet.href = newLinks[i].downloaded;
document.getElementsByTagName('head')[0].appendChild(stylesheet);
}
}
}).then(() => {
let delay = 250
setTimeout(() => {
console.log('Stylesheets with Removed Links', [...document.styleSheets]);
}, delay)
}).then(() => {
console.log([...document.styleSheets])
})
}
Error on Safari SecurityError: Not allowed to access cross-origin stylesheet
I have seen this link Cannot access cssRules from local css file in Chrome 64
Result From Network Tab
Share Improve this question edited Mar 2, 2022 at 18:16 Nikster asked Mar 2, 2022 at 17:58 NiksterNikster 4841 gold badge6 silver badges22 bronze badges 2-
Doing
document.getElementsByTagName('head')[0].appendChild(stylesheet)
should trigger a request for that stylesheet. Does that request look ok in the Network tab? – James Commented Mar 2, 2022 at 18:03 - @James yup all of it looks good. Updated description with the request – Nikster Commented Mar 2, 2022 at 18:06
2 Answers
Reset to default 3I ended up finding a solution...
All thanks to Paulo Belo from this link Uncaught DOMException: Failed to read the 'cssRules' property
stylesheet.crossOrigin = "anonymous"
solved my problem giving me access to the cssRules.
https://developer.mozilla/en-US/docs/Web/HTML/Attributes/crossorigin
Note this fix does not work with existing stylesheets that are throwing this error.
Exception: DOMException: Failed to read the 'cssRules' property from 'CSSStyleSheet': Cannot access rules at CSSStyleSheet.s
This fix only works for your own uploaded sheets or in my case the ones from my CDN.
For ReactJS,
- Go to public folder
- In index.html file, add
crossorigin="anonymous"
for css (which is giving cross-origin error)
Example:
本文标签: javascriptCannot Access cssRules for Stylesheet CORSStack Overflow
版权声明:本文标题:javascript - Cannot Access cssRules for Stylesheet CORS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744453884a2606892.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论