Skip to content
Closed
Changes from all 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
22 changes: 22 additions & 0 deletions packages/next/client/page-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ export default class PageLoader {
!document.querySelector(`script[src^="${d}"]`)
) {
this.loadScript(d, route, false)
} else if (
/\.css$/.test(d) &&
!document.querySelector(`link[href^="${d}"]`)
) {
this.loadStylesheet(d)
}
})
this.loadRoute(route)
Expand All @@ -126,6 +131,14 @@ export default class PageLoader {
this.loadScript(url, route, true)
}

loadStylesheet(url) {
const link = document.createElement('link')
link.rel = 'stylesheet'
link.href = encodeURI(url)
link.as = 'style'
document.head.appendChild(link)
}

loadScript(url, route, isPage) {
const script = document.createElement('script')
if (process.env.__NEXT_MODERN_BUILD && 'noModule' in script) {
Expand Down Expand Up @@ -182,6 +195,15 @@ export default class PageLoader {

async prefetch(route, isDependency) {
route = this.normalizeRoute(route)

if (
/\.css$/.test(route) &&
!document.querySelector(`link[href^="${route}"]`)
) {
this.loadStylesheet(route)
Copy link
Member

@Timer Timer Nov 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefetching should be done via a real prefetch, i.e.:

<link rel="prefetch" href="/style.css" as="style" />

We probably need two different versions of the loadStylesheet method, similar to what we do for JS.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I've tried both preload and prefetch, but the browser (Safari) doesn't work as expected (checked preload/prefetch support before), so the only way to "preload" the CSS files would be the current loadStylesheet method.
Alternatively, we could just return blank on prefetching CSS and simply load it on page transition which seems to work without any FOUC or sth.

return
}

let scriptRoute = `${route === '/' ? '/index' : route}.js`

if (
Expand Down