This repository has been archived by the owner on Dec 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 141
fix issue with preloading wrong assets on pages #141
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,24 @@ const getCrossOriginHeader = (fragmentUrl, host) => { | |
return ''; | ||
}; | ||
|
||
// Early preloading of primary fragments assets to improve Performance | ||
const getAssetsToPreload = ({ link }, { headers = {} }) => { | ||
let assetsToPreload = ''; | ||
const links = parseLinkHeader(link); | ||
// Handle Server rendered fragments without depending on assets | ||
if (links == null) { | ||
return assetsToPreload; | ||
} | ||
if (links.stylesheet && links.stylesheet.url) { | ||
assetsToPreload += `<${links.stylesheet.url}>; rel="preload"; as="style"; nopush,`; | ||
} | ||
if (links['fragment-script'] && links['fragment-script'].url) { | ||
const crossOrigin = getCrossOriginHeader(links['fragment-script'].url, headers.host); | ||
assetsToPreload += `<${links['fragment-script'].url}>; rel="preload"; as="script"; nopush; ${crossOrigin}`; | ||
} | ||
return assetsToPreload; | ||
}; | ||
|
||
/** | ||
* Process the HTTP Request to the Tailor Middleware | ||
* | ||
|
@@ -42,33 +60,6 @@ module.exports = function processRequest (options, request, response) { | |
|
||
let shouldWriteHead = true; | ||
let index = 0; | ||
let assetsToPreload = ''; | ||
|
||
// Early preloading in browsers to improve the primary fragments | ||
const getAssetsToPreload = (fragmentReq, fragment, statusCode, headers) => { | ||
// Do not preload assets for non primary fragments | ||
if (!fragment.primary) { | ||
return; | ||
} | ||
const links = parseLinkHeader(headers.link); | ||
// Remove the event listener once we are done capturing the necessary assets | ||
// to make sure we are not exceeding EventListener's limit (10 default) | ||
this.removeListener('fragment:response', getAssetsToPreload); | ||
|
||
// Handle Server rendered fragments without depending on assets | ||
if (links == null) { | ||
return; | ||
} | ||
if (links.stylesheet && links.stylesheet.url) { | ||
assetsToPreload += `<${links.stylesheet.url}>; rel="preload"; as="style"; nopush,`; | ||
} | ||
if (links['fragment-script'] && links['fragment-script'].url) { | ||
const crossOrigin = getCrossOriginHeader(links['fragment-script'].url, request.headers.host); | ||
assetsToPreload += `<${links['fragment-script'].url}>; rel="preload"; as="script"; nopush; ${crossOrigin},`; | ||
} | ||
}; | ||
|
||
this.on('fragment:response', getAssetsToPreload); | ||
|
||
contextPromise.then((context) => { | ||
|
||
|
@@ -118,7 +109,10 @@ module.exports = function processRequest (options, request, response) { | |
} | ||
this.emit('response', request, statusCode, responseHeaders); | ||
// Make resources early discoverable while processing HTML | ||
assetsToPreload && response.setHeader('Link', assetsToPreload); | ||
let preloadAssets = getAssetsToPreload(headers, request); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, thought I had a config for eslint to capture this case. |
||
if (preloadAssets !== '') { | ||
response.setHeader('Link', preloadAssets); | ||
} | ||
response.writeHead(statusCode, responseHeaders); | ||
resultStream | ||
.pipe(contentLengthStream) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we might end up with a dangling comma here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean? We need the comma between two preloaded assets.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably a trailing comma when the primary fragment doesn't have a JS?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But it's the existing behaviour.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes and browsers does ignore it.. Don't think we need to do anything here. Thoughts?