Unverified Commit 328abb72 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Do not rely on `cssText` (#16611)

parent 42e8dcf2
...@@ -17,7 +17,7 @@ import * as envConfig from '../next-server/lib/runtime-config' ...@@ -17,7 +17,7 @@ import * as envConfig from '../next-server/lib/runtime-config'
import { getURL, loadGetInitialProps, ST } from '../next-server/lib/utils' import { getURL, loadGetInitialProps, ST } from '../next-server/lib/utils'
import type { NEXT_DATA } from '../next-server/lib/utils' import type { NEXT_DATA } from '../next-server/lib/utils'
import initHeadManager from './head-manager' import initHeadManager from './head-manager'
import PageLoader, { StyleSheetTuple } from './page-loader' import PageLoader, { looseToArray, StyleSheetTuple } from './page-loader'
import measureWebVitals from './performance-relayer' import measureWebVitals from './performance-relayer'
import { createRouter, makePublicRouterInstance } from './router' import { createRouter, makePublicRouterInstance } from './router'
...@@ -84,26 +84,7 @@ if (hasBasePath(asPath)) { ...@@ -84,26 +84,7 @@ if (hasBasePath(asPath)) {
type RegisterFn = (input: [string, () => void]) => void type RegisterFn = (input: [string, () => void]) => void
const looseToArray = <T extends {}>(input: any): T[] => [].slice.call(input) const pageLoader = new PageLoader(buildId, prefix, page)
const pageLoader = new PageLoader(
buildId,
prefix,
page,
looseToArray<CSSStyleSheet>(document.styleSheets)
.filter(
(el: CSSStyleSheet) =>
el.ownerNode &&
(el.ownerNode as Element).tagName === 'LINK' &&
(el.ownerNode as Element).hasAttribute('data-n-p')
)
.map((sheet) => ({
href: (sheet.ownerNode as Element).getAttribute('href')!,
text: looseToArray<CSSRule>(sheet.cssRules)
.map((r) => r.cssText)
.join(''),
}))
)
const register: RegisterFn = ([r, f]) => pageLoader.registerPage(r, f) const register: RegisterFn = ([r, f]) => pageLoader.registerPage(r, f)
if (window.__NEXT_P) { if (window.__NEXT_P) {
// Defer page registration for another tick. This will increase the overall // Defer page registration for another tick. This will increase the overall
......
...@@ -12,6 +12,25 @@ import { searchParamsToUrlQuery } from '../next-server/lib/router/utils/querystr ...@@ -12,6 +12,25 @@ import { searchParamsToUrlQuery } from '../next-server/lib/router/utils/querystr
import { getRouteMatcher } from '../next-server/lib/router/utils/route-matcher' import { getRouteMatcher } from '../next-server/lib/router/utils/route-matcher'
import { getRouteRegex } from '../next-server/lib/router/utils/route-regex' import { getRouteRegex } from '../next-server/lib/router/utils/route-regex'
export const looseToArray = <T extends {}>(input: any): T[] =>
[].slice.call(input)
function getInitialStylesheets(): StyleSheetTuple[] {
return looseToArray<CSSStyleSheet>(document.styleSheets)
.filter(
(el: CSSStyleSheet) =>
el.ownerNode &&
(el.ownerNode as Element).tagName === 'LINK' &&
(el.ownerNode as Element).hasAttribute('data-n-p')
)
.map((sheet) => ({
href: (sheet.ownerNode as Element).getAttribute('href')!,
text: looseToArray<CSSRule>(sheet.cssRules)
.map((r) => r.cssText)
.join(''),
}))
}
function hasRel(rel: string, link?: HTMLLinkElement) { function hasRel(rel: string, link?: HTMLLinkElement) {
try { try {
link = document.createElement('link') link = document.createElement('link')
...@@ -99,7 +118,6 @@ export type PageCacheEntry = { error: any } | GoodPageCache ...@@ -99,7 +118,6 @@ export type PageCacheEntry = { error: any } | GoodPageCache
export default class PageLoader { export default class PageLoader {
private initialPage: string private initialPage: string
private initialStyleSheets: StyleSheetTuple[]
private buildId: string private buildId: string
private assetPrefix: string private assetPrefix: string
private pageCache: Record<string, PageCacheEntry> private pageCache: Record<string, PageCacheEntry>
...@@ -109,14 +127,8 @@ export default class PageLoader { ...@@ -109,14 +127,8 @@ export default class PageLoader {
private promisedSsgManifest?: Promise<ClientSsgManifest> private promisedSsgManifest?: Promise<ClientSsgManifest>
private promisedDevPagesManifest?: Promise<any> private promisedDevPagesManifest?: Promise<any>
constructor( constructor(buildId: string, assetPrefix: string, initialPage: string) {
buildId: string,
assetPrefix: string,
initialPage: string,
initialStyleSheets: StyleSheetTuple[]
) {
this.initialPage = initialPage this.initialPage = initialPage
this.initialStyleSheets = initialStyleSheets
this.buildId = buildId this.buildId = buildId
this.assetPrefix = assetPrefix this.assetPrefix = assetPrefix
...@@ -422,22 +434,33 @@ export default class PageLoader { ...@@ -422,22 +434,33 @@ export default class PageLoader {
}) })
} }
const isInitialLoad = route === this.initialPage
const promisedDeps: Promise<StyleSheetTuple[]> = const promisedDeps: Promise<StyleSheetTuple[]> =
// Shared styles will already be on the page: // Shared styles will already be on the page:
route === '/_app' || route === '/_app' ||
// We use `style-loader` in development: // We use `style-loader` in development:
process.env.NODE_ENV !== 'production' process.env.NODE_ENV !== 'production'
? Promise.resolve([]) ? Promise.resolve([])
: route === this.initialPage
? Promise.resolve(this.initialStyleSheets)
: // Tests that this does not block hydration: : // Tests that this does not block hydration:
// test/integration/css-fixtures/hydrate-without-deps/ // test/integration/css-fixtures/hydrate-without-deps/
this.getDependencies(route) (isInitialLoad
.then((deps) => deps.filter((d) => d.endsWith('.css'))) ? Promise.resolve(
.then((cssFiles) => looseToArray<HTMLLinkElement>(
document.querySelectorAll('link[data-n-p]')
).map((e) => e.getAttribute('href')!)
)
: this.getDependencies(route).then((deps) =>
deps.filter((d) => d.endsWith('.css'))
)
).then((cssFiles) =>
// These files should've already been fetched by now, so this // These files should've already been fetched by now, so this
// should resolve pretty much instantly. // should resolve instantly.
Promise.all(cssFiles.map((d) => fetchStyleSheet(d))) Promise.all(cssFiles.map((d) => fetchStyleSheet(d))).catch(
(err) => {
if (isInitialLoad) return getInitialStylesheets()
throw err
}
)
) )
promisedDeps.then( promisedDeps.then(
(deps) => register(deps), (deps) => register(deps),
......
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