Skip to content

Commit 184cf78

Browse files
committed
Better access-control headers PROD-4372
1 parent 84443fe commit 184cf78

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

config/webpack/app-base.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ module.exports = function configFactory(ops) {
7373

7474
/* Build timestamp. */
7575
timestamp: now.utc().toISOString(),
76+
77+
...(o.crossOriginLoading ? {
78+
crossOriginLoading: o.crossOriginLoading
79+
} : {})
7680
};
7781
fs.writeFileSync(buildInfoUrl, JSON.stringify(buildInfo));
7882
}
@@ -103,6 +107,9 @@ module.exports = function configFactory(ops) {
103107
filename: `[name]-${now.valueOf()}.js`,
104108
path: path.resolve(__dirname, o.context, 'build'),
105109
publicPath: `${o.publicPath}/`,
110+
...(o.crossOriginLoading ? {
111+
crossOriginLoading: o.crossOriginLoading
112+
} : {})
106113
},
107114
plugins: [
108115
new MiniCssExtractPlugin({

src/server/renderer.jsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ export default function factory(webpackConfig, options) {
157157
const INJ = forge.util.encode64(`${iv}${cipher.output.data}`);
158158

159159
/* It is supposed to end with '/' symbol as path separator. */
160-
const { publicPath } = webpackConfig.output;
160+
const { publicPath, crossOriginLoading } = webpackConfig.output;
161+
const crossorigin = crossOriginLoading ? `crossorigin="${crossOriginLoading}"` : "";
161162

162163
let assetsByChunkName;
163164
const { webpackStats } = res.locals;
@@ -177,7 +178,7 @@ export default function factory(webpackConfig, options) {
177178
assets = assets.filter(asset => asset.endsWith('.css'));
178179
assets.forEach((asset) => {
179180
styles.push((
180-
`<link data-chunk="${chunk}" id="tru-style" href="${publicPath}${asset}" rel="stylesheet" />`
181+
`<link data-chunk="${chunk}" id="tru-style" href="${publicPath}${asset}" ${crossorigin} rel="stylesheet" />`
181182
));
182183
});
183184
});
@@ -192,6 +193,7 @@ export default function factory(webpackConfig, options) {
192193
<link rel="manifest" href="${publicPath}manifest.json">
193194
<link
194195
href="${publicPath}main-${timestamp}.css"
196+
${crossorigin}
195197
id="tru-style"
196198
rel="stylesheet"
197199
/>
@@ -211,11 +213,13 @@ export default function factory(webpackConfig, options) {
211213
</script>
212214
<script
213215
src="${publicPath}polyfills-${timestamp}.js"
216+
${crossorigin}
214217
type="application/javascript"
215218
></script>
216219
${extraScripts ? extraScripts.join('') : ''}
217220
<script
218221
src="${publicPath}main-${timestamp}.js"
222+
${crossorigin}
219223
type="application/javascript"
220224
></script>
221225
</body>

src/shared/containers/AppChunk/index.jsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export default class SplitRoute extends React.Component {
6161
} = this.props;
6262

6363
const PUBLIC_PATH = global.TRU_BUILD_INFO.publicPath;
64+
const CROSS_ORIGIN_LOADING = global.TRU_BUILD_INFO.crossOriginLoading;
6465

6566
const timestamp = moment(buildTimestamp()).valueOf();
6667

@@ -171,6 +172,9 @@ export default class SplitRoute extends React.Component {
171172
link = document.createElement('link');
172173
link.setAttribute('data-chunk', chunkName);
173174
link.setAttribute('href', `${PUBLIC_PATH}/${chunkName}-${timestamp}.css`);
175+
if (CROSS_ORIGIN_LOADING) {
176+
link.setAttribute('crossorigin', CROSS_ORIGIN_LOADING);
177+
}
174178
link.setAttribute('id', 'tru-style');
175179
link.setAttribute('rel', 'stylesheet');
176180
const head = document.getElementsByTagName('head')[0];

0 commit comments

Comments
 (0)