Skip to content

Commit

Permalink
refactor(next-client-pages-loader): allow toggling router HMR (#11989)
Browse files Browse the repository at this point in the history
* refactor(next-client-pages-loader): allow toggling router HMR

* Turn on

* Toggle dev properly

* Fix Test Run Order
  • Loading branch information
Timer authored Apr 18, 2020
1 parent bc12794 commit 55ffb96
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 23 deletions.
2 changes: 2 additions & 0 deletions packages/next/build/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type Entrypoints = {
}

export function createEntrypoints(
dev: boolean,
pages: PagesMapping,
target: 'server' | 'serverless' | 'experimental-serverless-trace',
buildId: string,
Expand Down Expand Up @@ -132,6 +133,7 @@ export function createEntrypoints(
const pageLoaderOpts: ClientPagesLoaderOptions = {
page,
absolutePagePath,
hotRouterUpdates: dev, // Hot router updates only apply in development mode
}
const pageLoader = `next-client-pages-loader?${stringify(
pageLoaderOpts
Expand Down
1 change: 1 addition & 0 deletions packages/next/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ export default async function build(dir: string, conf = null): Promise<void> {

const mappedPages = createPagesMapping(pagePaths, config.pageExtensions)
const entrypoints = createEntrypoints(
/* dev */ false,
mappedPages,
target,
buildId,
Expand Down
28 changes: 17 additions & 11 deletions packages/next/build/webpack/loaders/next-client-pages-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,31 @@ import loaderUtils from 'loader-utils'
export type ClientPagesLoaderOptions = {
absolutePagePath: string
page: string
hotRouterUpdates: boolean
}

const nextClientPagesLoader: loader.Loader = function() {
const { absolutePagePath, page }: any = loaderUtils.getOptions(this)
const { absolutePagePath, page, hotRouterUpdates } = loaderUtils.getOptions(
this
) as ClientPagesLoaderOptions
const stringifiedAbsolutePagePath = JSON.stringify(absolutePagePath)
const stringifiedPage = JSON.stringify(page)

return `
(window.__NEXT_P=window.__NEXT_P||[]).push([${stringifiedPage}, function() {
var mod = require(${stringifiedAbsolutePagePath})
if(module.hot) {
module.hot.accept(${stringifiedAbsolutePagePath}, function() {
if(!next.router.components[${stringifiedPage}]) return
var updatedPage = require(${stringifiedAbsolutePagePath})
next.router.update(${stringifiedPage}, updatedPage)
})
(window.__NEXT_P = window.__NEXT_P || []).push([
${stringifiedPage},
function () {
var mod = require(${stringifiedAbsolutePagePath});
if (${!!hotRouterUpdates} && module.hot) {
module.hot.accept(${stringifiedAbsolutePagePath}, function () {
if (!next.router.components[${stringifiedPage}]) return;
var updatedPage = require(${stringifiedAbsolutePagePath});
next.router.update(${stringifiedPage}, updatedPage);
});
}
return mod;
}
return mod
}]);
]);
`
}

Expand Down
2 changes: 2 additions & 0 deletions packages/next/server/hot-reloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ export default class HotReloader {
this.config.pageExtensions
)
const entrypoints = createEntrypoints(
/* dev */ true,
pages,
'server',
this.buildId,
Expand Down Expand Up @@ -488,6 +489,7 @@ export default class HotReloader {
pagesDir: this.pagesDir,
reload: this.reload.bind(this),
pageExtensions: this.config.pageExtensions,
hotRouterUpdates: true,
...(this.config.onDemandEntries as {
maxInactiveAge: number
pagesBufferLength: number
Expand Down
3 changes: 3 additions & 0 deletions packages/next/server/on-demand-entry-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ export default function onDemandEntryHandler(
pageExtensions,
maxInactiveAge,
pagesBufferLength,
hotRouterUpdates,
}: {
buildId: string
pagesDir: string
reload: any
pageExtensions: string[]
maxInactiveAge: number
pagesBufferLength: number
hotRouterUpdates: boolean
}
) {
const { compilers } = multiCompiler
Expand Down Expand Up @@ -88,6 +90,7 @@ export default function onDemandEntryHandler(
const pageLoaderOpts: ClientPagesLoaderOptions = {
page,
absolutePagePath,
hotRouterUpdates,
}
return addEntry(compilation, compiler.context, name, [
compiler.name === 'client'
Expand Down
24 changes: 12 additions & 12 deletions test/integration/production-config/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ describe('Production Config Usage', () => {
})
})

describe('with generateBuildId', () => {
it('should add the custom buildid', async () => {
const browser = await webdriver(appPort, '/')
const text = await browser.elementByCss('#mounted').text()
expect(text).toMatch(/ComponentDidMount executed on client\./)

const html = await browser.elementByCss('html').getAttribute('innerHTML')
expect(html).toMatch('custom-buildid')
await browser.close()
})
})

describe('env', () => {
it('should fail with leading __ in env key', async () => {
const result = await runNextCommand(['build', appDir], {
Expand Down Expand Up @@ -70,18 +82,6 @@ describe('Production Config Usage', () => {
expect(result.stderr).not.toMatch(/The key "SOME__ENV__VAR" under/)
})
})

describe('with generateBuildId', () => {
it('should add the custom buildid', async () => {
const browser = await webdriver(appPort, '/')
const text = await browser.elementByCss('#mounted').text()
expect(text).toMatch(/ComponentDidMount executed on client\./)

const html = await browser.elementByCss('html').getAttribute('innerHTML')
expect(html).toMatch('custom-buildid')
await browser.close()
})
})
})

async function testBrowser() {
Expand Down

0 comments on commit 55ffb96

Please sign in to comment.