Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 19 additions & 23 deletions packages/next/shared/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ const backgroundCache: Record<string, Promise<any>> = {}

interface FetchDataOutput {
dataHref: string
json: Record<string, any>
json: Record<string, any> | null
response: Response
text: string
}
Expand Down Expand Up @@ -508,7 +508,7 @@ function fetchNextData({

return {
dataHref,
json: parseJSON ? tryToParseAsJSON(text) : {},
json: parseJSON ? tryToParseAsJSON(text) : null,
response,
text,
}
Expand Down Expand Up @@ -552,7 +552,7 @@ function tryToParseAsJSON(text: string) {
try {
return JSON.parse(text)
} catch (error) {
return {}
return null
}
}

Expand Down Expand Up @@ -1807,28 +1807,24 @@ export default class Router implements BaseRouter {

const { props } = await this._getData(async () => {
if (shouldFetchData && !useStreamedFlightData) {
const { json } =
data?.json &&
data?.response.headers
.get('content-type')
?.includes('application/json')
? data
: await fetchNextData({
dataHref: this.pageLoader.getDataHref({
href: formatWithValidation({ pathname, query }),
asPath: resolvedAs,
locale,
}),
isServerRender: this.isSsr,
parseJSON: true,
inflightCache: this.sdc,
persistCache: !isPreview,
isPrefetch: false,
unstable_skipClientCache,
})
const { json } = data?.json
? data
: await fetchNextData({
dataHref: this.pageLoader.getDataHref({
href: formatWithValidation({ pathname, query }),
asPath: resolvedAs,
locale,
}),
isServerRender: this.isSsr,
parseJSON: true,
inflightCache: this.sdc,
persistCache: !isPreview,
isPrefetch: false,
unstable_skipClientCache,
})

return {
props: json,
props: json || {},
}
}

Expand Down
21 changes: 21 additions & 0 deletions test/e2e/middleware-rewrites/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@ describe('Middleware Rewrite', () => {
testsWithLocale('/fr')

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 () => {
const browser = await webdriver(next.url, '/param-1')

Expand Down