Skip to content

Commit 89597ae

Browse files
committed
feat: Add as attribute to prefetch tags as well.
Includes 92aad43 chore: update resource hints from vuejs#27
1 parent 6ad71c4 commit 89597ae

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

src/index.js

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ class PreloadPlugin {
3535
})
3636

3737
const htmlChunks = options.include === 'allAssets'
38-
// Handle all chunks.
38+
// Handle all chunks.
3939
? extractedChunks
40-
// Only handle chunks imported by this HtmlWebpackPlugin.
40+
// Only handle chunks imported by this HtmlWebpackPlugin.
4141
: extractedChunks.filter((chunk) => doesChunkBelongToHTML({
4242
chunk,
4343
compilation,
@@ -84,21 +84,17 @@ class PreloadPlugin {
8484
rel: options.rel
8585
}
8686

87-
// If we're preloading this resource (as opposed to prefetching),
88-
// then we need to set the 'as' attribute correctly.
89-
if (options.rel === 'preload') {
90-
attributes.as = determineAsValue({
91-
href,
92-
file,
93-
optionsAs: options.as
94-
})
95-
96-
// On the off chance that we have a cross-origin 'href' attribute,
97-
// set crossOrigin on the <link> to trigger CORS mode. Non-CORS
98-
// fonts can't be used.
99-
if (attributes.as === 'font') {
100-
attributes.crossorigin = ''
101-
}
87+
attributes.as = determineAsValue({
88+
href,
89+
file,
90+
optionsAs: options.as
91+
})
92+
93+
// On the off chance that we have a cross-origin 'href' attribute,
94+
// set crossOrigin on the <link> to trigger CORS mode. Non-CORS
95+
// fonts can't be used.
96+
if (options.rel === 'preload' && attributes.as === 'font') {
97+
attributes.crossorigin = ''
10298
}
10399

104100
links.push({

test/spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,10 +400,10 @@ module.exports = ({
400400
const links = dom.window.document.head.querySelectorAll('link')
401401
expect(links.length).toBe(2)
402402
expect(links[0].getAttribute('rel')).toBe('prefetch')
403-
expect(links[0].hasAttribute('as')).toBeFalsy()
403+
expect(links[0].getAttribute('as')).toBe('script')
404404
expect(links[0].getAttribute('href')).toBe('home.js')
405405
expect(links[1].getAttribute('rel')).toBe('prefetch')
406-
expect(links[1].hasAttribute('as')).toBeFalsy()
406+
expect(links[1].getAttribute('as')).toBe('script')
407407
expect(links[1].getAttribute('href')).toBe('main.js')
408408

409409
done()

0 commit comments

Comments
 (0)