Unverified Commit 5a3d558c authored by Javi Velasco's avatar Javi Velasco Committed by GitHub

Fix HMR for middleware #30791 (#31548)

Fixes #30791

The issue is that with Middleware we are introducing client compilation on a new layer. When a middleware changes (or is dropped after some time), Webpack reorganizes non-user modules as they are duplicated across different layer. This is currently triggering a full reload.

This also brings tests for HMR:
- Refresh on a Middleware change
- HMR works after a middleware compilation change.
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 01cf08e2
...@@ -957,6 +957,11 @@ export default async function getBaseWebpackConfig( ...@@ -957,6 +957,11 @@ export default async function getBaseWebpackConfig(
}, },
} }
const nonUserCondition = {
include: /node_modules/,
exclude: babelIncludeRegexes,
}
let webpackConfig: webpack.Configuration = { let webpackConfig: webpack.Configuration = {
parallelism: Number(process.env.NEXT_WEBPACK_PARALLELISM) || undefined, parallelism: Number(process.env.NEXT_WEBPACK_PARALLELISM) || undefined,
externals: targetWeb externals: targetWeb
...@@ -1250,6 +1255,13 @@ export default async function getBaseWebpackConfig( ...@@ -1250,6 +1255,13 @@ export default async function getBaseWebpackConfig(
}, },
], ],
}, },
{
...nonUserCondition,
// Make all non-user modules to be compiled in a single layer
// This avoids compiling them mutliple times and avoids module id changes
issuerLayer: 'middleware',
layer: '',
},
...(!config.images.disableStaticImages ...(!config.images.disableStaticImages
? [ ? [
{ {
......
import { NextResponse } from 'next/server'
export function middleware() {
return NextResponse.next(null, {
headers: {
'x-test-header': 'just a header',
},
})
}
import { NextResponse } from 'next/server'
export function middleware() {
return NextResponse.rewrite('/about/a')
}
export default function AboutA() {
return (
<div>
<h1>AboutA</h1>
</div>
)
}
export default function AboutB() {
return (
<div>
<h1>AboutB</h1>
</div>
)
}
export default function Home() {
return (
<div>
<h1>Home</h1>
</div>
)
}
/* eslint-env jest */
import { join } from 'path'
import fs from 'fs'
import webdriver from 'next-webdriver'
import {
fetchViaHTTP,
findPort,
killApp,
launchApp,
waitFor,
} from 'next-test-utils'
jest.setTimeout(1000 * 60 * 2)
const context = {}
context.appDir = join(__dirname, '../')
describe('HMR with middleware', () => {
let output = ''
beforeAll(async () => {
context.appPort = await findPort()
context.app = await launchApp(context.appDir, context.appPort, {
onStdout(msg) {
output += msg
},
onStderr(msg) {
output += msg
},
})
})
afterAll(() => killApp(context.app))
it('works for pages when middleware is compiled', async () => {
const browser = await webdriver(context.appPort, `/`)
try {
await browser.eval('window.itdidnotrefresh = "hello"')
await fetchViaHTTP(context.appPort, `/about`)
await waitFor(1000)
expect(output.includes('about/_middleware')).toEqual(true)
expect(await browser.eval('window.itdidnotrefresh')).toBe('hello')
} finally {
await browser.close()
}
})
it('refreshes the page when middleware changes ', async () => {
const browser = await webdriver(context.appPort, `/about`)
await browser.eval('window.didrefresh = "hello"')
const text = await browser.elementByCss('h1').text()
expect(text).toEqual('AboutA')
const middlewarePath = join(context.appDir, '/pages/about/_middleware.js')
const originalContent = fs.readFileSync(middlewarePath, 'utf-8')
const editedContent = originalContent.replace('/about/a', '/about/b')
try {
fs.writeFileSync(middlewarePath, editedContent)
await waitFor(1000)
const textb = await browser.elementByCss('h1').text()
expect(await browser.eval('window.itdidnotrefresh')).not.toBe('hello')
expect(textb).toEqual('AboutB')
} finally {
fs.writeFileSync(middlewarePath, originalContent)
await browser.close()
}
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment