Unverified Commit 52efe336 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Reuse existing `<link rel=stylesheet>` on nav (#16537)

This pull request reuses existing `<link rel=stylesheet>` tags if their `href` matches instead of recreating it. This is in effort to fix an edge case where the browser will FOUC on the tag swap.

This behavior should be sufficiently covered by all the existing CSS cases, as misbehavior would result in the resulting CSS styles being incorrect.
parent 34f737c4
......@@ -612,28 +612,49 @@ function doRender({
// Clean up previous render if canceling:
;([].slice.call(
document.querySelectorAll('link[data-n-staging]')
document.querySelectorAll(
'link[data-n-staging], noscript[data-n-staging]'
)
) as HTMLLinkElement[]).forEach((el) => {
el.parentNode!.removeChild(el)
})
let referenceNode: HTMLLinkElement | undefined = ([].slice.call(
const referenceNodes: HTMLLinkElement[] = [].slice.call(
document.querySelectorAll('link[data-n-g], link[data-n-p]')
) as HTMLLinkElement[]).pop()
const required: Promise<void>[] = styleSheets.map((href) => {
const [link, promise] = createLink(href, 'stylesheet')
link.setAttribute('data-n-staging', '')
// Media `none` does not work in Firefox, so `print` is more
// cross-browser. Since this is so short lived we don't have to worry
// about style thrashing in a print view (where no routing is going to be
// happening anyway).
link.setAttribute('media', 'print')
) as HTMLLinkElement[]
const referenceHrefs = new Set(
referenceNodes.map((e) => e.getAttribute('href'))
)
let referenceNode: Element | undefined =
referenceNodes[referenceNodes.length - 1]
const required: (Promise<any> | true)[] = styleSheets.map((href) => {
let newNode: Element, promise: Promise<any> | true
const existingLink = referenceHrefs.has(href)
if (existingLink) {
newNode = document.createElement('noscript')
newNode.setAttribute('data-n-staging', href)
promise = true
} else {
const [link, onload] = createLink(href, 'stylesheet')
link.setAttribute('data-n-staging', '')
// Media `none` does not work in Firefox, so `print` is more
// cross-browser. Since this is so short lived we don't have to worry
// about style thrashing in a print view (where no routing is going to be
// happening anyway).
link.setAttribute('media', 'print')
newNode = link
promise = onload
}
if (referenceNode) {
referenceNode.parentNode!.insertBefore(link, referenceNode.nextSibling)
referenceNode = link
referenceNode.parentNode!.insertBefore(
newNode,
referenceNode.nextSibling
)
referenceNode = newNode
} else {
document.head.appendChild(link)
document.head.appendChild(newNode)
}
return promise
})
......@@ -662,17 +683,34 @@ function doRender({
function onCommit() {
if (
// We can skip this during hydration. Running it wont cause any harm, but
// we may as well save the CPU cycles.
!isInitialRender &&
// We use `style-loader` in development, so we don't need to do anything
// unless we're in production:
process.env.NODE_ENV === 'production'
process.env.NODE_ENV === 'production' &&
// We can skip this during hydration. Running it wont cause any harm, but
// we may as well save the CPU cycles:
!isInitialRender &&
// Ensure this render commit owns the currently staged stylesheets:
renderPromiseReject === lastRenderReject
) {
// Remove old stylesheets:
// Remove or relocate old stylesheets:
const relocatePlaceholders = [].slice.call(
document.querySelectorAll('noscript[data-n-staging]')
) as HTMLElement[]
const relocateHrefs = relocatePlaceholders.map((e) =>
e.getAttribute('data-n-staging')
)
;([].slice.call(
document.querySelectorAll('link[data-n-p]')
) as HTMLLinkElement[]).forEach((el) => el.parentNode!.removeChild(el))
) as HTMLLinkElement[]).forEach((el) => {
const currentHref = el.getAttribute('href')
const relocateIndex = relocateHrefs.indexOf(currentHref)
if (relocateIndex !== -1) {
const placeholderElement = relocatePlaceholders[relocateIndex]
placeholderElement.parentNode?.replaceChild(el, placeholderElement)
} else {
el.parentNode!.removeChild(el)
}
})
// Activate new stylesheets:
;[].slice
......
......@@ -94,17 +94,17 @@ describe('Build Output', () => {
expect(parseFloat(indexSize) - 265).toBeLessThanOrEqual(0)
expect(indexSize.endsWith('B')).toBe(true)
// should be no bigger than 60.1 kb
expect(parseFloat(indexFirstLoad) - 60.1).toBeLessThanOrEqual(0)
// should be no bigger than 60.2 kb
expect(parseFloat(indexFirstLoad) - 60.2).toBeLessThanOrEqual(0)
expect(indexFirstLoad.endsWith('kB')).toBe(true)
expect(parseFloat(err404Size) - 3.8).toBeLessThanOrEqual(0)
expect(parseFloat(err404Size) - 3.5).toBeLessThanOrEqual(0)
expect(err404Size.endsWith('kB')).toBe(true)
expect(parseFloat(err404FirstLoad) - 63.3).toBeLessThanOrEqual(0)
expect(parseFloat(err404FirstLoad) - 63.4).toBeLessThanOrEqual(0)
expect(err404FirstLoad.endsWith('kB')).toBe(true)
expect(parseFloat(sharedByAll) - 59.8).toBeLessThanOrEqual(0)
expect(parseFloat(sharedByAll) - 59.9).toBeLessThanOrEqual(0)
expect(sharedByAll.endsWith('kB')).toBe(true)
if (_appSize.endsWith('kB')) {
......@@ -118,7 +118,7 @@ describe('Build Output', () => {
expect(parseFloat(webpackSize) - 752).toBeLessThanOrEqual(0)
expect(webpackSize.endsWith(' B')).toBe(true)
expect(parseFloat(mainSize) - 7).toBeLessThanOrEqual(0)
expect(parseFloat(mainSize) - 7.1).toBeLessThanOrEqual(0)
expect(mainSize.endsWith('kB')).toBe(true)
expect(parseFloat(frameworkSize) - 41).toBeLessThanOrEqual(0)
......
......@@ -80,7 +80,7 @@ describe('Production response size', () => {
)
// These numbers are without gzip compression!
const delta = responseSizesBytes - 277 * 1024
const delta = responseSizesBytes - 278 * 1024
expect(delta).toBeLessThanOrEqual(1024) // don't increase size more than 1kb
expect(delta).toBeGreaterThanOrEqual(-1024) // don't decrease size more than 1kb without updating target
})
......@@ -100,7 +100,7 @@ describe('Production response size', () => {
)
// These numbers are without gzip compression!
const delta = responseSizesBytes - 168 * 1024
const delta = responseSizesBytes - 169 * 1024
expect(delta).toBeLessThanOrEqual(1024) // don't increase size more than 1kb
expect(delta).toBeGreaterThanOrEqual(-1024) // don't decrease size more than 1kb without updating target
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment