@@ -12,6 +12,25 @@ import { searchParamsToUrlQuery } from '../next-server/lib/router/utils/querystr
1212import { getRouteMatcher } from '../next-server/lib/router/utils/route-matcher'
1313import { getRouteRegex } from '../next-server/lib/router/utils/route-regex'
1414
15+ export const looseToArray = < T extends { } > ( input : any ) : T [ ] =>
16+ [ ] . slice . call ( input )
17+
18+ function getInitialStylesheets ( ) : StyleSheetTuple [ ] {
19+ return looseToArray < CSSStyleSheet > ( document . styleSheets )
20+ . filter (
21+ ( el : CSSStyleSheet ) =>
22+ el . ownerNode &&
23+ ( el . ownerNode as Element ) . tagName === 'LINK' &&
24+ ( el . ownerNode as Element ) . hasAttribute ( 'data-n-p' )
25+ )
26+ . map ( ( sheet ) => ( {
27+ href : ( sheet . ownerNode as Element ) . getAttribute ( 'href' ) ! ,
28+ text : looseToArray < CSSRule > ( sheet . cssRules )
29+ . map ( ( r ) => r . cssText )
30+ . join ( '' ) ,
31+ } ) )
32+ }
33+
1534function hasRel ( rel : string , link ?: HTMLLinkElement ) {
1635 try {
1736 link = document . createElement ( 'link' )
@@ -99,7 +118,6 @@ export type PageCacheEntry = { error: any } | GoodPageCache
99118
100119export default class PageLoader {
101120 private initialPage : string
102- private initialStyleSheets : StyleSheetTuple [ ]
103121 private buildId : string
104122 private assetPrefix : string
105123 private pageCache : Record < string , PageCacheEntry >
@@ -109,14 +127,8 @@ export default class PageLoader {
109127 private promisedSsgManifest ?: Promise < ClientSsgManifest >
110128 private promisedDevPagesManifest ?: Promise < any >
111129
112- constructor (
113- buildId : string ,
114- assetPrefix : string ,
115- initialPage : string ,
116- initialStyleSheets : StyleSheetTuple [ ]
117- ) {
130+ constructor ( buildId : string , assetPrefix : string , initialPage : string ) {
118131 this . initialPage = initialPage
119- this . initialStyleSheets = initialStyleSheets
120132
121133 this . buildId = buildId
122134 this . assetPrefix = assetPrefix
@@ -422,23 +434,34 @@ export default class PageLoader {
422434 } )
423435 }
424436
437+ const isInitialLoad = route === this . initialPage
425438 const promisedDeps : Promise < StyleSheetTuple [ ] > =
426439 // Shared styles will already be on the page:
427440 route === '/_app' ||
428441 // We use `style-loader` in development:
429442 process . env . NODE_ENV !== 'production'
430443 ? Promise . resolve ( [ ] )
431- : route === this . initialPage
432- ? Promise . resolve ( this . initialStyleSheets )
433444 : // Tests that this does not block hydration:
434445 // test/integration/css-fixtures/hydrate-without-deps/
435- this . getDependencies ( route )
436- . then ( ( deps ) => deps . filter ( ( d ) => d . endsWith ( '.css' ) ) )
437- . then ( ( cssFiles ) =>
438- // These files should've already been fetched by now, so this
439- // should resolve pretty much instantly.
440- Promise . all ( cssFiles . map ( ( d ) => fetchStyleSheet ( d ) ) )
446+ ( isInitialLoad
447+ ? Promise . resolve (
448+ looseToArray < HTMLLinkElement > (
449+ document . querySelectorAll ( 'link[data-n-p]' )
450+ ) . map ( ( e ) => e . getAttribute ( 'href' ) ! )
451+ )
452+ : this . getDependencies ( route ) . then ( ( deps ) =>
453+ deps . filter ( ( d ) => d . endsWith ( '.css' ) )
454+ )
455+ ) . then ( ( cssFiles ) =>
456+ // These files should've already been fetched by now, so this
457+ // should resolve instantly.
458+ Promise . all ( cssFiles . map ( ( d ) => fetchStyleSheet ( d ) ) ) . catch (
459+ ( err ) => {
460+ if ( isInitialLoad ) return getInitialStylesheets ( )
461+ throw err
462+ }
441463 )
464+ )
442465 promisedDeps . then (
443466 ( deps ) => register ( deps ) ,
444467 ( error ) => {
0 commit comments