Skip to content

Commit

Permalink
Merge pull request #16925 from chenjiahan/fix/css_cross_origin_loading
Browse files Browse the repository at this point in the history
fix: crossOriginLoading anonymous not work when loading styles
  • Loading branch information
TheLarkInn committed Apr 12, 2023
2 parents ee1a267 + be63915 commit 7f08e4d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 2 deletions.
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

0 comments on commit 7f08e4d

Please sign in to comment.