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

refactor(next-client-pages-loader): allow toggling router HMR #11989

Merged
merged 4 commits into from
Apr 18, 2020
Merged
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
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