Skip to content

Commit e2a7063

Browse files
trashpanda001Akryum
authored andcommitted
fix(modern): send credentials when loading script modules. (#1695)
* Send credentials when loading script modules. Otherwise, things like BasicAuth won't work. * fix: updated modern mode tests. * fix: updated crossorigin comment. * fix: typo
1 parent 14f2392 commit e2a7063

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

packages/@vue/cli-service/__tests__/modernMode.spec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ test('modern mode', async () => {
2525
// assert correct asset links
2626
const index = await project.read('dist/index.html')
2727

28-
// should use <script type="module"> for modern bundle
29-
expect(index).toMatch(/<script type=module src=\/js\/chunk-vendors\.\w{8}\.js>/)
30-
expect(index).toMatch(/<script type=module src=\/js\/app\.\w{8}\.js>/)
28+
// should use <script type="module" crossorigin=use-credentials> for modern bundle
29+
expect(index).toMatch(/<script type=module src=\/js\/chunk-vendors\.\w{8}\.js crossorigin=use-credentials>/)
30+
expect(index).toMatch(/<script type=module src=\/js\/app\.\w{8}\.js crossorigin=use-credentials>/)
3131

32-
// should use <link rel="modulepreload"> for modern bundle
33-
expect(index).toMatch(/<link [^>]*js\/chunk-vendors\.\w{8}\.js rel=modulepreload>/)
34-
expect(index).toMatch(/<link [^>]*js\/app\.\w{8}\.js rel=modulepreload>/)
32+
// should use <link rel="modulepreload" crossorigin=use-credentials> for modern bundle
33+
expect(index).toMatch(/<link [^>]*js\/chunk-vendors\.\w{8}\.js rel=modulepreload crossorigin=use-credentials>/)
34+
expect(index).toMatch(/<link [^>]*js\/app\.\w{8}\.js rel=modulepreload crossorigin=use-credentials>/)
3535

3636
// should use <script nomodule> for legacy bundle
3737
expect(index).toMatch(/<script src=\/js\/chunk-vendors-legacy\.\w{8}\.js nomodule>/)

packages/@vue/cli-service/lib/webpack/ModernModePlugin.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ class ModernModePlugin {
3838
compilation.hooks.htmlWebpackPluginAlterAssetTags.tapAsync(ID, async (data, cb) => {
3939
// use <script type="module"> for modern assets
4040
const modernAssets = data.body.filter(a => a.tagName === 'script')
41-
modernAssets.forEach(a => { a.attributes.type = 'module' })
41+
modernAssets.forEach(a => {
42+
a.attributes.type = 'module'
43+
a.attributes.crossorigin = 'use-credentials'
44+
})
4245

43-
// inject Safari 10 nomdoule fix
46+
// inject Safari 10 nomodule fix
4447
data.body.push({
4548
tagName: 'script',
4649
closeTag: true,
@@ -62,7 +65,7 @@ class ModernModePlugin {
6265
data.html = data.html
6366
// use <link rel="modulepreload"> instead of <link rel="preload">
6467
// for modern assets
65-
.replace(/(<link as=script .*?)rel=preload>/g, '$1rel=modulepreload>')
68+
.replace(/(<link as=script .*?)rel=preload>/g, '$1rel=modulepreload crossorigin=use-credentials>')
6669
.replace(/\snomodule="">/g, ' nomodule>')
6770
})
6871
})

0 commit comments

Comments
 (0)