Commit 092df0f1 authored by Janka Uryga's avatar Janka Uryga

support experimental.validateRSCRequestHeaders for runtime prefetches

parent 63e373bf
...@@ -2,7 +2,9 @@ import { LinkAccordion } from '../components/link-accordion' ...@@ -2,7 +2,9 @@ import { LinkAccordion } from '../components/link-accordion'
export default function Page() { export default function Page() {
return ( return (
<ul> <div>
<h2>auto prefetches</h2>
<ul id="prefetch-auto">
<li> <li>
<LinkAccordion href="/target-page">Target page</LinkAccordion> <LinkAccordion href="/target-page">Target page</LinkAccordion>
</li> </li>
...@@ -12,5 +14,25 @@ export default function Page() { ...@@ -12,5 +14,25 @@ export default function Page() {
</LinkAccordion> </LinkAccordion>
</li> </li>
</ul> </ul>
{process.env.__NEXT_CACHE_COMPONENTS && (
// runtime prefetches are only available if cacheComponents is enabled.
<>
<h2>runtime prefetches</h2>
<ul id="prefetch-runtime">
<li>
<LinkAccordion href="/target-page" prefetch={true}>
Target page
</LinkAccordion>
</li>
<li>
<LinkAccordion href="/redirect-to-target-page" prefetch={true}>
Redirects to target page
</LinkAccordion>
</li>
</ul>
</>
)}
</div>
) )
} }
...@@ -5,14 +5,15 @@ import { findPort, nextBuild } from 'next-test-utils' ...@@ -5,14 +5,15 @@ import { findPort, nextBuild } from 'next-test-utils'
import { isNextDeploy, isNextDev } from 'e2e-utils' import { isNextDeploy, isNextDev } from 'e2e-utils'
import { start } from './server.mjs' import { start } from './server.mjs'
const isCacheComponentsEnabled =
process.env.__NEXT_EXPERIMENTAL_CACHE_COMPONENTS === 'true'
describe('segment cache (CDN cache busting)', () => { describe('segment cache (CDN cache busting)', () => {
if (isNextDev || isNextDeploy) { if (isNextDev || isNextDeploy) {
test('should not run during dev or deploy test runs', () => {}) test('should not run during dev or deploy test runs', () => {})
return return
} }
// TODO(runtime-ppr): add tests for runtime prefetches
// To debug these tests locally, run: // To debug these tests locally, run:
// node start.mjs // node start.mjs
// //
...@@ -34,10 +35,11 @@ describe('segment cache (CDN cache busting)', () => { ...@@ -34,10 +35,11 @@ describe('segment cache (CDN cache busting)', () => {
await cleanup() await cleanup()
}) })
it( describe(
"perform fully prefetched navigation with a CDN that doesn't respect " + "perform fully prefetched navigation with a CDN that doesn't respect " +
'the Vary header', 'the Vary header',
async () => { () => {
it('static prefetch', async () => {
let act let act
const browser = await webdriver(port, '/', { const browser = await webdriver(port, '/', {
beforePageLoad(p: Playwright.Page) { beforePageLoad(p: Playwright.Page) {
...@@ -53,11 +55,50 @@ describe('segment cache (CDN cache busting)', () => { ...@@ -53,11 +55,50 @@ describe('segment cache (CDN cache busting)', () => {
await act( await act(
async () => { async () => {
const linkToggle = await browser.elementByCss( const linkToggle = await browser.elementByCss(
'[data-link-accordion="/target-page"]' '#prefetch-auto [data-link-accordion="/target-page"]'
)
await linkToggle.click()
},
{
includes: 'Target page',
}
)
// Navigate to the prefetched target page.
await act(async () => {
const link = await browser.elementByCss('a[href="/target-page"]')
await link.click()
// The page was prefetched, so we're able to render the target
// page immediately.
const div = await browser.elementById('target-page')
expect(await div.text()).toBe('Target page')
}, 'no-requests')
})
if (isCacheComponentsEnabled) {
it('runtime prefetch', async () => {
let act
const browser = await webdriver(port, '/', {
beforePageLoad(p: Playwright.Page) {
act = createRouterAct(p)
},
})
// Initiate a prefetch. We'll send two requests - one to prefetch the tree,
// and another to prefetch the page content (which is static, so it will be complete).
// If we didn't also set a cache-busting search param, then
// the fake CDN used by this test suite would incorrectly use the same
// entry for both responses, poisoning the cache.
await act(
async () => {
const linkToggle = await browser.elementByCss(
'#prefetch-runtime [data-link-accordion="/target-page"]'
) )
await linkToggle.click() await linkToggle.click()
}, },
{ {
// This should be returned as part of the second request, if it wasn't cache poisoned.
includes: 'Target page', includes: 'Target page',
} }
) )
...@@ -72,14 +113,17 @@ describe('segment cache (CDN cache busting)', () => { ...@@ -72,14 +113,17 @@ describe('segment cache (CDN cache busting)', () => {
const div = await browser.elementById('target-page') const div = await browser.elementById('target-page')
expect(await div.text()).toBe('Target page') expect(await div.text()).toBe('Target page')
}, 'no-requests') }, 'no-requests')
})
}
} }
) )
it( describe(
'prevent cache poisoning attacks by responding with a redirect to correct ' + 'prevent cache poisoning attacks by responding with a redirect to correct ' +
'cache busting query param if a custom header is sent during a prefetch ' + 'cache busting query param if a custom header is sent during a prefetch ' +
'without a corresponding cache-busting search param', 'without a corresponding cache-busting search param',
async () => { () => {
it('static prefetch', async () => {
const browser = await webdriver(port, '/') const browser = await webdriver(port, '/')
const { status, responseUrl, redirected } = await browser.eval( const { status, responseUrl, redirected } = await browser.eval(
async () => { async () => {
...@@ -100,13 +144,74 @@ describe('segment cache (CDN cache busting)', () => { ...@@ -100,13 +144,74 @@ describe('segment cache (CDN cache busting)', () => {
expect(status).toBe(200) expect(status).toBe(200)
expect(responseUrl).toContain('_rsc=') expect(responseUrl).toContain('_rsc=')
expect(redirected).toBe(true) expect(redirected).toBe(true)
})
if (isCacheComponentsEnabled) {
it('runtime prefetch', async () => {
const browser = await webdriver(port, '/')
const { status, responseUrl, redirected } = await browser.eval(
async () => {
const res = await fetch('/target-page', {
headers: {
rsc: '1',
'next-router-prefetch': '2',
},
})
return {
status: res.status,
responseUrl: res.url,
redirected: res.redirected,
}
}
)
expect(status).toBe(200)
expect(responseUrl).toContain('_rsc=')
expect(redirected).toBe(true)
})
}
} }
) )
it( describe(
'perform fully prefetched navigation when a third-party proxy ' + 'perform fully prefetched navigation when a third-party proxy ' +
'performs a redirect', 'performs a redirect',
() => {
it('static prefetch', async () => {
let act
const browser = await webdriver(port, '/', {
beforePageLoad(p: Playwright.Page) {
act = createRouterAct(p)
},
})
await act(
async () => { async () => {
const linkToggle = await browser.elementByCss(
'#prefetch-auto [data-link-accordion="/redirect-to-target-page"]'
)
await linkToggle.click()
},
{
includes: 'Target page',
}
)
// Navigate to the prefetched target page.
await act(async () => {
const link = await browser.elementByCss(
'a[href="/redirect-to-target-page"]'
)
await link.click()
// The page was prefetched, so we're able to render the target
// page immediately.
const div = await browser.elementById('target-page')
expect(await div.text()).toBe('Target page')
}, 'no-requests')
})
if (isCacheComponentsEnabled) {
it('runtime prefetch', async () => {
let act let act
const browser = await webdriver(port, '/', { const browser = await webdriver(port, '/', {
beforePageLoad(p: Playwright.Page) { beforePageLoad(p: Playwright.Page) {
...@@ -117,7 +222,7 @@ describe('segment cache (CDN cache busting)', () => { ...@@ -117,7 +222,7 @@ describe('segment cache (CDN cache busting)', () => {
await act( await act(
async () => { async () => {
const linkToggle = await browser.elementByCss( const linkToggle = await browser.elementByCss(
'[data-link-accordion="/redirect-to-target-page"]' '#prefetch-runtime [data-link-accordion="/redirect-to-target-page"]'
) )
await linkToggle.click() await linkToggle.click()
}, },
...@@ -138,6 +243,8 @@ describe('segment cache (CDN cache busting)', () => { ...@@ -138,6 +243,8 @@ describe('segment cache (CDN cache busting)', () => {
const div = await browser.elementById('target-page') const div = await browser.elementById('target-page')
expect(await div.text()).toBe('Target page') expect(await div.text()).toBe('Target page')
}, 'no-requests') }, 'no-requests')
})
}
} }
) )
}) })
...@@ -135,11 +135,15 @@ async function createFakeCDN(destPort) { ...@@ -135,11 +135,15 @@ async function createFakeCDN(destPort) {
} }
resolveCDNEntry(entry) resolveCDNEntry(entry)
}) })
return } else {
}
// If the response isn't cacheable, pipe it through to the client. // If the response isn't cacheable, pipe it through to the client.
res.writeHead(
proxyRes.statusCode || 200,
proxyRes.statusMessage,
proxyRes.headers
)
proxyRes.pipe(res) proxyRes.pipe(res)
return }
}) })
return cdnServer return cdnServer
......
...@@ -159,7 +159,7 @@ export function createRouterAct( ...@@ -159,7 +159,7 @@ export function createRouterAct(
// `act` controls the timing of when responses reach the client, // `act` controls the timing of when responses reach the client,
// but it should not affect the timing of when requests reach the // but it should not affect the timing of when requests reach the
// server; we pass the request to the server the immediately. // server; we pass the request to the server the immediately.
result: new Promise(async (resolve) => { result: (async () => {
const originalResponse = await page.request.fetch(request, { const originalResponse = await page.request.fetch(request, {
maxRedirects: 0, maxRedirects: 0,
}) })
...@@ -172,13 +172,13 @@ export function createRouterAct( ...@@ -172,13 +172,13 @@ export function createRouterAct(
const headers = originalResponse.headers() const headers = originalResponse.headers()
delete headers['transfer-encoding'] delete headers['transfer-encoding']
resolve({ return {
text: await originalResponse.text(), text: await originalResponse.text(),
body: await originalResponse.body(), body: await originalResponse.body(),
headers, headers,
status: originalResponse.status(), status: originalResponse.status(),
}) }
}), })(),
didProcess: false, didProcess: false,
}) })
if (onDidIssueFirstRequest !== null) { if (onDidIssueFirstRequest !== null) {
...@@ -417,14 +417,14 @@ ${fulfilled.body} ...@@ -417,14 +417,14 @@ ${fulfilled.body}
batch.pendingRequests.add({ batch.pendingRequests.add({
url: req.url(), url: req.url(),
route: null, route: null,
result: new Promise(async (resolve) => { result: (async () => {
resolve({ return {
text: await res.text(), text: await res.text(),
body: await res.body(), body: await res.body(),
headers: res.headers(), headers: res.headers(),
status: res.status(), status: res.status(),
}) }
}), })(),
didProcess: false, didProcess: false,
}) })
page.off('response', handleResponse) page.off('response', handleResponse)
......
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