-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix HMR when custom _app or _document is removed (#28227)
This adds the fallback webpack alias handling to handle a custom `_app` or `_document` being removed in development gracefully. ## Bug - [x] Related issues linked using `fixes #number` - [x] Integration tests added - [x] Errors have helpful link attached, see `contributing.md` Fixes: #27888
- Loading branch information
Showing
5 changed files
with
205 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export default function MyApp({ Component, pageProps }) { | ||
return ( | ||
<> | ||
<p>custom _app</p> | ||
<Component {...pageProps} /> | ||
</> | ||
) | ||
} |
23 changes: 23 additions & 0 deletions
23
test/integration/app-document-remove-hmr/pages/_document.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import Document, { Html, Head, Main, NextScript } from 'next/document' | ||
|
||
class MyDocument extends Document { | ||
static async getInitialProps(ctx) { | ||
const initialProps = await Document.getInitialProps(ctx) | ||
return { ...initialProps } | ||
} | ||
|
||
render() { | ||
return ( | ||
<Html> | ||
<Head /> | ||
<body> | ||
<p>custom _document</p> | ||
<Main /> | ||
<NextScript /> | ||
</body> | ||
</Html> | ||
) | ||
} | ||
} | ||
|
||
export default MyDocument |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function Page() { | ||
return <p>index page</p> | ||
} |
128 changes: 128 additions & 0 deletions
128
test/integration/app-document-remove-hmr/test/index.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* eslint-env jest */ | ||
|
||
import fs from 'fs-extra' | ||
import { join } from 'path' | ||
import webdriver from 'next-webdriver' | ||
import { killApp, findPort, launchApp, check } from 'next-test-utils' | ||
|
||
jest.setTimeout(1000 * 60 * 2) | ||
|
||
const appDir = join(__dirname, '../') | ||
const appPage = join(appDir, 'pages/_app.js') | ||
const indexPage = join(appDir, 'pages/index.js') | ||
const documentPage = join(appDir, 'pages/_document.js') | ||
|
||
let appPort | ||
let app | ||
|
||
describe('_app removal HMR', () => { | ||
beforeAll(async () => { | ||
appPort = await findPort() | ||
app = await launchApp(appDir, appPort) | ||
}) | ||
afterAll(() => killApp(app)) | ||
|
||
it('should HMR when _app is removed', async () => { | ||
let indexContent = await fs.readFile(indexPage) | ||
try { | ||
const browser = await webdriver(appPort, '/') | ||
|
||
const html = await browser.eval('document.documentElement.innerHTML') | ||
expect(html).toContain('custom _app') | ||
|
||
await fs.rename(appPage, appPage + '.bak') | ||
|
||
await check(async () => { | ||
const html = await browser.eval('document.documentElement.innerHTML') | ||
return html.includes('index page') && !html.includes('custom _app') | ||
? 'success' | ||
: html | ||
}, 'success') | ||
|
||
await fs.writeFile( | ||
indexPage, | ||
` | ||
export default function Page() { | ||
return <p>index page updated</p> | ||
} | ||
` | ||
) | ||
|
||
await check(async () => { | ||
const html = await browser.eval('document.documentElement.innerHTML') | ||
return html.indexOf('index page updated') && | ||
!html.includes('custom _app') | ||
? 'success' | ||
: html | ||
}, 'success') | ||
|
||
await fs.rename(appPage + '.bak', appPage) | ||
|
||
await check(async () => { | ||
const html = await browser.eval('document.documentElement.innerHTML') | ||
return html.includes('index page updated') && | ||
html.includes('custom _app') | ||
? 'success' | ||
: html | ||
}, 'success') | ||
} finally { | ||
await fs.writeFile(indexPage, indexContent) | ||
|
||
if (await fs.pathExists(appPage + '.bak')) { | ||
await fs.rename(appPage + '.bak', appPage) | ||
} | ||
} | ||
}) | ||
|
||
it('should HMR when _document is removed', async () => { | ||
let indexContent = await fs.readFile(indexPage) | ||
try { | ||
const browser = await webdriver(appPort, '/') | ||
|
||
const html = await browser.eval('document.documentElement.innerHTML') | ||
expect(html).toContain('custom _document') | ||
|
||
await fs.rename(documentPage, documentPage + '.bak') | ||
|
||
await check(async () => { | ||
const html = await browser.eval('document.documentElement.innerHTML') | ||
return html.includes('index page') && !html.includes('custom _document') | ||
? 'success' | ||
: html | ||
}, 'success') | ||
|
||
await fs.writeFile( | ||
indexPage, | ||
` | ||
export default function Page() { | ||
return <p>index page updated</p> | ||
} | ||
` | ||
) | ||
|
||
await check(async () => { | ||
const html = await browser.eval('document.documentElement.innerHTML') | ||
return html.indexOf('index page updated') && | ||
!html.includes('custom _document') | ||
? 'success' | ||
: html | ||
}, 'success') | ||
|
||
await fs.rename(documentPage + '.bak', documentPage) | ||
|
||
await check(async () => { | ||
const html = await browser.eval('document.documentElement.innerHTML') | ||
return html.includes('index page updated') && | ||
html.includes('custom _document') | ||
? 'success' | ||
: html | ||
}, 'success') | ||
} finally { | ||
await fs.writeFile(indexPage, indexContent) | ||
|
||
if (await fs.pathExists(documentPage + '.bak')) { | ||
await fs.rename(documentPage + '.bak', documentPage) | ||
} | ||
} | ||
}) | ||
}) |