Unverified Commit cd3e054f authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure middleware has single data fetch on query hydration with valid props (#39210)

* Ensure middleware has single data fetch on query hydration with valid props

* re-add check

* fix redirect case
parent c4e8d7d7
...@@ -427,7 +427,7 @@ const backgroundCache: Record<string, Promise<any>> = {} ...@@ -427,7 +427,7 @@ const backgroundCache: Record<string, Promise<any>> = {}
interface FetchDataOutput { interface FetchDataOutput {
dataHref: string dataHref: string
json: Record<string, any> json: Record<string, any> | null
response: Response response: Response
text: string text: string
} }
...@@ -508,7 +508,7 @@ function fetchNextData({ ...@@ -508,7 +508,7 @@ function fetchNextData({
return { return {
dataHref, dataHref,
json: parseJSON ? tryToParseAsJSON(text) : {}, json: parseJSON ? tryToParseAsJSON(text) : null,
response, response,
text, text,
} }
...@@ -552,7 +552,7 @@ function tryToParseAsJSON(text: string) { ...@@ -552,7 +552,7 @@ function tryToParseAsJSON(text: string) {
try { try {
return JSON.parse(text) return JSON.parse(text)
} catch (error) { } catch (error) {
return {} return null
} }
} }
...@@ -1807,28 +1807,24 @@ export default class Router implements BaseRouter { ...@@ -1807,28 +1807,24 @@ export default class Router implements BaseRouter {
const { props } = await this._getData(async () => { const { props } = await this._getData(async () => {
if (shouldFetchData && !useStreamedFlightData) { if (shouldFetchData && !useStreamedFlightData) {
const { json } = const { json } = data?.json
data?.json && ? data
data?.response.headers : await fetchNextData({
.get('content-type') dataHref: this.pageLoader.getDataHref({
?.includes('application/json') href: formatWithValidation({ pathname, query }),
? data asPath: resolvedAs,
: await fetchNextData({ locale,
dataHref: this.pageLoader.getDataHref({ }),
href: formatWithValidation({ pathname, query }), isServerRender: this.isSsr,
asPath: resolvedAs, parseJSON: true,
locale, inflightCache: this.sdc,
}), persistCache: !isPreview,
isServerRender: this.isSsr, isPrefetch: false,
parseJSON: true, unstable_skipClientCache,
inflightCache: this.sdc, })
persistCache: !isPreview,
isPrefetch: false,
unstable_skipClientCache,
})
return { return {
props: json, props: json || {},
} }
} }
......
...@@ -27,6 +27,27 @@ describe('Middleware Rewrite', () => { ...@@ -27,6 +27,27 @@ describe('Middleware Rewrite', () => {
testsWithLocale('/fr') testsWithLocale('/fr')
function tests() { function tests() {
it('should not have un-necessary data request on rewrite', async () => {
const browser = await webdriver(next.url, '/to-blog/first', {
waitHydration: false,
})
let requests = []
browser.on('request', (req) => {
requests.push(new URL(req.url()).pathname)
})
await check(
() => browser.eval(`next.router.isReady ? "yup" : "nope"`),
'yup'
)
expect(
requests.filter((url) => url === '/fallback-true-blog/first.json')
.length
).toBeLessThan(2)
})
it('should not mix component cache when navigating between dynamic routes', async () => { it('should not mix component cache when navigating between dynamic routes', async () => {
const browser = await webdriver(next.url, '/param-1') const browser = await webdriver(next.url, '/param-1')
......
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