Skip to content

Commit

Permalink
Fix pageProps is missing when route changes (#38178)
Browse files Browse the repository at this point in the history
## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`


Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
  • Loading branch information
huozhi and ijjk committed Jun 30, 2022
1 parent 50fa170 commit f21a9dd
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/next/shared/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1857,8 +1857,9 @@ export default class Router implements BaseRouter {
).catch(() => {})
}

let flightInfo
if (routeInfo.__N_RSC) {
props.pageProps = Object.assign(props.pageProps, {
flightInfo = {
__flight__: useStreamedFlightData
? (
await this._getData(() =>
Expand All @@ -1877,9 +1878,10 @@ export default class Router implements BaseRouter {
)
).data
: props.__flight__,
})
}
}

props.pageProps = Object.assign({}, props.pageProps, flightInfo)
routeInfo.props = props
routeInfo.route = route
routeInfo.query = query
Expand Down
8 changes: 8 additions & 0 deletions test/e2e/middleware-general/app/pages/_app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default function App({ Component, pageProps }) {
if (!pageProps || typeof pageProps !== 'object') {
throw new Error(
`Invariant: received invalid pageProps in _app, received ${pageProps}`
)
}
return <Component {...pageProps} />
}
9 changes: 9 additions & 0 deletions test/e2e/middleware-redirects/app/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,13 @@ module.exports = {
locales: ['en', 'fr', 'nl', 'es'],
defaultLocale: 'en',
},
redirects() {
return [
{
source: '/to-new',
destination: '/dynamic/new',
permanent: false,
},
]
},
}
8 changes: 8 additions & 0 deletions test/e2e/middleware-redirects/app/pages/_app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default function App({ Component, pageProps }) {
if (!pageProps || typeof pageProps !== 'object') {
throw new Error(
`Invariant: received invalid pageProps in _app, received ${pageProps}`
)
}
return <Component {...pageProps} />
}
13 changes: 13 additions & 0 deletions test/e2e/middleware-redirects/app/pages/dynamic/[slug].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default function Account() {
return (
<p id="dynamic" className="title">
Welcome to a /dynamic/[slug]
</p>
)
}

export function getServerSideProps() {
return {
props: {},
}
}
8 changes: 8 additions & 0 deletions test/e2e/middleware-redirects/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ describe('Middleware Redirect', () => {
testsWithLocale('/fr')

function tests() {
it('should redirect correctly with redirect in next.config.js', async () => {
const browser = await webdriver(next.url, '/')
await browser.eval('window.beforeNav = 1')
await browser.eval('window.next.router.push("/to-new")')
await browser.waitForElementByCss('#dynamic')
expect(await browser.eval('window.beforeNav')).toBe(1)
})

it('does not include the locale in redirects by default', async () => {
const res = await fetchViaHTTP(next.url, `/old-home`, undefined, {
redirect: 'manual',
Expand Down
8 changes: 8 additions & 0 deletions test/e2e/middleware-rewrites/app/pages/_app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default function App({ Component, pageProps }) {
if (!pageProps || typeof pageProps !== 'object') {
throw new Error(
`Invariant: received invalid pageProps in _app, received ${pageProps}`
)
}
return <Component {...pageProps} />
}
8 changes: 8 additions & 0 deletions test/integration/dynamic-routing/pages/_app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default function App({ Component, pageProps }) {
if (!pageProps || typeof pageProps !== 'object') {
throw new Error(
`Invariant: received invalid pageProps in _app, received ${pageProps}`
)
}
return <Component {...pageProps} />
}

0 comments on commit f21a9dd

Please sign in to comment.