Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

chore: add crossorigin to resource tags (#12031)

forehalo4662ee8da7cb
diff --git a/packages/backend/server/src/__tests__/doc/renderer.spec.ts b/packages/backend/server/src/__tests__/doc/renderer.spec.ts
index 8189cbcec1..f8fb832a0e 100644
--- a/packages/backend/server/src/__tests__/doc/renderer.spec.ts
+++ b/packages/backend/server/src/__tests__/doc/renderer.spec.ts
@@ -64,7 +64,7 @@ test('should render correct html', async t => {
t.true(
res.text.includes(
- `<script src="https://app.affine.pro/main.a.js"></script>`
+ `<script src="https://app.affine.pro/main.a.js" crossorigin></script>`
)
);
});
@@ -78,7 +78,7 @@ test.skip('should render correct mobile html', async t => {
t.true(
res.text.includes(
- `<script src="https://app.affine.pro/main.c.js"></script>`
+ `<script src="https://app.affine.pro/main.c.js" crossorigin></script>`
)
);
});
diff --git a/packages/backend/server/src/core/doc-renderer/controller.ts b/packages/backend/server/src/core/doc-renderer/controller.ts
index d54f179248..e9b8c11419 100644
--- a/packages/backend/server/src/core/doc-renderer/controller.ts
+++ b/packages/backend/server/src/core/doc-renderer/controller.ts
@@ -194,11 +194,11 @@ export class DocRendererController {
${Object.entries(envMeta)
.map(([key, val]) => `<meta name="env:${key}" content="${val}" />`)
.join('\n')}
- ${assets.css.map(url => `<link rel="stylesheet" href="${url}" />`).join('\n')}
+ ${assets.css.map(url => `<link rel="stylesheet" href="${url}" crossorigin />`).join('\n')}
</head>
<body>
<div id="app" data-version="${assets.gitHash}"></div>
- ${assets.js.map(url => `<script src="${url}"></script>`).join('\n')}
+ ${assets.js.map(url => `<script src="${url}" crossorigin></script>`).join('\n')}
</body>
</html>
`;
diff --git a/tools/cli/src/webpack/html-plugin.ts b/tools/cli/src/webpack/html-plugin.ts
index b710cae85e..5f1838c116 100644
--- a/tools/cli/src/webpack/html-plugin.ts
+++ b/tools/cli/src/webpack/html-plugin.ts
@@ -81,6 +81,7 @@ function getHTMLPluginOptions(BUILD_CONFIG: BUILD_CONFIG_TYPE) {
minify: false,
templateParameters: templateParams,
chunks: ['app'],
+ scriptLoading: 'blocking',
} satisfies HTMLPlugin.Options;
}
@@ -154,6 +155,27 @@ const GlobalErrorHandlerPlugin = {
},
};
+const CorsPlugin = {
+ apply(compiler: Compiler) {
+ compiler.hooks.compilation.tap('html-js-cors-plugin', compilation => {
+ HTMLPlugin.getHooks(compilation).alterAssetTags.tap(
+ 'html-js-cors-plugin',
+ options => {
+ if (options.publicPath !== '/') {
+ options.assetTags.scripts.forEach(script => {
+ script.attributes.crossorigin = true;
+ });
+ options.assetTags.styles.forEach(style => {
+ style.attributes.crossorigin = true;
+ });
+ }
+ return options;
+ }
+ );
+ });
+ },
+};
+
export function createHTMLPlugins(
BUILD_CONFIG: BUILD_CONFIG_TYPE,
config: CreateHTMLPluginConfig
@@ -206,6 +228,10 @@ export function createHTMLPlugins(
);
}
+ if (!BUILD_CONFIG.isElectron) {
+ plugins.push(CorsPlugin);
+ }
+
if (config.emitAssetsManifest) {
plugins.push(AssetsManifestPlugin);
}