mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
Move webpack idle wait code to the page-loader.
Because that's the place to do it.
This commit is contained in:
parent
751d2e9853
commit
4f26e84ff2
|
@ -1,6 +1,8 @@
|
|||
/* global window, document */
|
||||
import mitt from 'mitt'
|
||||
|
||||
const webpackModule = module
|
||||
|
||||
export default class PageLoader {
|
||||
constructor (buildId) {
|
||||
this.buildId = buildId
|
||||
|
@ -18,19 +20,6 @@ export default class PageLoader {
|
|||
return route.replace(/index$/, '')
|
||||
}
|
||||
|
||||
loadPageSync (route) {
|
||||
route = this.normalizeRoute(route)
|
||||
const cachedPage = this.pageCache[route]
|
||||
|
||||
if (!cachedPage) {
|
||||
return null
|
||||
} else if (cachedPage.error) {
|
||||
throw cachedPage.error
|
||||
} else {
|
||||
return cachedPage.page
|
||||
}
|
||||
}
|
||||
|
||||
loadPage (route) {
|
||||
route = this.normalizeRoute(route)
|
||||
|
||||
|
@ -79,12 +68,32 @@ export default class PageLoader {
|
|||
}
|
||||
|
||||
// This method if called by the route code.
|
||||
registerPage (route, error, page) {
|
||||
route = this.normalizeRoute(route)
|
||||
registerPage (route, regFn) {
|
||||
const register = () => {
|
||||
regFn((error, page) => {
|
||||
route = this.normalizeRoute(route)
|
||||
|
||||
// add the page to the cache
|
||||
this.pageCache[route] = { error, page }
|
||||
this.registerEvents.emit(route, { error, page })
|
||||
// add the page to the cache
|
||||
this.pageCache[route] = { error, page }
|
||||
this.registerEvents.emit(route, { error, page })
|
||||
})
|
||||
}
|
||||
|
||||
// Wait for webpack to became idle if it's not.
|
||||
// More info: https://github.com/zeit/next.js/pull/1511
|
||||
if (webpackModule && webpackModule.hot && webpackModule.hot.status() !== 'idle') {
|
||||
console.log(`Waiting webpack to became "idle" to initialize the page: "${route}"`)
|
||||
|
||||
const check = (status) => {
|
||||
if (status === 'idle') {
|
||||
webpackModule.hot.removeStatusHandler(check)
|
||||
register()
|
||||
}
|
||||
}
|
||||
webpackModule.hot.status(check)
|
||||
} else {
|
||||
register()
|
||||
}
|
||||
}
|
||||
|
||||
clearCache (route) {
|
||||
|
|
|
@ -5,8 +5,6 @@ import PQueue from '../p-queue'
|
|||
import { loadGetInitialProps, getURL } from '../utils'
|
||||
import { _notifyBuildIdMismatch } from './'
|
||||
|
||||
const webpackModule = module
|
||||
|
||||
export default class Router {
|
||||
constructor (pathname, query, as, { pageLoader, Component, ErrorComponent, err } = {}) {
|
||||
// represents the current component key
|
||||
|
@ -320,20 +318,6 @@ export default class Router {
|
|||
}
|
||||
|
||||
async fetchRoute (route) {
|
||||
// Wait for webpack to became idle if it's not.
|
||||
// More info: https://github.com/zeit/next.js/pull/1511
|
||||
if (webpackModule && webpackModule.hot && webpackModule.hot.status() !== 'idle') {
|
||||
await new Promise((resolve) => {
|
||||
const check = (status) => {
|
||||
if (status === 'idle') {
|
||||
webpackModule.hot.removeStatusHandler(check)
|
||||
resolve()
|
||||
}
|
||||
}
|
||||
webpackModule.hot.status(check)
|
||||
})
|
||||
}
|
||||
|
||||
return await this.pageLoader.loadPage(route)
|
||||
}
|
||||
|
||||
|
|
|
@ -17,8 +17,10 @@ export default class PagesPlugin {
|
|||
const content = page.source()
|
||||
const newContent = `
|
||||
function loadPage () {
|
||||
var comp = ${content}
|
||||
window.__NEXT_PAGE_LOADER__.registerPage('${routeName}', null, comp.default)
|
||||
window.__NEXT_PAGE_LOADER__.registerPage('${routeName}', function(cb) {
|
||||
var comp = ${content}
|
||||
cb(null, comp.default)
|
||||
})
|
||||
}
|
||||
|
||||
if (window.__NEXT_PAGE_LOADER__) {
|
||||
|
|
|
@ -123,7 +123,9 @@ export async function renderScriptError (req, res, page, error, customFields, op
|
|||
var error = new Error('Page not exists: ${page}')
|
||||
error.pageNotFound = true
|
||||
error.statusCode = 404
|
||||
__NEXT_PAGE_LOADER__.registerPage('${page}', error)
|
||||
__NEXT_PAGE_LOADER__.registerPage('${page}', function(cb) {
|
||||
cb(error)
|
||||
})
|
||||
}
|
||||
|
||||
if (window.__NEXT_PAGE_LOADER__) {
|
||||
|
@ -145,7 +147,9 @@ export async function renderScriptError (req, res, page, error, customFields, op
|
|||
res.end(`
|
||||
function loadPage () {
|
||||
var error = ${JSON.stringify(errorJson)}
|
||||
__NEXT_PAGE_LOADER__.registerPage('${page}', error)
|
||||
__NEXT_PAGE_LOADER__.registerPage('${page}', function(cb) {
|
||||
cb(error)
|
||||
})
|
||||
}
|
||||
|
||||
if (window.__NEXT_PAGE_LOADER__) {
|
||||
|
|
Loading…
Reference in a new issue