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 140
Prioritize the module loader script with link headers #203
Merged
Merged
Changes from all commits
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
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
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 |
---|---|---|
@@ -1,7 +1,16 @@ | ||
'use strict'; | ||
|
||
const AsyncStream = require('./streams/async-stream'); | ||
const LinkHeader = require('http-link-header'); | ||
const ContentLengthStream = require('./streams/content-length-stream'); | ||
const { TEMPLATE_NOT_FOUND } = require('./fetch-template'); | ||
const processTemplate = require('./process-template'); | ||
const { | ||
getLoaderScript, | ||
getFragmentAssetsToPreload, | ||
nextIndexGenerator | ||
} = require('./utils'); | ||
|
||
// Events emitted by fragments on the template | ||
const FRAGMENT_EVENTS = [ | ||
'start', | ||
'response', | ||
|
@@ -11,54 +20,6 @@ const FRAGMENT_EVENTS = [ | |
'fallback', | ||
'warn' | ||
]; | ||
const { TEMPLATE_NOT_FOUND } = require('./fetch-template'); | ||
|
||
const processTemplate = require('./process-template'); | ||
|
||
const getCrossOriginHeader = (fragmentUrl, host) => { | ||
if (host && fragmentUrl.indexOf(`://${host}`) < 0) { | ||
return 'crossorigin'; | ||
} | ||
return ''; | ||
}; | ||
|
||
// Early preloading of primary fragments assets to improve Performance | ||
const getAssetsToPreload = ({ link }, { headers = {} }) => { | ||
let assetsToPreload = []; | ||
|
||
const { refs = [] } = LinkHeader.parse(link); | ||
const scriptRefs = refs | ||
.filter(ref => ref.rel === 'fragment-script') | ||
.map(ref => ref.uri); | ||
const styleRefs = refs | ||
.filter(ref => ref.rel === 'stylesheet') | ||
.map(ref => ref.uri); | ||
|
||
// Handle Server rendered fragments without depending on assets | ||
if (!scriptRefs[0] && !styleRefs[0]) { | ||
return ''; | ||
} | ||
styleRefs.forEach(uri => { | ||
assetsToPreload.push(`<${uri}>; rel="preload"; as="style"; nopush`); | ||
}); | ||
scriptRefs.forEach(uri => { | ||
const crossOrigin = getCrossOriginHeader(uri, headers.host); | ||
assetsToPreload.push( | ||
`<${uri}>; rel="preload"; as="script"; nopush; ${crossOrigin}` | ||
); | ||
}); | ||
return assetsToPreload.join(','); | ||
}; | ||
|
||
function nextIndexGenerator(initialIndex, step) { | ||
let index = initialIndex; | ||
|
||
return () => { | ||
let pastIndex = index; | ||
index += step; | ||
return pastIndex; | ||
}; | ||
} | ||
|
||
/** | ||
* Process the HTTP Request to the Tailor Middleware | ||
|
@@ -74,7 +35,8 @@ module.exports = function processRequest(options, request, response) { | |
fetchTemplate, | ||
parseTemplate, | ||
filterResponseHeaders, | ||
maxAssetLinks | ||
maxAssetLinks, | ||
amdLoaderUrl | ||
} = options; | ||
|
||
const asyncStream = new AsyncStream(); | ||
|
@@ -142,12 +104,16 @@ module.exports = function processRequest(options, request, response) { | |
} | ||
|
||
// Make resources early discoverable while processing HTML | ||
const preloadAssets = headers.link | ||
? getAssetsToPreload(headers, request) | ||
: ''; | ||
if (preloadAssets !== '') { | ||
responseHeaders.link = preloadAssets; | ||
} | ||
const assetsToPreload = getFragmentAssetsToPreload( | ||
headers, | ||
request.headers | ||
); | ||
|
||
// Loader script must be preloaded before every fragment asset | ||
const loaderScript = getLoaderScript(amdLoaderUrl, request.headers); | ||
loaderScript !== '' && assetsToPreload.unshift(loaderScript); | ||
|
||
responseHeaders.link = assetsToPreload.join(','); | ||
this.emit('response', request, statusCode, responseHeaders); | ||
|
||
response.writeHead(statusCode, responseHeaders); | ||
|
@@ -180,8 +146,10 @@ module.exports = function processRequest(options, request, response) { | |
extendedOptions, | ||
context | ||
); | ||
let isFragmentFound = false; | ||
|
||
resultStream.on('fragment:found', fragment => { | ||
isFragmentFound = true; | ||
FRAGMENT_EVENTS.forEach(eventName => { | ||
fragment.once(eventName, (...args) => { | ||
const prefixedName = 'fragment:' + eventName; | ||
|
@@ -202,7 +170,18 @@ module.exports = function processRequest(options, request, response) { | |
const statusCode = response.statusCode || 200; | ||
if (shouldWriteHead) { | ||
shouldWriteHead = false; | ||
// Preload the loader script when atleast | ||
// one fragment is present on the page | ||
if (isFragmentFound) { | ||
const loaderScript = getLoaderScript( | ||
amdLoaderUrl, | ||
request.headers | ||
); | ||
loaderScript !== '' && | ||
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. any reason not to use explicit |
||
(responseHeaders.link = loaderScript); | ||
} | ||
this.emit('response', request, statusCode, responseHeaders); | ||
|
||
response.writeHead(statusCode, responseHeaders); | ||
resultStream.pipe(contentLengthStream).pipe(response); | ||
} | ||
|
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 |
---|---|---|
@@ -0,0 +1,96 @@ | ||
'use strict'; | ||
|
||
const LinkHeader = require('http-link-header'); | ||
|
||
const getCrossOrigin = (url = '', host = '') => { | ||
if (url.includes(`://${host}`)) { | ||
return ''; | ||
} | ||
return 'crossorigin'; | ||
}; | ||
|
||
const getPreloadAttributes = ({ | ||
assetUrl, | ||
host, | ||
asAttribute, | ||
corsCheck = false, | ||
noPush = true // Disable HTTP/2 Push behaviour until digest spec is implemented by most browsers | ||
}) => { | ||
return ( | ||
assetUrl && | ||
`<${assetUrl}>; rel="preload"; as="${asAttribute}"; ${noPush | ||
? 'nopush;' | ||
: ''} ${corsCheck ? getCrossOrigin(assetUrl, host) : ''}`.trim() | ||
); | ||
}; | ||
|
||
// Module loader script used by tailor for managing dependency between fragments | ||
const getLoaderScript = (amdLoaderUrl, { host } = {}) => { | ||
if (amdLoaderUrl.startsWith('file://')) { | ||
return ''; | ||
} | ||
|
||
return getPreloadAttributes({ | ||
assetUrl: amdLoaderUrl, | ||
asAttribute: 'script', | ||
corsCheck: true, | ||
host | ||
}); | ||
}; | ||
|
||
// Early preloading of primary fragments assets to improve Performance | ||
const getFragmentAssetsToPreload = ({ link = '' }, { host } = {}) => { | ||
let assetsToPreload = []; | ||
|
||
const { refs = [] } = LinkHeader.parse(link); | ||
const scriptRefs = refs | ||
.filter(ref => ref.rel === 'fragment-script') | ||
.map(ref => ref.uri); | ||
const styleRefs = refs | ||
.filter(ref => ref.rel === 'stylesheet') | ||
.map(ref => ref.uri); | ||
|
||
// Handle Server rendered fragments without depending on assets | ||
if (!scriptRefs[0] && !styleRefs[0]) { | ||
return assetsToPreload; | ||
} | ||
|
||
for (const uri of styleRefs) { | ||
assetsToPreload.push( | ||
getPreloadAttributes({ | ||
assetUrl: uri, | ||
asAttribute: 'style' | ||
}) | ||
); | ||
} | ||
|
||
for (const uri of scriptRefs) { | ||
assetsToPreload.push( | ||
getPreloadAttributes({ | ||
assetUrl: uri, | ||
asAttribute: 'script', | ||
corsCheck: true, | ||
host | ||
}) | ||
); | ||
} | ||
|
||
return assetsToPreload; | ||
}; | ||
|
||
const nextIndexGenerator = (initialIndex, step) => { | ||
let index = initialIndex; | ||
|
||
return () => { | ||
let pastIndex = index; | ||
index += step; | ||
return pastIndex; | ||
}; | ||
}; | ||
|
||
module.exports = { | ||
getCrossOrigin, | ||
getFragmentAssetsToPreload, | ||
getLoaderScript, | ||
nextIndexGenerator | ||
}; |
Oops, something went wrong.
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.
any reason not to use explicit
if () {}
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.
To avoid the typecasting.