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

Move client/index.js into DLL bundle so that it’s compiled once #5173

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion build/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export default async function getBaseWebpackConfig (dir: string, {dev = false, i
'main.js': [],
[CLIENT_STATIC_FILES_RUNTIME_MAIN]: [
path.join(NEXT_PROJECT_ROOT_DIST, 'client', (dev ? `next-dev` : 'next'))
].filter(Boolean)
]
} : {}

const resolveConfig = {
Expand Down Expand Up @@ -226,6 +226,7 @@ export default async function getBaseWebpackConfig (dir: string, {dev = false, i
context: dir,
entry: {
dll: [
path.join(NEXT_PROJECT_ROOT_DIST, 'client', 'index'),
'react',
'react-dom'
]
Expand Down
12 changes: 2 additions & 10 deletions client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import HeadManager from './head-manager'
import { createRouter } from '../lib/router'
import EventEmitter from '../lib/EventEmitter'
import { loadGetInitialProps, getURL } from '../lib/utils'
import PageLoader from '../lib/page-loader'
import * as asset from '../lib/asset'
import * as envConfig from '../lib/runtime-config'
import ErrorBoundary from './error-boundary'
Expand All @@ -26,7 +25,6 @@ const {
page,
pathname,
query,
buildId,
assetPrefix,
runtimeConfig
},
Expand All @@ -48,13 +46,6 @@ envConfig.setConfig({

const asPath = getURL()

const pageLoader = new PageLoader(buildId, prefix)
window.__NEXT_LOADED_PAGES__.forEach(({ route, fn }) => {
pageLoader.registerPage(route, fn)
})
delete window.__NEXT_LOADED_PAGES__
window.__NEXT_REGISTER_PAGE = pageLoader.registerPage.bind(pageLoader)

const headManager = new HeadManager()
const appContainer = document.getElementById('__next')

Expand All @@ -68,7 +59,8 @@ let App
export const emitter = new EventEmitter()

export default async ({
webpackHMR: passedWebpackHMR
webpackHMR: passedWebpackHMR,
pageLoader
} = {}) => {
// This makes sure this specific line is removed in production
if (process.env.NODE_ENV === 'development') {
Expand Down
22 changes: 14 additions & 8 deletions client/next-dev.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
import initNext, * as next from './'
import initOnDemandEntries from './on-demand-entries-client'
import initWebpackHMR from './webpack-hot-middleware-client'
import {initPageLoader} from './page-loader'

const {
buildId,
assetPrefix
} = window.__NEXT_DATA__
const prefix = assetPrefix || ''

// With dynamic assetPrefix it's no longer possible to set assetPrefix at the build time
// So, this is how we do it in the client side at runtime
__webpack_public_path__ = `${prefix}/_next/` //eslint-disable-line

// Temporary workaround for the issue described here:
// https://github.com/zeit/next.js/issues/3775#issuecomment-407438123
// The runtimeChunk doesn't have dynamic import handling code when there hasn't been a dynamic import
// The runtimeChunk can't hot reload itself currently to correct it when adding pages using on-demand-entries
import('./noop')

const {
__NEXT_DATA__: {
assetPrefix
}
} = window

const prefix = assetPrefix || ''
const webpackHMR = initWebpackHMR({assetPrefix: prefix})
const pageLoader = initPageLoader({buildId, assetPrefix: prefix})

window.next = next
initNext({ webpackHMR })

initNext({ webpackHMR, pageLoader })
.then((emitter) => {
initOnDemandEntries({assetPrefix: prefix})

Expand Down
9 changes: 8 additions & 1 deletion client/next.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import initNext, * as next from './'
import {initPageLoader} from './page-loader'
const {
buildId,
assetPrefix
} = window.__NEXT_DATA__

window.next = next
const prefix = assetPrefix || ''
const pageLoader = initPageLoader({buildId, prefix})

initNext()
initNext({pageLoader})
.catch((err) => {
console.error(`${err.message}\n${err.stack}`)
})
11 changes: 11 additions & 0 deletions client/page-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import PageLoader from '../lib/page-loader'

export function initPageLoader ({buildId, assetPrefix}) {
const pageLoader = new PageLoader(buildId, assetPrefix)
window.__NEXT_LOADED_PAGES__.forEach(({ route, fn }) => {
pageLoader.registerPage(route, fn)
})
delete window.__NEXT_LOADED_PAGES__
window.__NEXT_REGISTER_PAGE = pageLoader.registerPage.bind(pageLoader)
return pageLoader
}