Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: crossOriginLoading anonymous not work when loading styles #16925

Merged
merged 1 commit into from Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/css/CssLoadingRuntimeModule.js
Expand Up @@ -111,7 +111,7 @@ class CssLoadingRuntimeModule extends RuntimeModule {
? crossOriginLoading === "use-credentials"
? 'link.crossOrigin = "use-credentials";'
: Template.asString([
"if (link.src.indexOf(window.location.origin + '/') !== 0) {",
"if (link.href.indexOf(window.location.origin + '/') !== 0) {",
Template.indent(
`link.crossOrigin = ${JSON.stringify(crossOriginLoading)};`
),
Expand Down
14 changes: 14 additions & 0 deletions test/configCases/crossorigin/set-crossorigin/index.js
Expand Up @@ -65,3 +65,17 @@ it("should load script with crossorigin attribute anonymous (different origin)",

return promise;
});

it("should load style with crossorigin attribute anonymous (different origin)", function() {
var originalValue = __webpack_public_path__;
__webpack_public_path__ = "https://example.com/";
const promise = import("./style.css?e" /* webpackChunkName: "crossorigin-different-origin" */);
__webpack_public_path__ = originalValue;

var link = document.head._children[0];

expect(link.href).toBe("https://example.com/crossorigin-different-origin.web.css");
expect(link.crossOrigin).toBe("anonymous");

return promise;
});
3 changes: 3 additions & 0 deletions test/configCases/crossorigin/set-crossorigin/style.css
@@ -0,0 +1,3 @@
body {
color: red;
}
Expand Up @@ -10,5 +10,8 @@ module.exports = {
},
optimization: {
minimize: false
},
experiments: {
css: true
}
};
4 changes: 3 additions & 1 deletion test/helpers/FakeDocument.js
Expand Up @@ -160,7 +160,9 @@ class FakeSheet {
let css = fs.readFileSync(
path.resolve(
this._basePath,
this._element.href.replace(/^https:\/\/test\.cases\/path\//, "")
this._element.href
.replace(/^https:\/\/test\.cases\/path\//, "")
.replace(/^https:\/\/example\.com\//, "")
),
"utf-8"
);
Expand Down