Skip to content

Commit

Permalink
Ensure query for static pages with rewrites is updated correctly (#24189
Browse files Browse the repository at this point in the history
)

This updates the query refreshing on the client to also refresh when rewrites are used and the page is static since additional query values can be provided from rewrites that are relied on client-side. An additional test has been added in the custom-routes suite to ensure this is working correctly. 

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added

Fixes: #23490
Fixes: #22931
Fixes: #21062
  • Loading branch information
ijjk authored Apr 18, 2021
1 parent 6e7245c commit 6558808
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 9 deletions.
18 changes: 16 additions & 2 deletions packages/next/client/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,24 @@ class Container extends React.Component<{
// - if it is a client-side skeleton (fallback render)
if (
router.isSsr &&
// We don't update for 404 requests as this can modify
// the asPath unexpectedly e.g. adding basePath when
// it wasn't originally present
page !== '/404' &&
!(
page === '/_error' &&
hydrateProps &&
hydrateProps.pageProps &&
hydrateProps.pageProps.statusCode === 404
) &&
(isFallback ||
(data.nextExport &&
(isDynamicRoute(router.pathname) || location.search)) ||
(hydrateProps && hydrateProps.__N_SSG && location.search))
(isDynamicRoute(router.pathname) ||
location.search ||
process.env.__NEXT_HAS_REWRITES)) ||
(hydrateProps &&
hydrateProps.__N_SSG &&
(location.search || process.env.__NEXT_HAS_REWRITES)))
) {
// update query on mount for exported pages
router.replace(
Expand Down
4 changes: 3 additions & 1 deletion packages/next/next-server/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,9 @@ export default class Router implements BaseRouter {
this.isReady = !!(
self.__NEXT_DATA__.gssp ||
self.__NEXT_DATA__.gip ||
(!autoExportDynamic && !self.location.search)
(!autoExportDynamic &&
!self.location.search &&
!process.env.__NEXT_HAS_REWRITES)
)
this.isPreview = !!isPreview
this.isLocaleDomain = false
Expand Down
4 changes: 2 additions & 2 deletions test/integration/build-output/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('Build Output', () => {
expect(indexSize.endsWith('B')).toBe(true)

// should be no bigger than 64.8 kb
expect(parseFloat(indexFirstLoad)).toBeCloseTo(65.3, 1)
expect(parseFloat(indexFirstLoad)).toBeCloseTo(65.4, 1)
expect(indexFirstLoad.endsWith('kB')).toBe(true)

expect(parseFloat(err404Size)).toBeCloseTo(3.7, 1)
Expand All @@ -108,7 +108,7 @@ describe('Build Output', () => {
expect(parseFloat(err404FirstLoad)).toBeCloseTo(68.5, 0)
expect(err404FirstLoad.endsWith('kB')).toBe(true)

expect(parseFloat(sharedByAll)).toBeCloseTo(65, 1)
expect(parseFloat(sharedByAll)).toBeCloseTo(65.1, 1)
expect(sharedByAll.endsWith('kB')).toBe(true)

if (_appSize.endsWith('kB')) {
Expand Down
6 changes: 5 additions & 1 deletion test/integration/custom-routes/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ module.exports = {
: []),
{
source: '/rewriting-to-auto-export',
destination: '/auto-export/hello',
destination: '/auto-export/hello?rewrite=1',
},
{
source: '/rewriting-to-another-auto-export/:path*',
destination: '/auto-export/another?rewrite=1',
},
{
source: '/to-another',
Expand Down
8 changes: 7 additions & 1 deletion test/integration/custom-routes/pages/auto-export/[slug].js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { useRouter } from 'next/router'

export default function Page() {
return <p id="auto-export">auto-export {useRouter().query.slug}</p>
const router = useRouter()
return (
<>
<p id="auto-export">auto-export {router.query.slug}</p>
<p id="query">{JSON.stringify(router.query)}</p>
</>
)
}
11 changes: 11 additions & 0 deletions test/integration/custom-routes/pages/auto-export/another.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useRouter } from 'next/router'

export default function Page() {
const router = useRouter()
return (
<>
<p id="auto-export-another">auto-export another</p>
<p id="query">{JSON.stringify(router.query)}</p>
</>
)
}
28 changes: 27 additions & 1 deletion test/integration/custom-routes/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@ const runTests = (isDev = false) => {
const browser = await webdriver(appPort, '/rewriting-to-auto-export')
const text = await browser.eval(() => document.documentElement.innerHTML)
expect(text).toContain('auto-export hello')
expect(JSON.parse(await browser.elementByCss('#query').text())).toEqual({
rewrite: '1',
slug: 'hello',
})
})

it('should provide params correctly for rewrite to auto-export non-dynamic page', async () => {
const browser = await webdriver(
appPort,
'/rewriting-to-another-auto-export/first'
)

expect(await browser.elementByCss('#auto-export-another').text()).toBe(
'auto-export another'
)
expect(JSON.parse(await browser.elementByCss('#query').text())).toEqual({
rewrite: '1',
path: ['first'],
})
})

it('should handle one-to-one rewrite successfully', async () => {
Expand Down Expand Up @@ -1377,10 +1396,17 @@ const runTests = (isDev = false) => {
],
afterFiles: [
{
destination: '/auto-export/hello',
destination: '/auto-export/hello?rewrite=1',
regex: normalizeRegEx('^\\/rewriting-to-auto-export$'),
source: '/rewriting-to-auto-export',
},
{
destination: '/auto-export/another?rewrite=1',
regex: normalizeRegEx(
'^\\/rewriting-to-another-auto-export(?:\\/((?:[^\\/]+?)(?:\\/(?:[^\\/]+?))*))?$'
),
source: '/rewriting-to-another-auto-export/:path*',
},
{
destination: '/another/one',
regex: normalizeRegEx('^\\/to-another$'),
Expand Down
3 changes: 2 additions & 1 deletion test/integration/production/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,8 @@ describe('Production Usage', () => {
it('should clear all core performance marks', async () => {
let browser
try {
browser = await webdriver(appPort, '/about')
browser = await webdriver(appPort, '/fully-dynamic')

const currentPerfMarks = await browser.eval(
`window.performance.getEntriesByType('mark')`
)
Expand Down

0 comments on commit 6558808

Please sign in to comment.