Skip to content

Commit

Permalink
feat: Add as attribute to prefetch tags as well.
Browse files Browse the repository at this point in the history
Includes 92aad43 chore: update resource hints from vuejs#27
  • Loading branch information
Twipped committed Mar 17, 2022
1 parent 6ad71c4 commit 89597ae
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
30 changes: 13 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ class PreloadPlugin {
})

const htmlChunks = options.include === 'allAssets'
// Handle all chunks.
// Handle all chunks.
? extractedChunks
// Only handle chunks imported by this HtmlWebpackPlugin.
// Only handle chunks imported by this HtmlWebpackPlugin.
: extractedChunks.filter((chunk) => doesChunkBelongToHTML({
chunk,
compilation,
Expand Down Expand Up @@ -84,21 +84,17 @@ class PreloadPlugin {
rel: options.rel
}

// If we're preloading this resource (as opposed to prefetching),
// then we need to set the 'as' attribute correctly.
if (options.rel === 'preload') {
attributes.as = determineAsValue({
href,
file,
optionsAs: options.as
})

// On the off chance that we have a cross-origin 'href' attribute,
// set crossOrigin on the <link> to trigger CORS mode. Non-CORS
// fonts can't be used.
if (attributes.as === 'font') {
attributes.crossorigin = ''
}
attributes.as = determineAsValue({
href,
file,
optionsAs: options.as
})

// On the off chance that we have a cross-origin 'href' attribute,
// set crossOrigin on the <link> to trigger CORS mode. Non-CORS
// fonts can't be used.
if (options.rel === 'preload' && attributes.as === 'font') {
attributes.crossorigin = ''
}

links.push({
Expand Down
4 changes: 2 additions & 2 deletions test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,10 +400,10 @@ module.exports = ({
const links = dom.window.document.head.querySelectorAll('link')
expect(links.length).toBe(2)
expect(links[0].getAttribute('rel')).toBe('prefetch')
expect(links[0].hasAttribute('as')).toBeFalsy()
expect(links[0].getAttribute('as')).toBe('script')
expect(links[0].getAttribute('href')).toBe('home.js')
expect(links[1].getAttribute('rel')).toBe('prefetch')
expect(links[1].hasAttribute('as')).toBeFalsy()
expect(links[1].getAttribute('as')).toBe('script')
expect(links[1].getAttribute('href')).toBe('main.js')

done()
Expand Down

0 comments on commit 89597ae

Please sign in to comment.