Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not rely on cssText #16611

Merged
merged 4 commits into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 1 addition & 18 deletions packages/next/client/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,24 +86,7 @@ type RegisterFn = (input: [string, () => void]) => void

const looseToArray = <T extends {}>(input: any): T[] => [].slice.call(input)

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 pageLoader = new PageLoader(buildId, prefix, page)
const register: RegisterFn = ([r, f]) => pageLoader.registerPage(r, f)
if (window.__NEXT_P) {
// Defer page registration for another tick. This will increase the overall
Expand Down
32 changes: 15 additions & 17 deletions packages/next/client/page-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ export type PageCacheEntry = { error: any } | GoodPageCache

export default class PageLoader {
private initialPage: string
private initialStyleSheets: StyleSheetTuple[]
private buildId: string
private assetPrefix: string
private pageCache: Record<string, PageCacheEntry>
Expand All @@ -109,14 +108,8 @@ export default class PageLoader {
private promisedSsgManifest?: Promise<ClientSsgManifest>
private promisedDevPagesManifest?: Promise<any>

constructor(
buildId: string,
assetPrefix: string,
initialPage: string,
initialStyleSheets: StyleSheetTuple[]
) {
constructor(buildId: string, assetPrefix: string, initialPage: string) {
this.initialPage = initialPage
this.initialStyleSheets = initialStyleSheets

this.buildId = buildId
this.assetPrefix = assetPrefix
Expand Down Expand Up @@ -428,17 +421,22 @@ export default class PageLoader {
// We use `style-loader` in development:
process.env.NODE_ENV !== 'production'
? Promise.resolve([])
: route === this.initialPage
? Promise.resolve(this.initialStyleSheets)
: // Tests that this does not block hydration:
// test/integration/css-fixtures/hydrate-without-deps/
this.getDependencies(route)
.then((deps) => deps.filter((d) => d.endsWith('.css')))
.then((cssFiles) =>
// These files should've already been fetched by now, so this
// should resolve pretty much instantly.
Promise.all(cssFiles.map((d) => fetchStyleSheet(d)))
)
(route === this.initialPage
? Promise.resolve(
([].slice.call(
document.querySelectorAll('link[data-n-p]')
) as HTMLLinkElement[]).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
// should resolve instantly.
Promise.all(cssFiles.map((d) => fetchStyleSheet(d)))
)
promisedDeps.then(
(deps) => register(deps),
(error) => {
Expand Down