-
Notifications
You must be signed in to change notification settings - Fork 26.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Revert "Avoid unnecessary router state changes"" (#37593)
Reverts #37572, with a new test case added about `routeChangeComplete`.
- Loading branch information
Showing
4 changed files
with
154 additions
and
28 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,10 @@ | ||
module.exports = { | ||
rewrites() { | ||
return [ | ||
{ | ||
source: '/rewrite', | ||
destination: '/?foo=bar', | ||
}, | ||
] | ||
}, | ||
} |
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,17 @@ | ||
import { useEffect } from 'react' | ||
import Router, { useRouter } from 'next/router' | ||
|
||
Router.events.on('routeChangeComplete', (url) => { | ||
window.__route_change_complete = url === '/' | ||
}) | ||
|
||
export default function Index() { | ||
const { query } = useRouter() | ||
|
||
useEffect(() => { | ||
window.__renders = window.__renders || [] | ||
window.__renders.push(query.foo) | ||
}) | ||
|
||
return <p>A page should not be rerendered if unnecessary.</p> | ||
} |
56 changes: 56 additions & 0 deletions
56
test/integration/rewrites-client-rerender/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,56 @@ | ||
/* eslint-env jest */ | ||
|
||
import { join } from 'path' | ||
import { | ||
findPort, | ||
killApp, | ||
launchApp, | ||
nextBuild, | ||
nextStart, | ||
} from 'next-test-utils' | ||
import webdriver from 'next-webdriver' | ||
|
||
const appDir = join(__dirname, '../') | ||
|
||
let appPort | ||
let app | ||
|
||
const runTests = () => { | ||
it('should not trigger unncessary rerenders', async () => { | ||
const browser = await webdriver(appPort, '/') | ||
await new Promise((resolve) => setTimeout(resolve, 100)) | ||
|
||
expect(await browser.eval('window.__renders')).toEqual([undefined]) | ||
expect(await browser.eval('window.__route_change_complete')).toEqual(true) | ||
}) | ||
|
||
it('should rerender with the correct query parameter if present', async () => { | ||
const browser = await webdriver(appPort, '/rewrite') | ||
await new Promise((resolve) => setTimeout(resolve, 100)) | ||
|
||
expect(await browser.eval('window.__renders')).toEqual([undefined, 'bar']) | ||
}) | ||
} | ||
|
||
describe('rewrites client rerender', () => { | ||
describe('dev mode', () => { | ||
beforeAll(async () => { | ||
appPort = await findPort() | ||
app = await launchApp(appDir, appPort) | ||
}) | ||
afterAll(() => killApp(app)) | ||
|
||
runTests() | ||
}) | ||
|
||
describe('production mode', () => { | ||
beforeAll(async () => { | ||
await nextBuild(appDir) | ||
appPort = await findPort() | ||
app = await nextStart(appDir, appPort) | ||
}) | ||
afterAll(() => killApp(app)) | ||
|
||
runTests() | ||
}) | ||
}) |