Unverified Commit 092458fd authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

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

## Summary

Implements an LRU cache with compound keys for the minimal mode response
cache to improve cache hit rates during parallel revalidation scenarios.

**Problem**: The previous single-entry cache (`previousCacheItem`) keyed
by pathname caused cache collisions when multiple concurrent invocations
(e.g., during ISR revalidation) accessed the same pathname. Each
invocation would overwrite the previous entry, leading to cache misses
and redundant work.

**Solution**: An LRU cache using compound keys (`pathname +
invocationID`) that allows multiple invocations to cache entries for the
same pathname independently:

```
Cache Key Structure
─────────────────────
/blog/post-1\0inv-abc  →  {entry, expiresAt}
/blog/post-1\0inv-def  →  {entry, expiresAt}
/blog/post-1\0__ttl__  →  {entry, expiresAt}  (TTL fallback)
/api/data\0inv-ghi     →  {entry, expiresAt}
```

### Cache Key Strategy

- **With `x-invocation-id` header**: Entries are keyed by invocation ID
for exact-match lookups (always a cache hit if the entry exists)
- **Without header (TTL fallback)**: Entries use a `__ttl__` sentinel
key and validate via expiration timestamp

### Configuration via Environment Variables

Cache sizing can be tuned via environment variables (using
`NEXT_PRIVATE_*` prefix for infrastructure-level settings):

| Environment Variable | Default | Description |
|---------------------|---------|-------------|
| `NEXT_PRIVATE_RESPONSE_CACHE_MAX_SIZE` | 150 | Max entries in the LRU
cache |
| `NEXT_PRIVATE_RESPONSE_CACHE_TTL` | 10000 | TTL in ms for cache
entries (fallback validation) |

### LRU Cache Enhancement

Added an optional `onEvict` callback to `LRUCache` that fires when
entries are evicted due to capacity limits. This enables tracking
evicted invocation IDs for warning detection without introducing
timer-based cleanup.

### Eviction Warnings

When a cache entry is evicted and later accessed by the same invocation,
a warning is logged suggesting to increase
`NEXT_PRIVATE_RESPONSE_CACHE_MAX_SIZE`. This helps developers tune cache
sizes for their workload.

### Additional Changes

- Renamed header from `x-vercel-id` to `x-invocation-id` for clarity
- Added `withInvocationId()` test helper for cache testing

## Test Plan

- Existing response cache tests pass with updated header name
- Unit tests for `LRUCache` including `onEvict` callback behavior
- Updated standalone mode tests to use `withInvocationId()` helper
parent 4e8671cf
...@@ -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 {
......
...@@ -1018,6 +1018,9 @@ export abstract class RouteModule< ...@@ -1018,6 +1018,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'
...@@ -199,6 +200,37 @@ export function fetchViaHTTP( ...@@ -199,6 +200,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'
...@@ -103,7 +104,11 @@ describe('required server files app router', () => { ...@@ -103,7 +104,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({
...@@ -111,6 +116,7 @@ describe('required server files app router', () => { ...@@ -111,6 +116,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')
}) })
...@@ -120,7 +126,7 @@ describe('required server files app router', () => { ...@@ -120,7 +126,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({
...@@ -129,7 +135,7 @@ describe('required server files app router', () => { ...@@ -129,7 +135,7 @@ describe('required server files app router', () => {
slug: 'slug', slug: 'slug',
}).toString(), }).toString(),
}, },
} })
) )
expect(res.status).toBe(200) expect(res.status).toBe(200)
...@@ -142,7 +148,7 @@ describe('required server files app router', () => { ...@@ -142,7 +148,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({
...@@ -150,7 +156,7 @@ describe('required server files app router', () => { ...@@ -150,7 +156,7 @@ describe('required server files app router', () => {
flags: 'flags', flags: 'flags',
}).toString(), }).toString(),
}, },
} })
) )
expect(res.status).toBe(200) expect(res.status).toBe(200)
...@@ -162,7 +168,11 @@ describe('required server files app router', () => { ...@@ -162,7 +168,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({
...@@ -170,6 +180,7 @@ describe('required server files app router', () => { ...@@ -170,6 +180,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'
...@@ -181,13 +192,18 @@ describe('required server files app router', () => { ...@@ -181,13 +192,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()
...@@ -195,19 +211,28 @@ describe('required server files app router', () => { ...@@ -195,19 +211,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)',
...@@ -217,6 +242,7 @@ describe('required server files app router', () => { ...@@ -217,6 +242,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()
...@@ -224,7 +250,11 @@ describe('required server files app router', () => { ...@@ -224,7 +250,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)',
...@@ -234,6 +264,7 @@ describe('required server files app router', () => { ...@@ -234,6 +264,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)
}) })
...@@ -258,9 +289,14 @@ describe('required server files app router', () => { ...@@ -258,9 +289,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)
} }
...@@ -273,9 +309,14 @@ describe('required server files app router', () => { ...@@ -273,9 +309,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()
} }
...@@ -292,9 +333,9 @@ describe('required server files app router', () => { ...@@ -292,9 +333,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()
...@@ -306,11 +347,11 @@ describe('required server files app router', () => { ...@@ -306,11 +347,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()
...@@ -318,4 +359,80 @@ describe('required server files app router', () => { ...@@ -318,4 +359,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 {
initNextServerScript, initNextServerScript,
killApp, killApp,
retry, retry,
withInvocationId,
} from 'next-test-utils' } from 'next-test-utils'
import { ChildProcess } from 'node:child_process' import { ChildProcess } from 'node:child_process'
...@@ -144,7 +145,11 @@ describe.skip('required server files app router', () => { ...@@ -144,7 +145,11 @@ describe.skip('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': createNowRouteMatches({ 'x-now-route-matches': createNowRouteMatches({
...@@ -152,6 +157,7 @@ describe.skip('required server files app router', () => { ...@@ -152,6 +157,7 @@ describe.skip('required server files app router', () => {
}).toString(), }).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')
...@@ -167,7 +173,11 @@ describe.skip('required server files app router', () => { ...@@ -167,7 +173,11 @@ describe.skip('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',
...@@ -175,6 +185,7 @@ describe.skip('required server files app router', () => { ...@@ -175,6 +185,7 @@ describe.skip('required server files app router', () => {
method: 'POST', method: 'POST',
body: delayedPostpone, body: delayedPostpone,
}) })
)
expect(res.status).toBe(200) expect(res.status).toBe(200)
...@@ -199,13 +210,18 @@ describe.skip('required server files app router', () => { ...@@ -199,13 +210,18 @@ describe.skip('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()
...@@ -213,19 +229,28 @@ describe.skip('required server files app router', () => { ...@@ -213,19 +229,28 @@ describe.skip('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)',
...@@ -235,6 +260,7 @@ describe.skip('required server files app router', () => { ...@@ -235,6 +260,7 @@ describe.skip('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()
...@@ -242,7 +268,11 @@ describe.skip('required server files app router', () => { ...@@ -242,7 +268,11 @@ describe.skip('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)',
...@@ -252,6 +282,7 @@ describe.skip('required server files app router', () => { ...@@ -252,6 +282,7 @@ describe.skip('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)
}) })
...@@ -267,7 +298,7 @@ describe.skip('required server files app router', () => { ...@@ -267,7 +298,7 @@ describe.skip('required server files app router', () => {
// route backed by PPR. // route backed by PPR.
`/_next/data/${next.buildId}/index.json`, `/_next/data/${next.buildId}/index.json`,
undefined, undefined,
{ withInvocationId({
method: 'POST', method: 'POST',
headers: { headers: {
'x-matched-path': '/[...catchAll]', 'x-matched-path': '/[...catchAll]',
...@@ -277,7 +308,7 @@ describe.skip('required server files app router', () => { ...@@ -277,7 +308,7 @@ describe.skip('required server files app router', () => {
'next-resume': '1', 'next-resume': '1',
}, },
body: postponed, body: postponed,
} })
) )
// Expect that the status code is 422, we asked for a /_next/data route and // Expect that the status code is 422, we asked for a /_next/data route and
...@@ -301,14 +332,14 @@ describe.skip('required server files app router', () => { ...@@ -301,14 +332,14 @@ describe.skip('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)
...@@ -327,7 +358,11 @@ describe.skip('required server files app router', () => { ...@@ -327,7 +358,11 @@ describe.skip('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]',
...@@ -336,6 +371,7 @@ describe.skip('required server files app router', () => { ...@@ -336,6 +371,7 @@ describe.skip('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)
...@@ -369,9 +405,14 @@ describe.skip('required server files app router', () => { ...@@ -369,9 +405,14 @@ describe.skip('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)
...@@ -390,9 +431,14 @@ describe.skip('required server files app router', () => { ...@@ -390,9 +431,14 @@ describe.skip('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()
...@@ -416,9 +462,9 @@ describe.skip('required server files app router', () => { ...@@ -416,9 +462,9 @@ describe.skip('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)
...@@ -433,7 +479,11 @@ describe.skip('required server files app router', () => { ...@@ -433,7 +479,11 @@ describe.skip('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({ 'x-now-route-matches': createNowRouteMatches({
...@@ -441,6 +491,7 @@ describe.skip('required server files app router', () => { ...@@ -441,6 +491,7 @@ describe.skip('required server files app router', () => {
}).toString(), }).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')
...@@ -466,14 +517,14 @@ describe.skip('required server files app router', () => { ...@@ -466,14 +517,14 @@ describe.skip('required server files app router', () => {
appPort, appPort,
'/rewrite/second-cookie.rsc', '/rewrite/second-cookie.rsc',
undefined, undefined,
{ withInvocationId({
headers: { headers: {
'x-matched-path': '/rewrite/[slug]', 'x-matched-path': '/rewrite/[slug]',
'x-now-route-matches': createNowRouteMatches({ 'x-now-route-matches': createNowRouteMatches({
slug: 'second-cookie', slug: 'second-cookie',
}).toString(), }).toString(),
}, },
} })
) )
expect(res.status).toBe(200) expect(res.status).toBe(200)
...@@ -508,7 +559,11 @@ describe.skip('required server files app router', () => { ...@@ -508,7 +559,11 @@ describe.skip('required server files app router', () => {
// Then let's get the Dynamic RSC request and verify that the random value // Then let's get the Dynamic RSC request and verify that the random value
// is present in the response by passing the postponed state. // is present in the response by passing the postponed state.
res = await fetchViaHTTP(appPort, '/rewrite/second-cookie.rsc', undefined, { res = await fetchViaHTTP(
appPort,
'/rewrite/second-cookie.rsc',
undefined,
withInvocationId({
method: 'POST', method: 'POST',
headers: { headers: {
'x-matched-path': '/rewrite/[slug]', 'x-matched-path': '/rewrite/[slug]',
...@@ -519,6 +574,7 @@ describe.skip('required server files app router', () => { ...@@ -519,6 +574,7 @@ describe.skip('required server files app router', () => {
}, },
body: secondCookiePostpone, body: secondCookiePostpone,
}) })
)
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')
...@@ -551,7 +607,11 @@ describe.skip('required server files app router', () => { ...@@ -551,7 +607,11 @@ describe.skip('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
...@@ -559,6 +619,7 @@ describe.skip('required server files app router', () => { ...@@ -559,6 +619,7 @@ describe.skip('required server files app router', () => {
'x-now-route-matches': '', 'x-now-route-matches': '',
}, },
}) })
)
expect(res.status).toBe(200) expect(res.status).toBe(200)
......
...@@ -96,7 +96,7 @@ describe('minimal-mode-response-cache', () => { ...@@ -96,7 +96,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(
...@@ -126,7 +126,7 @@ describe('minimal-mode-response-cache', () => { ...@@ -126,7 +126,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