Unverified Commit aff987d5 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

backport: feat: implement LRU cache with invocation ID scoping for minimal mode…

backport: feat: implement LRU cache with invocation ID scoping for minimal mode response cache (#89129)

Backports:
- #88509
- #89123

---------
Co-authored-by: 's avatarWyatt Johnson <accounts+github@wyattjoh.ca>
parent 330089bd
...@@ -226,4 +226,75 @@ describe('LRUCache', () => { ...@@ -226,4 +226,75 @@ describe('LRUCache', () => {
expect(cache.has('key149')).toBe(true) // recent keys retained expect(cache.has('key149')).toBe(true) // recent keys retained
}) })
}) })
describe('onEvict Callback', () => {
it('should call onEvict when an entry is evicted', () => {
const evicted: Array<{ key: string; value: string }> = []
const cache = new LRUCache<string>(2, undefined, (key, value) => {
evicted.push({ key, value })
})
cache.set('a', 'value-a')
cache.set('b', 'value-b')
expect(evicted.length).toBe(0)
cache.set('c', 'value-c') // should evict 'a'
expect(evicted.length).toBe(1)
expect(evicted[0]).toEqual({ key: 'a', value: 'value-a' })
})
it('should not call onEvict when updating existing entry', () => {
const evicted: string[] = []
const cache = new LRUCache<string>(2, undefined, (key) => {
evicted.push(key)
})
cache.set('a', 'value-a')
cache.set('a', 'new-value-a')
expect(evicted.length).toBe(0)
})
it('should call onEvict for each evicted entry when multiple are evicted', () => {
const evicted: string[] = []
const cache = new LRUCache<string>(
10,
(value) => value.length,
(key) => {
evicted.push(key)
}
)
cache.set('key1', 'ab') // size 2
cache.set('key2', 'cd') // size 2
cache.set('key3', 'ef') // size 2, total = 6
cache.set('key4', 'ghijklmno') // size 9, should evict key1, key2, key3
expect(evicted).toEqual(['key1', 'key2', 'key3'])
})
it('should work without onEvict callback', () => {
const cache = new LRUCache<string>(2)
cache.set('a', 'value-a')
cache.set('b', 'value-b')
cache.set('c', 'value-c') // should evict without error
expect(cache.has('a')).toBe(false)
})
it('should pass the evicted value to the callback', () => {
const evicted: Array<{ id: number }> = []
const cache = new LRUCache<{ id: number }>(
1,
undefined,
(_key, value) => {
evicted.push(value)
}
)
cache.set('obj1', { id: 1 })
cache.set('obj2', { id: 2 }) // should evict obj1
expect(evicted.length).toBe(1)
expect(evicted[0]).toEqual({ id: 1 })
})
})
}) })
...@@ -50,10 +50,16 @@ export class LRUCache<T> { ...@@ -50,10 +50,16 @@ export class LRUCache<T> {
private totalSize: number = 0 private totalSize: number = 0
private readonly maxSize: number private readonly maxSize: number
private readonly calculateSize: ((value: T) => number) | undefined private readonly calculateSize: ((value: T) => number) | undefined
private readonly onEvict: ((key: string, value: T) => void) | undefined
constructor(maxSize: number, calculateSize?: (value: T) => number) { constructor(
maxSize: number,
calculateSize?: (value: T) => number,
onEvict?: (key: string, value: T) => void
) {
this.maxSize = maxSize this.maxSize = maxSize
this.calculateSize = calculateSize this.calculateSize = calculateSize
this.onEvict = onEvict
// Create sentinel nodes to simplify doubly-linked list operations // Create sentinel nodes to simplify doubly-linked list operations
// HEAD <-> TAIL (empty list) // HEAD <-> TAIL (empty list)
...@@ -144,6 +150,7 @@ export class LRUCache<T> { ...@@ -144,6 +150,7 @@ export class LRUCache<T> {
const tail = this.removeTail() const tail = this.removeTail()
this.cache.delete(tail.key) this.cache.delete(tail.key)
this.totalSize -= tail.size this.totalSize -= tail.size
this.onEvict?.(tail.key, tail.data)
} }
} }
...@@ -191,6 +198,10 @@ export class LRUCache<T> { ...@@ -191,6 +198,10 @@ export class LRUCache<T> {
* Removes a specific key from the cache. * Removes a specific key from the cache.
* Updates both the hash map and doubly-linked list. * Updates both the hash map and doubly-linked list.
* *
* Note: This is an explicit removal and does NOT trigger the `onEvict`
* callback. Use this for intentional deletions where eviction tracking
* is not needed.
*
* Time Complexity: O(1) * Time Complexity: O(1)
*/ */
public remove(key: string): void { public remove(key: string): void {
......
...@@ -879,6 +879,9 @@ export abstract class RouteModule< ...@@ -879,6 +879,9 @@ export abstract class RouteModule<
isRoutePPREnabled, isRoutePPREnabled,
isOnDemandRevalidate, isOnDemandRevalidate,
isPrefetch: req.headers.purpose === 'prefetch', isPrefetch: req.headers.purpose === 'prefetch',
// Use x-invocation-id header to scope the in-memory cache to a single
// revalidation request in minimal mode.
invocationID: req.headers['x-invocation-id'] as string | undefined,
incrementalCache: await this.getIncrementalCache( incrementalCache: await this.getIncrementalCache(
req, req,
nextConfig, nextConfig,
......
...@@ -16,6 +16,7 @@ import { writeFile } from 'fs-extra' ...@@ -16,6 +16,7 @@ import { writeFile } from 'fs-extra'
import getPort from 'get-port' import getPort from 'get-port'
import { getRandomPort } from 'get-port-please' import { getRandomPort } from 'get-port-please'
import fetch from 'node-fetch' import fetch from 'node-fetch'
import { nanoid } from 'nanoid'
import qs from 'querystring' import qs from 'querystring'
import treeKill from 'tree-kill' import treeKill from 'tree-kill'
import { once } from 'events' import { once } from 'events'
...@@ -182,6 +183,37 @@ export function fetchViaHTTP( ...@@ -182,6 +183,37 @@ export function fetchViaHTTP(
return fetch(getFullUrl(appPort, url), opts) return fetch(getFullUrl(appPort, url), opts)
} }
/**
* Creates request options with a unique x-invocation-id header for testing
* cache deduplication in minimal mode. Use this when you need to ensure each
* request is treated as independent, or when multiple requests need to share
* the same invocation ID.
*
* @example
* // Independent requests (each gets its own invocation ID)
* const res1 = await fetchViaHTTP(appPort, '/page', undefined, withInvocationId())
* const res2 = await fetchViaHTTP(appPort, '/page', undefined, withInvocationId())
*
* @example
* // Grouped requests (share the same invocation ID for cache testing)
* const sharedOpts = withInvocationId()
* const res1 = await fetchViaHTTP(appPort, '/page', undefined, sharedOpts)
* const res2 = await fetchViaHTTP(appPort, '/_next/data/.../page.json', undefined, sharedOpts)
*
* @param opts - Optional existing RequestInit to merge with
* @returns RequestInit with x-invocation-id header added
*/
export function withInvocationId(opts?: RequestInit): RequestInit {
const invocationId = `test:${nanoid()}`
return {
...opts,
headers: {
...opts?.headers,
'x-invocation-id': invocationId,
},
}
}
export function renderViaHTTP( export function renderViaHTTP(
appPort: string | number, appPort: string | number,
pathname: string, pathname: string,
......
...@@ -10,6 +10,7 @@ import { ...@@ -10,6 +10,7 @@ import {
findPort, findPort,
initNextServerScript, initNextServerScript,
killApp, killApp,
withInvocationId,
} from 'next-test-utils' } from 'next-test-utils'
import { ChildProcess } from 'child_process' import { ChildProcess } from 'child_process'
...@@ -105,7 +106,11 @@ describe('required server files app router', () => { ...@@ -105,7 +106,11 @@ describe('required server files app router', () => {
}) })
it('should send the right cache headers for an app route', async () => { it('should send the right cache headers for an app route', async () => {
const res = await fetchViaHTTP(appPort, '/api/test/123', undefined, { const res = await fetchViaHTTP(
appPort,
'/api/test/123',
undefined,
withInvocationId({
headers: { headers: {
'x-matched-path': '/api/test/[slug]', 'x-matched-path': '/api/test/[slug]',
'x-now-route-matches': createNowRouteMatches({ 'x-now-route-matches': createNowRouteMatches({
...@@ -113,6 +118,7 @@ describe('required server files app router', () => { ...@@ -113,6 +118,7 @@ describe('required server files app router', () => {
}).toString(), }).toString(),
}, },
}) })
)
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(res.headers.get('cache-control')).toBe('s-maxage=31536000') expect(res.headers.get('cache-control')).toBe('s-maxage=31536000')
}) })
...@@ -122,7 +128,7 @@ describe('required server files app router', () => { ...@@ -122,7 +128,7 @@ describe('required server files app router', () => {
appPort, appPort,
'/optional-catchall/[lang]/[flags]/[[...slug]]', '/optional-catchall/[lang]/[flags]/[[...slug]]',
undefined, undefined,
{ withInvocationId({
headers: { headers: {
'x-matched-path': '/optional-catchall/[lang]/[flags]/[[...slug]]', 'x-matched-path': '/optional-catchall/[lang]/[flags]/[[...slug]]',
'x-now-route-matches': createNowRouteMatches({ 'x-now-route-matches': createNowRouteMatches({
...@@ -131,7 +137,7 @@ describe('required server files app router', () => { ...@@ -131,7 +137,7 @@ describe('required server files app router', () => {
slug: 'slug', slug: 'slug',
}).toString(), }).toString(),
}, },
} })
) )
expect(res.status).toBe(200) expect(res.status).toBe(200)
...@@ -144,7 +150,7 @@ describe('required server files app router', () => { ...@@ -144,7 +150,7 @@ describe('required server files app router', () => {
appPort, appPort,
'/optional-catchall/[lang]/[flags]/[[...slug]]', '/optional-catchall/[lang]/[flags]/[[...slug]]',
undefined, undefined,
{ withInvocationId({
headers: { headers: {
'x-matched-path': '/optional-catchall/[lang]/[flags]/[[...slug]]', 'x-matched-path': '/optional-catchall/[lang]/[flags]/[[...slug]]',
'x-now-route-matches': createNowRouteMatches({ 'x-now-route-matches': createNowRouteMatches({
...@@ -152,7 +158,7 @@ describe('required server files app router', () => { ...@@ -152,7 +158,7 @@ describe('required server files app router', () => {
flags: 'flags', flags: 'flags',
}).toString(), }).toString(),
}, },
} })
) )
expect(res.status).toBe(200) expect(res.status).toBe(200)
...@@ -164,7 +170,11 @@ describe('required server files app router', () => { ...@@ -164,7 +170,11 @@ describe('required server files app router', () => {
}) })
it('should send the right cache headers for an app page', async () => { it('should send the right cache headers for an app page', async () => {
const res = await fetchViaHTTP(appPort, '/test/123', undefined, { const res = await fetchViaHTTP(
appPort,
'/test/123',
undefined,
withInvocationId({
headers: { headers: {
'x-matched-path': '/test/[slug]', 'x-matched-path': '/test/[slug]',
'x-now-route-matches': createNowRouteMatches({ 'x-now-route-matches': createNowRouteMatches({
...@@ -172,6 +182,7 @@ describe('required server files app router', () => { ...@@ -172,6 +182,7 @@ describe('required server files app router', () => {
}).toString(), }).toString(),
}, },
}) })
)
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(res.headers.get('cache-control')).toBe( expect(res.headers.get('cache-control')).toBe(
's-maxage=3600, stale-while-revalidate=31532400' 's-maxage=3600, stale-while-revalidate=31532400'
...@@ -183,13 +194,18 @@ describe('required server files app router', () => { ...@@ -183,13 +194,18 @@ describe('required server files app router', () => {
}) })
it('should properly handle prerender for bot request', async () => { it('should properly handle prerender for bot request', async () => {
const res = await fetchViaHTTP(appPort, '/isr/first', undefined, { const res = await fetchViaHTTP(
appPort,
'/isr/first',
undefined,
withInvocationId({
headers: { headers: {
'user-agent': 'user-agent':
'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.179 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)', 'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.179 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
'x-matched-path': '/isr/first', 'x-matched-path': '/isr/first',
}, },
}) })
)
expect(res.status).toBe(200) expect(res.status).toBe(200)
const html = await res.text() const html = await res.text()
...@@ -197,19 +213,28 @@ describe('required server files app router', () => { ...@@ -197,19 +213,28 @@ describe('required server files app router', () => {
expect($('#page').text()).toBe('/isr/[slug]') expect($('#page').text()).toBe('/isr/[slug]')
const rscRes = await fetchViaHTTP(appPort, '/isr/first.rsc', undefined, { const rscRes = await fetchViaHTTP(
appPort,
'/isr/first.rsc',
undefined,
withInvocationId({
headers: { headers: {
'user-agent': 'user-agent':
'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.179 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)', 'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.179 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
'x-matched-path': '/isr/first', 'x-matched-path': '/isr/first',
}, },
}) })
)
expect(rscRes.status).toBe(200) expect(rscRes.status).toBe(200)
}) })
it('should properly handle fallback for bot request', async () => { it('should properly handle fallback for bot request', async () => {
const res = await fetchViaHTTP(appPort, '/isr/[slug]', undefined, { const res = await fetchViaHTTP(
appPort,
'/isr/[slug]',
undefined,
withInvocationId({
headers: { headers: {
'user-agent': 'user-agent':
'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.179 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)', 'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.179 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
...@@ -219,6 +244,7 @@ describe('required server files app router', () => { ...@@ -219,6 +244,7 @@ describe('required server files app router', () => {
'x-matched-path': '/isr/[slug]', 'x-matched-path': '/isr/[slug]',
}, },
}) })
)
expect(res.status).toBe(200) expect(res.status).toBe(200)
const html = await res.text() const html = await res.text()
...@@ -226,7 +252,11 @@ describe('required server files app router', () => { ...@@ -226,7 +252,11 @@ describe('required server files app router', () => {
expect($('#page').text()).toBe('/isr/[slug]') expect($('#page').text()).toBe('/isr/[slug]')
const rscRes = await fetchViaHTTP(appPort, '/isr/[slug].rsc', undefined, { const rscRes = await fetchViaHTTP(
appPort,
'/isr/[slug].rsc',
undefined,
withInvocationId({
headers: { headers: {
'user-agent': 'user-agent':
'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.179 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)', 'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.179 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
...@@ -236,6 +266,7 @@ describe('required server files app router', () => { ...@@ -236,6 +266,7 @@ describe('required server files app router', () => {
'x-matched-path': '/isr/[slug]', 'x-matched-path': '/isr/[slug]',
}, },
}) })
)
expect(rscRes.status).toBe(200) expect(rscRes.status).toBe(200)
}) })
...@@ -260,9 +291,14 @@ describe('required server files app router', () => { ...@@ -260,9 +291,14 @@ describe('required server files app router', () => {
], ],
]) { ]) {
require('console').error('checking', { path, tags }) require('console').error('checking', { path, tags })
const res = await fetchViaHTTP(appPort, path, undefined, { const res = await fetchViaHTTP(
appPort,
path,
undefined,
withInvocationId({
redirect: 'manual', redirect: 'manual',
}) })
)
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(res.headers.get('x-next-cache-tags')).toBe(tags) expect(res.headers.get('x-next-cache-tags')).toBe(tags)
} }
...@@ -275,9 +311,14 @@ describe('required server files app router', () => { ...@@ -275,9 +311,14 @@ describe('required server files app router', () => {
'/api/ssr/first', '/api/ssr/first',
'/api/ssr/second', '/api/ssr/second',
]) { ]) {
const res = await fetchViaHTTP(appPort, path, undefined, { const res = await fetchViaHTTP(
appPort,
path,
undefined,
withInvocationId({
redirect: 'manual', redirect: 'manual',
}) })
)
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(res.headers.get('x-next-cache-tags')).toBeFalsy() expect(res.headers.get('x-next-cache-tags')).toBeFalsy()
} }
...@@ -294,9 +335,9 @@ describe('required server files app router', () => { ...@@ -294,9 +335,9 @@ describe('required server files app router', () => {
appPort, appPort,
path, path,
{ hello: 'world' }, { hello: 'world' },
{ withInvocationId({
redirect: 'manual', redirect: 'manual',
} })
) )
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(res.headers.get('x-next-cache-tags')).toBeFalsy() expect(res.headers.get('x-next-cache-tags')).toBeFalsy()
...@@ -308,11 +349,11 @@ describe('required server files app router', () => { ...@@ -308,11 +349,11 @@ describe('required server files app router', () => {
appPort, appPort,
'/search/[key]', '/search/[key]',
{ key: 'searchParams', nxtPkey: 'params' }, { key: 'searchParams', nxtPkey: 'params' },
{ withInvocationId({
headers: { headers: {
'x-matched-path': '/search/[key]', 'x-matched-path': '/search/[key]',
}, },
} })
) )
const html = await res.text() const html = await res.text()
...@@ -320,4 +361,80 @@ describe('required server files app router', () => { ...@@ -320,4 +361,80 @@ describe('required server files app router', () => {
expect($('dd[data-params]').text()).toBe('params') expect($('dd[data-params]').text()).toBe('params')
expect($('dd[data-searchParams]').text()).toBe('searchParams') expect($('dd[data-searchParams]').text()).toBe('searchParams')
}) })
it('should de-dupe HTML/RSC requests for ISR pages', async () => {
// Create a shared invocation ID for HTML and RSC requests to test de-duplication
const sharedOpts = withInvocationId()
// First request: HTML for ISR page
const htmlRes = await fetchViaHTTP(appPort, '/isr/[slug]', undefined, {
...sharedOpts,
headers: {
...sharedOpts.headers,
'x-matched-path': '/isr/[slug]',
'x-now-route-matches': createNowRouteMatches({
slug: 'first',
}).toString(),
},
})
expect(htmlRes.status).toBe(200)
const html = await htmlRes.text()
const $ = cheerio.load(html)
const timestamp1 = $('#now').text()
// Second request: RSC for same page with same x-invocation-id
const rscRes = await fetchViaHTTP(appPort, '/isr/[slug].rsc', undefined, {
...sharedOpts,
headers: {
...sharedOpts.headers,
'x-matched-path': '/isr/[slug]',
'x-now-route-matches': createNowRouteMatches({
slug: 'first',
}).toString(),
},
})
expect(rscRes.status).toBe(200)
const rscText = await rscRes.text()
// Both should have the same timestamp (same cached render)
expect(rscText).toContain(timestamp1)
})
it('should isolate cache between different ISR request groups', async () => {
// First group makes a request with its own invocation ID
const group1Opts = withInvocationId()
const res1 = await fetchViaHTTP(appPort, '/isr/[slug]', undefined, {
...group1Opts,
headers: {
...group1Opts.headers,
'x-matched-path': '/isr/[slug]',
'x-now-route-matches': createNowRouteMatches({
slug: 'first',
}).toString(),
},
})
expect(res1.status).toBe(200)
const $1 = cheerio.load(await res1.text())
const data1 = $1('#data').text()
// Second group with different x-invocation-id
const group2Opts = withInvocationId()
const res2 = await fetchViaHTTP(appPort, '/isr/[slug]', undefined, {
...group2Opts,
headers: {
...group2Opts.headers,
'x-matched-path': '/isr/[slug]',
'x-now-route-matches': createNowRouteMatches({
slug: 'first',
}).toString(),
},
})
expect(res2.status).toBe(200)
const $2 = cheerio.load(await res2.text())
const data2 = $2('#data').text()
// Each group should get its own render with different random data
// since ISR pages fetch fresh data on each render
expect(data1).not.toBe(data2)
})
}) })
...@@ -10,6 +10,7 @@ import { ...@@ -10,6 +10,7 @@ import {
findPort, findPort,
initNextServerScript, initNextServerScript,
killApp, killApp,
withInvocationId,
} from 'next-test-utils' } from 'next-test-utils'
import { ChildProcess } from 'child_process' import { ChildProcess } from 'child_process'
...@@ -139,12 +140,19 @@ describe('required server files app router', () => { ...@@ -139,12 +140,19 @@ describe('required server files app router', () => {
require('console').error('requesting', outputSegmentPath) require('console').error('requesting', outputSegmentPath)
const res = await fetchViaHTTP(appPort, outputSegmentPath, undefined, { const res = await fetchViaHTTP(
appPort,
outputSegmentPath,
undefined,
withInvocationId({
headers: { headers: {
'x-matched-path': '/isr/[slug].segments/_tree.segment.rsc', 'x-matched-path': '/isr/[slug].segments/_tree.segment.rsc',
'x-now-route-matches': 'slug=first&1=first', 'x-now-route-matches': createNowRouteMatches({
slug: 'first',
}).toString(),
}, },
}) })
)
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toBe('text/x-component') expect(res.headers.get('content-type')).toBe('text/x-component')
...@@ -158,7 +166,11 @@ describe('required server files app router', () => { ...@@ -158,7 +166,11 @@ describe('required server files app router', () => {
} }
it('should properly stream resume with Next-Resume', async () => { it('should properly stream resume with Next-Resume', async () => {
const res = await fetchViaHTTP(appPort, '/delayed', undefined, { const res = await fetchViaHTTP(
appPort,
'/delayed',
undefined,
withInvocationId({
headers: { headers: {
'x-matched-path': '/delayed', 'x-matched-path': '/delayed',
'next-resume': '1', 'next-resume': '1',
...@@ -166,6 +178,7 @@ describe('required server files app router', () => { ...@@ -166,6 +178,7 @@ describe('required server files app router', () => {
method: 'POST', method: 'POST',
body: delayedPostpone, body: delayedPostpone,
}) })
)
expect(res.status).toBe(200) expect(res.status).toBe(200)
...@@ -192,13 +205,18 @@ describe('required server files app router', () => { ...@@ -192,13 +205,18 @@ describe('required server files app router', () => {
}) })
it('should properly handle prerender for bot request', async () => { it('should properly handle prerender for bot request', async () => {
const res = await fetchViaHTTP(appPort, '/isr/first', undefined, { const res = await fetchViaHTTP(
appPort,
'/isr/first',
undefined,
withInvocationId({
headers: { headers: {
'user-agent': 'user-agent':
'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.179 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)', 'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.179 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
'x-matched-path': '/isr/first', 'x-matched-path': '/isr/first',
}, },
}) })
)
expect(res.status).toBe(200) expect(res.status).toBe(200)
const html = await res.text() const html = await res.text()
...@@ -206,19 +224,28 @@ describe('required server files app router', () => { ...@@ -206,19 +224,28 @@ describe('required server files app router', () => {
expect($('#page').text()).toBe('/isr/[slug]') expect($('#page').text()).toBe('/isr/[slug]')
const rscRes = await fetchViaHTTP(appPort, '/isr/first.rsc', undefined, { const rscRes = await fetchViaHTTP(
appPort,
'/isr/first.rsc',
undefined,
withInvocationId({
headers: { headers: {
'user-agent': 'user-agent':
'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.179 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)', 'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.179 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
'x-matched-path': '/isr/first', 'x-matched-path': '/isr/first',
}, },
}) })
)
expect(rscRes.status).toBe(200) expect(rscRes.status).toBe(200)
}) })
it('should properly handle fallback for bot request', async () => { it('should properly handle fallback for bot request', async () => {
const res = await fetchViaHTTP(appPort, '/isr/[slug]', undefined, { const res = await fetchViaHTTP(
appPort,
'/isr/[slug]',
undefined,
withInvocationId({
headers: { headers: {
'user-agent': 'user-agent':
'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.179 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)', 'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.179 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
...@@ -228,6 +255,7 @@ describe('required server files app router', () => { ...@@ -228,6 +255,7 @@ describe('required server files app router', () => {
'x-matched-path': '/isr/[slug]', 'x-matched-path': '/isr/[slug]',
}, },
}) })
)
expect(res.status).toBe(200) expect(res.status).toBe(200)
const html = await res.text() const html = await res.text()
...@@ -235,7 +263,11 @@ describe('required server files app router', () => { ...@@ -235,7 +263,11 @@ describe('required server files app router', () => {
expect($('#page').text()).toBe('/isr/[slug]') expect($('#page').text()).toBe('/isr/[slug]')
const rscRes = await fetchViaHTTP(appPort, '/isr/[slug].rsc', undefined, { const rscRes = await fetchViaHTTP(
appPort,
'/isr/[slug].rsc',
undefined,
withInvocationId({
headers: { headers: {
'user-agent': 'user-agent':
'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.179 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)', 'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.179 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
...@@ -245,6 +277,7 @@ describe('required server files app router', () => { ...@@ -245,6 +277,7 @@ describe('required server files app router', () => {
'x-matched-path': '/isr/[slug]', 'x-matched-path': '/isr/[slug]',
}, },
}) })
)
expect(rscRes.status).toBe(200) expect(rscRes.status).toBe(200)
}) })
...@@ -255,14 +288,14 @@ describe('required server files app router', () => { ...@@ -255,14 +288,14 @@ describe('required server files app router', () => {
appPort, appPort,
'/rewrite-with-cookie', '/rewrite-with-cookie',
undefined, undefined,
{ withInvocationId({
method: 'POST', method: 'POST',
headers: { headers: {
'x-matched-path': '/rewrite/first-cookie', 'x-matched-path': '/rewrite/first-cookie',
'next-resume': '1', 'next-resume': '1',
}, },
body: rewritePostpone, body: rewritePostpone,
} })
) )
expect(res.status).toBe(200) expect(res.status).toBe(200)
...@@ -277,7 +310,11 @@ describe('required server files app router', () => { ...@@ -277,7 +310,11 @@ describe('required server files app router', () => {
it('should still render when postponed is corrupted with Next-Resume', async () => { it('should still render when postponed is corrupted with Next-Resume', async () => {
const random = Math.random().toString(36).substring(2) const random = Math.random().toString(36).substring(2)
const res = await fetchViaHTTP(appPort, '/dyn/' + random, undefined, { const res = await fetchViaHTTP(
appPort,
'/dyn/' + random,
undefined,
withInvocationId({
method: 'POST', method: 'POST',
headers: { headers: {
'x-matched-path': '/dyn/[slug]', 'x-matched-path': '/dyn/[slug]',
...@@ -286,6 +323,7 @@ describe('required server files app router', () => { ...@@ -286,6 +323,7 @@ describe('required server files app router', () => {
// This is a corrupted postponed JSON payload. // This is a corrupted postponed JSON payload.
body: '{', body: '{',
}) })
)
expect(res.status).toBe(200) expect(res.status).toBe(200)
...@@ -316,9 +354,14 @@ describe('required server files app router', () => { ...@@ -316,9 +354,14 @@ describe('required server files app router', () => {
], ],
]) { ]) {
require('console').error('checking', { path, tags }) require('console').error('checking', { path, tags })
const res = await fetchViaHTTP(appPort, path, undefined, { const res = await fetchViaHTTP(
appPort,
path,
undefined,
withInvocationId({
redirect: 'manual', redirect: 'manual',
}) })
)
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(res.headers.get('x-next-cache-tags')).toBe(tags) expect(res.headers.get('x-next-cache-tags')).toBe(tags)
} }
...@@ -331,9 +374,15 @@ describe('required server files app router', () => { ...@@ -331,9 +374,15 @@ describe('required server files app router', () => {
'/api/ssr/first', '/api/ssr/first',
'/api/ssr/second', '/api/ssr/second',
]) { ]) {
const res = await fetchViaHTTP(appPort, path, undefined, { const res = await fetchViaHTTP(
appPort,
path,
undefined,
withInvocationId({
redirect: 'manual', redirect: 'manual',
}) })
)
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(res.headers.get('x-next-cache-tags')).toBeFalsy() expect(res.headers.get('x-next-cache-tags')).toBeFalsy()
} }
...@@ -350,9 +399,9 @@ describe('required server files app router', () => { ...@@ -350,9 +399,9 @@ describe('required server files app router', () => {
appPort, appPort,
path, path,
{ hello: 'world' }, { hello: 'world' },
{ withInvocationId({
redirect: 'manual', redirect: 'manual',
} })
) )
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(res.headers.get('x-next-cache-tags')).toBeFalsy() expect(res.headers.get('x-next-cache-tags')).toBeFalsy()
...@@ -360,11 +409,19 @@ describe('required server files app router', () => { ...@@ -360,11 +409,19 @@ describe('required server files app router', () => {
}) })
it('should handle RSC requests', async () => { it('should handle RSC requests', async () => {
const res = await fetchViaHTTP(appPort, '/dyn/first.rsc', undefined, { const res = await fetchViaHTTP(
appPort,
'/dyn/first.rsc',
undefined,
withInvocationId({
headers: { headers: {
'x-matched-path': '/dyn/[slug]', 'x-matched-path': '/dyn/[slug]',
'x-now-route-matches': createNowRouteMatches({
slug: 'first',
}).toString(),
}, },
}) })
)
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toEqual('text/x-component') expect(res.headers.get('content-type')).toEqual('text/x-component')
...@@ -376,11 +433,11 @@ describe('required server files app router', () => { ...@@ -376,11 +433,11 @@ describe('required server files app router', () => {
appPort, appPort,
'/dyn/first.prefetch.rsc', '/dyn/first.prefetch.rsc',
undefined, undefined,
{ withInvocationId({
headers: { headers: {
'x-matched-path': '/dyn/[slug]', 'x-matched-path': '/dyn/[slug]',
}, },
} })
) )
expect(res.status).toBe(200) expect(res.status).toBe(200)
...@@ -389,7 +446,11 @@ describe('required server files app router', () => { ...@@ -389,7 +446,11 @@ describe('required server files app router', () => {
}) })
it('should handle revalidating the fallback page', async () => { it('should handle revalidating the fallback page', async () => {
const res = await fetchViaHTTP(appPort, '/postpone/isr/[slug]', undefined, { const res = await fetchViaHTTP(
appPort,
'/postpone/isr/[slug]',
undefined,
withInvocationId({
headers: { headers: {
'x-matched-path': '/postpone/isr/[slug]', 'x-matched-path': '/postpone/isr/[slug]',
// We don't include the `x-now-route-matches` header because we want to // We don't include the `x-now-route-matches` header because we want to
...@@ -397,6 +458,7 @@ describe('required server files app router', () => { ...@@ -397,6 +458,7 @@ describe('required server files app router', () => {
'x-now-route-matches': '', 'x-now-route-matches': '',
}, },
}) })
)
expect(res.status).toBe(200) expect(res.status).toBe(200)
......
...@@ -95,7 +95,7 @@ describe('minimal-mode-response-cache', () => { ...@@ -95,7 +95,7 @@ describe('minimal-mode-response-cache', () => {
vary: 'rsc, next-router-state-tree, next-router-prefetch', vary: 'rsc, next-router-state-tree, next-router-prefetch',
'x-now-route-matches': '1=compare&rsc=1', 'x-now-route-matches': '1=compare&rsc=1',
'x-matched-path': '/app-blog/compare.rsc', 'x-matched-path': '/app-blog/compare.rsc',
'x-vercel-id': '1', 'x-invocation-id': '1',
rsc: '1', rsc: '1',
} }
const res1 = await fetchViaHTTP( const res1 = await fetchViaHTTP(
...@@ -125,7 +125,7 @@ describe('minimal-mode-response-cache', () => { ...@@ -125,7 +125,7 @@ describe('minimal-mode-response-cache', () => {
vary: 'rsc, next-router-state-tree, next-router-prefetch', vary: 'rsc, next-router-state-tree, next-router-prefetch',
'x-now-route-matches': '1=app-another&rsc=1', 'x-now-route-matches': '1=app-another&rsc=1',
'x-matched-path': '/app-another.rsc', 'x-matched-path': '/app-another.rsc',
'x-vercel-id': '1', 'x-invocation-id': '1',
rsc: '1', rsc: '1',
} }
const res1 = await fetchViaHTTP(appPort, '/app-another.rsc', undefined, { const res1 = await fetchViaHTTP(appPort, '/app-another.rsc', undefined, {
......
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