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

fix: Add Next-Url to http vary in consideration of intercept routes. #52746

Merged
merged 11 commits into from
Jul 22, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const NEXT_URL = 'Next-Url' as const
export const FETCH_CACHE_HEADER = 'x-vercel-sc-headers' as const
export const RSC_CONTENT_TYPE_HEADER = 'text/x-component' as const
export const RSC_VARY_HEADER =
`${RSC}, ${NEXT_ROUTER_STATE_TREE}, ${NEXT_ROUTER_PREFETCH}` as const
`${RSC}, ${NEXT_ROUTER_STATE_TREE}, ${NEXT_ROUTER_PREFETCH}, ${NEXT_URL}` as const

export const FLIGHT_PARAMETERS = [
[RSC],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export async function fetchServerResponse(
[
headers[NEXT_ROUTER_PREFETCH] || '0',
headers[NEXT_ROUTER_STATE_TREE],
headers[NEXT_URL],
].join(',')
)

Expand Down
5 changes: 5 additions & 0 deletions test/e2e/app-dir/app-prefetch/app/dashboard/page.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Link from 'next/link'

export const revalidate = 0

async function getData() {
Expand All @@ -12,6 +14,9 @@ export default async function DashboardPage(props) {
return (
<>
<p id="dashboard-page">{message}</p>
<Link href="/static-page" id="to-static-page">
To Static Page
</Link>
</>
)
}
37 changes: 37 additions & 0 deletions test/e2e/app-dir/app-prefetch/prefetching.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,43 @@ createNextDescribe(
).toBe(1)
})

it('should calculate `_rsc` query based on `Next-Url`', async () => {
const browser = await next.browser('/404', browserConfigWithFixedTime)
let staticPageRequests: string[] = []

browser.on('request', (req) => {
const url = new URL(req.url())
if (url.toString().includes(`/static-page?${NEXT_RSC_UNION_QUERY}=`)) {
staticPageRequests.push(`${url.pathname}${url.search}`)
}
})
await browser.eval('location.href = "/"')
await browser.eval(
`window.nd.router.prefetch("/static-page", {kind: "auto"})`
)
await check(() => {
return staticPageRequests.length === 1
? 'success'
: JSON.stringify(staticPageRequests)
}, 'success')

// Unable to clear router cache so mpa navigation
await browser.eval('location.href = "/dashboard"')
await browser.eval(
`window.nd.router.prefetch("/static-page", {kind: "auto"})`
)
await check(() => {
return staticPageRequests.length === 2
? 'success'
: JSON.stringify(staticPageRequests)
}, 'success')

expect(staticPageRequests[0]).toMatch('/static-page?_rsc=')
expect(staticPageRequests[1]).toMatch('/static-page?_rsc=')
// `_rsc` does not match because it depends on the `Next-Url`
expect(staticPageRequests[0]).not.toBe(staticPageRequests[1])
})

it('should not prefetch for a bot user agent', async () => {
const browser = await next.browser('/404')
let requests: string[] = []
Expand Down
8 changes: 4 additions & 4 deletions test/e2e/app-dir/app/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ createNextDescribe(
expect(res.headers.get('x-edge-runtime')).toBe('1')
expect(res.headers.get('vary')).toBe(
isNextDeploy
? 'RSC, Next-Router-State-Tree, Next-Router-Prefetch'
: 'RSC, Next-Router-State-Tree, Next-Router-Prefetch, Accept-Encoding'
? 'RSC, Next-Router-State-Tree, Next-Router-Prefetch, Next-Url'
: 'RSC, Next-Router-State-Tree, Next-Router-Prefetch, Next-Url, Accept-Encoding'
)
})

Expand All @@ -269,8 +269,8 @@ createNextDescribe(
})
expect(res.headers.get('vary')).toBe(
isNextDeploy
? 'RSC, Next-Router-State-Tree, Next-Router-Prefetch'
: 'RSC, Next-Router-State-Tree, Next-Router-Prefetch, Accept-Encoding'
? 'RSC, Next-Router-State-Tree, Next-Router-Prefetch, Next-Url'
: 'RSC, Next-Router-State-Tree, Next-Router-Prefetch, Next-Url, Accept-Encoding'
)
})

Expand Down
3 changes: 2 additions & 1 deletion test/integration/dynamic-routing/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,8 @@ function runTests({ dev }) {
rsc: {
header: 'RSC',
contentTypeHeader: 'text/x-component',
varyHeader: 'RSC, Next-Router-State-Tree, Next-Router-Prefetch',
varyHeader:
'RSC, Next-Router-State-Tree, Next-Router-Prefetch, Next-Url',
},
})
})
Expand Down