-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(build): add crossorigin attribute to link[rel="stylesheet"]
#12991
Conversation
Run & review this pull request in StackBlitz Codeflow. |
This sounds good to me, as we are already adding it for |
I also think this looks fine to me and shouldn't impact much of existing setups today. Also agree that we add |
link[rel="stylesheet"]
@Priestch |
@devdubby What's the problem of mixed request? I think maybe it's an issue related to the browser, do you have a link can reproduced the issue? I'm very curious. |
@Priestch
I asked the team developing the CDN, and in this case, the cors policy issue can be caused by the mixture of the cors request and the browser cached no-cors request. (There is nothing wrong with the request and response in the CDN.) Have you had any experience like this? I'm writing this comment because I want to get a little bit of your insight. Thank you for your time. |
@devdubby I add the I'm sorry and have no idea about the problem, but I found a similar article I found a similar article https://anthonymineo.com/solving-a-cors-issue-when-serving-from-chromes-browser-cache/, hope it helps. |
We are facing the same issue. After Migrating to vite5, the css links contains crossorigin. Access to CSS stylesheet at 'https://cdn.getyourguide.com/tf/assets/compiled/client/assets/css-slider-card-CMnb0hfz-v2... 'from origin 'https://www.getyourguide.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. |
The issue seems to be that we are mixing request with
For us, this seems to happen because on some pages, the Vue component is used async ( @Priestch, could it be that we are missing some where to add the |
@fallemand I think the async import failed to add the // ...
transform(code, id) {
if (id.endsWith('vite/preload-helper')) {
return vitePreloadHelperCode;
}
return code;
},
// ...
|
Nice, thanks. I created an issue, and will follow up with a PR. Here is the temp solution: import type { PluginOption } from "vite";
export default function cssCrossoriginPreload(): PluginOption {
return {
name: "css-crossorigin-plugin",
transform(code, id) {
if (id.endsWith("vite/preload-helper.js")) {
const searchValue = "if (!isCss) {";
const replaceValue = `if (isCss) link.crossOrigin = '';${searchValue}`;
if (!code.includes(searchValue)) {
throw new Error(`Failed to path Vite "vite/preload-helper.js"`);
}
const patchedCode = code.replace(searchValue, replaceValue);
return {
code: patchedCode,
map: null,
};
}
return null;
},
};
} |
Description
Add crossorigin attribute to CSS link tag when cssCodeSplit not enable, to make the CSS file can be host on CDN.
Additional context
I'm not sure whether crossorigin attribute should be added to
vite/packages/vite/src/node/plugins/html.ts
Line 672 in 07bd6d1
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).