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', () => {
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> {
private totalSize: number = 0
private readonly maxSize: number
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.calculateSize = calculateSize
this.onEvict = onEvict
// Create sentinel nodes to simplify doubly-linked list operations
// HEAD <-> TAIL (empty list)
......@@ -144,6 +150,7 @@ export class LRUCache<T> {
const tail = this.removeTail()
this.cache.delete(tail.key)
this.totalSize -= tail.size
this.onEvict?.(tail.key, tail.data)
}
}
......@@ -191,6 +198,10 @@ export class LRUCache<T> {
* Removes a specific key from the cache.
* 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)
*/
public remove(key: string): void {
......
......@@ -7,6 +7,8 @@ import type {
} from './types'
import { Batcher } from '../../lib/batcher'
import { LRUCache } from '../lib/lru-cache'
import { warnOnce } from '../../build/output/log'
import { scheduleOnNextTick } from '../../lib/scheduler'
import {
fromResponseCacheEntry,
......@@ -15,6 +17,92 @@ import {
} from './utils'
import type { RouteKind } from '../route-kind'
/**
* Parses an environment variable as a positive integer, returning the fallback
* if the value is missing, not a number, or not positive.
*/
function parsePositiveInt(
envValue: string | undefined,
fallback: number
): number {
if (!envValue) return fallback
const parsed = parseInt(envValue, 10)
return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback
}
/**
* Default TTL (in milliseconds) for minimal mode response cache entries.
* Used for cache hit validation as a fallback for providers that don't
* send the x-invocation-id header yet.
*
* 10 seconds chosen because:
* - Long enough to dedupe rapid successive requests (e.g., page + data)
* - Short enough to not serve stale data across unrelated requests
*
* Can be configured via `NEXT_PRIVATE_RESPONSE_CACHE_TTL` environment variable.
*/
const DEFAULT_TTL_MS = parsePositiveInt(
process.env.NEXT_PRIVATE_RESPONSE_CACHE_TTL,
10_000
)
/**
* Default maximum number of entries in the response cache.
* Can be configured via `NEXT_PRIVATE_RESPONSE_CACHE_MAX_SIZE` environment variable.
*/
const DEFAULT_MAX_SIZE = parsePositiveInt(
process.env.NEXT_PRIVATE_RESPONSE_CACHE_MAX_SIZE,
150
)
/**
* Separator used in compound cache keys to join pathname and invocationID.
* Using null byte (\0) since it cannot appear in valid URL paths or UUIDs.
*/
const KEY_SEPARATOR = '\0'
/**
* Sentinel value used for TTL-based cache entries (when invocationID is undefined).
* Uses KEY_SEPARATOR prefix to guarantee uniqueness since null bytes cannot appear
* in HTTP headers (RFC 7230), making collision with real invocation IDs impossible.
*/
const TTL_SENTINEL = `${KEY_SEPARATOR}ttl`
/**
* Entry stored in the LRU cache.
*/
type CacheEntry = {
entry: IncrementalResponseCacheEntry | null
/**
* TTL expiration timestamp in milliseconds. Used as a fallback for
* cache hit validation when providers don't send x-invocation-id.
* Memory pressure is managed by LRU eviction rather than timers.
*/
expiresAt: number
}
/**
* Creates a compound cache key from pathname and invocationID.
*/
function createCacheKey(
pathname: string,
invocationID: string | undefined
): string {
return `${pathname}${KEY_SEPARATOR}${invocationID ?? TTL_SENTINEL}`
}
/**
* Extracts the invocationID from a compound cache key.
* Returns undefined if the key used TTL_SENTINEL.
*/
function extractInvocationID(compoundKey: string): string | undefined {
const separatorIndex = compoundKey.lastIndexOf(KEY_SEPARATOR)
if (separatorIndex === -1) return undefined
const invocationID = compoundKey.slice(separatorIndex + 1)
return invocationID === TTL_SENTINEL ? undefined : invocationID
}
export * from './types'
export default class ResponseCache implements ResponseCacheBase {
......@@ -43,19 +131,63 @@ export default class ResponseCache implements ResponseCacheBase {
schedulerFn: scheduleOnNextTick,
})
private previousCacheItem?: {
key: string
entry: IncrementalResponseCacheEntry | null
expiresAt: number
}
/**
* LRU cache for minimal mode using compound keys (pathname + invocationID).
* This allows multiple invocations to cache the same pathname without
* overwriting each other's entries.
*/
private readonly cache: LRUCache<CacheEntry>
/**
* Set of invocation IDs that have had cache entries evicted.
* Used to detect when the cache size may be too small.
* Bounded to prevent memory growth.
*/
private readonly evictedInvocationIDs: Set<string> = new Set()
/**
* The configured max size, stored for logging.
*/
private readonly maxSize: number
/**
* The configured TTL for cache entries in milliseconds.
*/
private readonly ttl: number
// we don't use minimal_mode name here as this.minimal_mode is
// statically replace for server runtimes but we need it to
// be dynamic here
private minimal_mode?: boolean
constructor(minimal_mode: boolean) {
constructor(
minimal_mode: boolean,
maxSize: number = DEFAULT_MAX_SIZE,
ttl: number = DEFAULT_TTL_MS
) {
this.minimal_mode = minimal_mode
this.maxSize = maxSize
this.ttl = ttl
// Create the LRU cache with eviction tracking
this.cache = new LRUCache(maxSize, undefined, (compoundKey) => {
const invocationID = extractInvocationID(compoundKey)
if (invocationID) {
// Bound to 100 entries to prevent unbounded memory growth.
// FIFO eviction is acceptable here because:
// 1. Invocations are short-lived (single request lifecycle), so older
// invocations are unlikely to still be active after 100 newer ones
// 2. This warning mechanism is best-effort for developer guidance—
// missing occasional eviction warnings doesn't affect correctness
// 3. If a long-running invocation is somehow evicted and then has
// another cache entry evicted, it will simply be re-added
if (this.evictedInvocationIDs.size >= 100) {
const first = this.evictedInvocationIDs.values().next().value
if (first) this.evictedInvocationIDs.delete(first)
}
this.evictedInvocationIDs.add(invocationID)
}
})
}
/**
......@@ -77,6 +209,12 @@ export default class ResponseCache implements ResponseCacheBase {
isRoutePPREnabled?: boolean
isFallback?: boolean
waitUntil?: (prom: Promise<any>) => void
/**
* The invocation ID from the infrastructure. Used to scope the
* in-memory cache to a single revalidation request in minimal mode.
*/
invocationID?: string
}
): Promise<ResponseCacheEntry | null> {
// If there is no key for the cache, we can't possibly look this up in the
......@@ -88,13 +226,38 @@ export default class ResponseCache implements ResponseCacheBase {
})
}
// Check minimal mode cache before doing any other work
if (
this.minimal_mode &&
this.previousCacheItem?.key === key &&
this.previousCacheItem.expiresAt > Date.now()
) {
return toResponseCacheEntry(this.previousCacheItem.entry)
// Check minimal mode cache before doing any other work.
if (this.minimal_mode) {
const cacheKey = createCacheKey(key, context.invocationID)
const cachedItem = this.cache.get(cacheKey)
if (cachedItem) {
// With invocationID: exact match found - always a hit
// With TTL mode: must check expiration
if (context.invocationID !== undefined) {
return toResponseCacheEntry(cachedItem.entry)
}
// TTL mode: check expiration
const now = Date.now()
if (cachedItem.expiresAt > now) {
return toResponseCacheEntry(cachedItem.entry)
}
// TTL expired - clean up
this.cache.remove(cacheKey)
}
// Warn if this invocation had entries evicted - indicates cache may be too small.
if (
context.invocationID &&
this.evictedInvocationIDs.has(context.invocationID)
) {
warnOnce(
`Response cache entry was evicted for invocation ${context.invocationID}. ` +
`Consider increasing NEXT_PRIVATE_RESPONSE_CACHE_MAX_SIZE (current: ${this.maxSize}).`
)
}
}
const {
......@@ -105,6 +268,7 @@ export default class ResponseCache implements ResponseCacheBase {
isPrefetch = false,
waitUntil,
routeKind,
invocationID,
} = context
const response = await this.getBatcher.batch(
......@@ -120,6 +284,7 @@ export default class ResponseCache implements ResponseCacheBase {
isRoutePPREnabled,
isPrefetch,
routeKind,
invocationID,
},
resolve
)
......@@ -153,6 +318,7 @@ export default class ResponseCache implements ResponseCacheBase {
isRoutePPREnabled: boolean
isPrefetch: boolean
routeKind: RouteKind
invocationID: string | undefined
},
resolve: (value: IncrementalResponseCacheEntry | null) => void
): Promise<IncrementalResponseCacheEntry | null> {
......@@ -188,13 +354,18 @@ export default class ResponseCache implements ResponseCacheBase {
context.isFallback,
responseGenerator,
previousIncrementalCacheEntry,
previousIncrementalCacheEntry !== null && !context.isOnDemandRevalidate
previousIncrementalCacheEntry !== null && !context.isOnDemandRevalidate,
undefined,
context.invocationID
)
// Handle null response
if (!incrementalResponseCacheEntry) {
// Unset the previous cache item if it was set so we don't use it again.
if (this.minimal_mode) this.previousCacheItem = undefined
// Remove the cache item if it was set so we don't use it again.
if (this.minimal_mode) {
const cacheKey = createCacheKey(key, context.invocationID)
this.cache.remove(cacheKey)
}
return null
}
......@@ -226,6 +397,8 @@ export default class ResponseCache implements ResponseCacheBase {
* @param responseGenerator - The response generator to use to generate the response cache entry.
* @param previousIncrementalCacheEntry - The previous cache entry to use to revalidate the cache entry.
* @param hasResolved - Whether the response has been resolved.
* @param waitUntil - Optional function to register background work.
* @param invocationID - The invocation ID for cache key scoping.
* @returns The revalidated cache entry.
*/
public async revalidate(
......@@ -236,7 +409,8 @@ export default class ResponseCache implements ResponseCacheBase {
responseGenerator: ResponseGenerator,
previousIncrementalCacheEntry: IncrementalResponseCacheEntry | null,
hasResolved: boolean,
waitUntil?: (prom: Promise<any>) => void
waitUntil?: (prom: Promise<any>) => void,
invocationID?: string
) {
return this.revalidateBatcher.batch(key, () => {
const promise = this.handleRevalidate(
......@@ -246,7 +420,8 @@ export default class ResponseCache implements ResponseCacheBase {
isFallback,
responseGenerator,
previousIncrementalCacheEntry,
hasResolved
hasResolved,
invocationID
)
// We need to ensure background revalidates are passed to waitUntil.
......@@ -263,7 +438,8 @@ export default class ResponseCache implements ResponseCacheBase {
isFallback: boolean,
responseGenerator: ResponseGenerator,
previousIncrementalCacheEntry: IncrementalResponseCacheEntry | null,
hasResolved: boolean
hasResolved: boolean,
invocationID: string | undefined
) {
try {
// Generate the response cache entry using the response generator.
......@@ -286,11 +462,14 @@ export default class ResponseCache implements ResponseCacheBase {
// defined.
if (incrementalResponseCacheEntry.cacheControl) {
if (this.minimal_mode) {
this.previousCacheItem = {
key,
// Set TTL expiration for cache hit validation. Entries are validated
// by invocationID when available, with TTL as a fallback for providers
// that don't send x-invocation-id. Memory is managed by LRU eviction.
const cacheKey = createCacheKey(key, invocationID)
this.cache.set(cacheKey, {
entry: incrementalResponseCacheEntry,
expiresAt: Date.now() + 1000,
}
expiresAt: Date.now() + this.ttl,
})
} else {
await incrementalCache.set(key, incrementalResponseCacheEntry.value, {
cacheControl: incrementalResponseCacheEntry.cacheControl,
......
......@@ -1018,6 +1018,9 @@ export abstract class RouteModule<
isRoutePPREnabled,
isOnDemandRevalidate,
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(
req,
nextConfig,
......
......@@ -16,6 +16,7 @@ import { writeFile } from 'fs-extra'
import getPort from 'get-port'
import { getRandomPort } from 'get-port-please'
import fetch from 'node-fetch'
import { nanoid } from 'nanoid'
import qs from 'querystring'
import treeKill from 'tree-kill'
import { once } from 'events'
......@@ -199,6 +200,37 @@ export function fetchViaHTTP(
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(
appPort: string | number,
pathname: string,
......
......@@ -3,8 +3,14 @@
import fs from 'fs-extra'
import { join } from 'path'
import cheerio from 'cheerio'
import { nextServer, startApp, waitFor } from 'next-test-utils'
import { fetchViaHTTP, renderViaHTTP } from 'next-test-utils'
import {
fetchViaHTTP,
nextServer,
renderViaHTTP,
startApp,
waitFor,
withInvocationId,
} from 'next-test-utils'
import { nextTestSetup } from 'e2e-utils'
describe('Required Server Files', () => {
......@@ -90,14 +96,24 @@ describe('Required Server Files', () => {
})
it('should render SSR page correctly', async () => {
const html = await renderViaHTTP(appPort, '/')
const html = await renderViaHTTP(
appPort,
'/',
undefined,
withInvocationId()
)
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
expect($('#index').text()).toBe('index page')
expect(data.hello).toBe('world')
const html2 = await renderViaHTTP(appPort, '/')
const html2 = await renderViaHTTP(
appPort,
'/',
undefined,
withInvocationId()
)
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
......@@ -107,7 +123,12 @@ describe('Required Server Files', () => {
})
it('should render dynamic SSR page correctly', async () => {
const html = await renderViaHTTP(appPort, '/dynamic/first')
const html = await renderViaHTTP(
appPort,
'/dynamic/first',
undefined,
withInvocationId()
)
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
......@@ -115,7 +136,12 @@ describe('Required Server Files', () => {
expect($('#slug').text()).toBe('first')
expect(data.hello).toBe('world')
const html2 = await renderViaHTTP(appPort, '/dynamic/second')
const html2 = await renderViaHTTP(
appPort,
'/dynamic/second',
undefined,
withInvocationId()
)
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
......@@ -126,7 +152,12 @@ describe('Required Server Files', () => {
})
it('should render fallback page correctly', async () => {
const html = await renderViaHTTP(appPort, '/fallback/first')
const html = await renderViaHTTP(
appPort,
'/fallback/first',
undefined,
withInvocationId()
)
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
......@@ -135,7 +166,12 @@ describe('Required Server Files', () => {
expect(data.hello).toBe('world')
await waitFor(2000)
const html2 = await renderViaHTTP(appPort, '/fallback/first')
const html2 = await renderViaHTTP(
appPort,
'/fallback/first',
undefined,
withInvocationId()
)
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
......@@ -144,7 +180,12 @@ describe('Required Server Files', () => {
expect(isNaN(data2.random)).toBe(false)
expect(data2.random).not.toBe(data.random)
const html3 = await renderViaHTTP(appPort, '/fallback/second')
const html3 = await renderViaHTTP(
appPort,
'/fallback/second',
undefined,
withInvocationId()
)
const $3 = cheerio.load(html3)
const data3 = JSON.parse($3('#props').text())
......@@ -155,7 +196,9 @@ describe('Required Server Files', () => {
const { pageProps: data4 } = JSON.parse(
await renderViaHTTP(
appPort,
`/_next/data/${buildId}/fallback/third.json`
`/_next/data/${buildId}/fallback/third.json`,
undefined,
withInvocationId()
)
)
expect(data4.hello).toBe('world')
......@@ -167,11 +210,11 @@ describe('Required Server Files', () => {
appPort,
'/some-other-path',
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/',
},
}
})
)
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
......@@ -183,11 +226,11 @@ describe('Required Server Files', () => {
appPort,
'/some-other-path',
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/',
},
}
})
)
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
......@@ -202,11 +245,11 @@ describe('Required Server Files', () => {
appPort,
'/some-other-path?nxtPslug=first',
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/dynamic/[slug]',
},
}
})
)
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
......@@ -219,11 +262,11 @@ describe('Required Server Files', () => {
appPort,
'/some-other-path?slug=second',
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/dynamic/[slug]',
},
}
})
)
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
......@@ -239,12 +282,12 @@ describe('Required Server Files', () => {
appPort,
'/fallback/first',
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/fallback/first',
'x-now-route-matches': 'nxtPslug=first',
},
}
})
)
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
......@@ -257,12 +300,12 @@ describe('Required Server Files', () => {
appPort,
`/fallback/[slug]`,
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/fallback/[slug]',
'x-now-route-matches': 'nxtPslug=second',
},
}
})
)
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
......@@ -278,11 +321,11 @@ describe('Required Server Files', () => {
appPort,
`/_next/data/${buildId}/dynamic/first.json?nxtPslug=first`,
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/dynamic/[slug]',
},
}
})
)
const { pageProps: data } = await res.json()
......@@ -294,12 +337,12 @@ describe('Required Server Files', () => {
appPort,
`/_next/data/${buildId}/fallback/[slug].json`,
undefined,
{
withInvocationId({
headers: {
'x-matched-path': `/_next/data/${buildId}/fallback/[slug].json`,
'x-now-route-matches': 'nxtPslug=second',
},
}
})
)
const { pageProps: data2 } = await res2.json()
......@@ -313,12 +356,12 @@ describe('Required Server Files', () => {
appPort,
'/catch-all/[[...rest]]',
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/catch-all/[[...rest]]',
'x-now-route-matches': '',
},
}
})
)
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
......@@ -331,12 +374,12 @@ describe('Required Server Files', () => {
appPort,
'/catch-all/[[...rest]]',
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/catch-all/[[...rest]]',
'x-now-route-matches': 'nxtPrest=hello',
},
}
})
)
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
......@@ -350,13 +393,13 @@ describe('Required Server Files', () => {
appPort,
'/catch-all/[[...rest]]',
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/catch-all/[[...rest]]',
'x-now-route-matches':
'nxtPrest=hello/world&catchAll=hello/world',
},
}
})
)
const $3 = cheerio.load(html3)
const data3 = JSON.parse($3('#props').text())
......@@ -370,11 +413,11 @@ describe('Required Server Files', () => {
appPort,
'/catch-all/[[...rest]]',
{ nxtPrest: 'frank' },
{
withInvocationId({
headers: {
'x-matched-path': '/catch-all/[[...rest]]',
},
}
})
)
const $4 = cheerio.load(html4)
const data4 = JSON.parse($4('#props').text())
......@@ -388,11 +431,11 @@ describe('Required Server Files', () => {
appPort,
'/catch-all/[[...rest]]',
{},
{
withInvocationId({
headers: {
'x-matched-path': '/catch-all/[[...rest]]',
},
}
})
)
const $5 = cheerio.load(html5)
const data5 = JSON.parse($5('#props').text())
......@@ -406,11 +449,11 @@ describe('Required Server Files', () => {
appPort,
'/catch-all/[[...rest]]',
{ nxtPrest: 'frank' },
{
withInvocationId({
headers: {
'x-matched-path': '/catch-all/[[...rest]]',
},
}
})
)
const $6 = cheerio.load(html6)
const data6 = JSON.parse($6('#props').text())
......@@ -443,11 +486,11 @@ describe('Required Server Files', () => {
appPort,
'/partial-catch-all/[domain]/[[...rest]]',
query,
{
withInvocationId({
headers: {
'x-matched-path': '/partial-catch-all/[domain]/[[...rest]]',
},
}
})
)
const $ = cheerio.load(html)
......@@ -464,11 +507,11 @@ describe('Required Server Files', () => {
appPort,
`/_next/data/${buildId}/catch-all.json`,
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/catch-all/[[...rest]]',
},
}
})
)
const { pageProps: data } = await res.json()
......@@ -480,12 +523,12 @@ describe('Required Server Files', () => {
appPort,
`/_next/data/${buildId}/catch-all/[[...rest]].json`,
undefined,
{
withInvocationId({
headers: {
'x-matched-path': `/_next/data/${buildId}/catch-all/[[...rest]].json`,
'x-now-route-matches': 'nxtPrest=hello&rest=hello',
},
}
})
)
const { pageProps: data2 } = await res2.json()
......@@ -497,12 +540,12 @@ describe('Required Server Files', () => {
appPort,
`/_next/data/${buildId}/catch-all/[[...rest]].json`,
undefined,
{
withInvocationId({
headers: {
'x-matched-path': `/_next/data/${buildId}/catch-all/[[...rest]].json`,
'x-now-route-matches': 'nxtPrest=hello/world&rest=hello/world',
},
}
})
)
const { pageProps: data3 } = await res3.json()
......@@ -521,9 +564,14 @@ describe('Required Server Files', () => {
'/fallback/another/',
'/fallback/another',
]) {
const res = await fetchViaHTTP(appPort, path, undefined, {
redirect: 'manual',
})
const res = await fetchViaHTTP(
appPort,
path,
undefined,
withInvocationId({
redirect: 'manual',
})
)
expect(res.status).toBe(200)
}
......@@ -536,11 +584,11 @@ describe('Required Server Files', () => {
{
path: 'hello/world',
},
{
withInvocationId({
headers: {
'x-matched-path': '/',
},
}
})
)
const $ = cheerio.load(html)
expect(JSON.parse($('#router').text()).query).toEqual({
......@@ -549,19 +597,34 @@ describe('Required Server Files', () => {
})
it('should bubble error correctly for gip page', async () => {
const res = await fetchViaHTTP(appPort, '/errors/gip', { crash: '1' })
const res = await fetchViaHTTP(
appPort,
'/errors/gip',
{ crash: '1' },
withInvocationId()
)
expect(res.status).toBe(500)
expect(await res.text()).toBe('Internal Server Error')
})
it('should bubble error correctly for gssp page', async () => {
const res = await fetchViaHTTP(appPort, '/errors/gssp', { crash: '1' })
const res = await fetchViaHTTP(
appPort,
'/errors/gssp',
{ crash: '1' },
withInvocationId()
)
expect(res.status).toBe(500)
expect(await res.text()).toBe('Internal Server Error')
})
it('should bubble error correctly for gsp page', async () => {
const res = await fetchViaHTTP(appPort, '/errors/gsp/crash')
const res = await fetchViaHTTP(
appPort,
'/errors/gsp/crash',
undefined,
withInvocationId()
)
expect(res.status).toBe(500)
expect(await res.text()).toBe('Internal Server Error')
})
......@@ -571,11 +634,11 @@ describe('Required Server Files', () => {
appPort,
'/optional-ssp',
{ nxtPrest: '', another: 'value' },
{
withInvocationId({
headers: {
'x-matched-path': '/optional-ssp/[[...rest]]',
},
}
})
)
const html = await res.text()
......@@ -590,11 +653,11 @@ describe('Required Server Files', () => {
appPort,
'/optional-ssg',
{ rest: '', another: 'value' },
{
withInvocationId({
headers: {
'x-matched-path': '/optional-ssg/[[...rest]]',
},
}
})
)
const html = await res.text()
......@@ -608,11 +671,11 @@ describe('Required Server Files', () => {
appPort,
'/api/optional',
{ nxtPrest: '', another: 'value' },
{
withInvocationId({
headers: {
'x-matched-path': '/api/optional/[[...rest]]',
},
}
})
)
const json = await res.json()
......@@ -621,12 +684,17 @@ describe('Required Server Files', () => {
})
it('should match the index page correctly', async () => {
const res = await fetchViaHTTP(appPort, '/', undefined, {
headers: {
'x-matched-path': '/index',
},
redirect: 'manual',
})
const res = await fetchViaHTTP(
appPort,
'/',
undefined,
withInvocationId({
headers: {
'x-matched-path': '/index',
},
redirect: 'manual',
})
)
const html = await res.text()
const $ = cheerio.load(html)
......@@ -634,12 +702,17 @@ describe('Required Server Files', () => {
})
it('should match the root dynamic page correctly', async () => {
const res = await fetchViaHTTP(appPort, '/slug-1', undefined, {
headers: {
'x-matched-path': '/[slug]',
},
redirect: 'manual',
})
const res = await fetchViaHTTP(
appPort,
'/slug-1',
undefined,
withInvocationId({
headers: {
'x-matched-path': '/[slug]',
},
redirect: 'manual',
})
)
const html = await res.text()
const $ = cheerio.load(html)
......@@ -652,12 +725,17 @@ describe('Required Server Files', () => {
'/non-existent',
'/404',
]) {
const res = await fetchViaHTTP(appPort, pathname, undefined, {
headers: {
'x-matched-path': '/404',
redirect: 'manual',
},
})
const res = await fetchViaHTTP(
appPort,
pathname,
undefined,
withInvocationId({
headers: {
'x-matched-path': '/404',
redirect: 'manual',
},
})
)
expect(res.status).toBe(404)
expect(await res.text()).toContain('custom 404')
}
......@@ -666,12 +744,17 @@ describe('Required Server Files', () => {
'/_next/static/chunks/pages/index-abc123.js',
'/_next/static/some-file.js',
]) {
const res = await fetchViaHTTP(appPort, pathname, undefined, {
headers: {
'x-matched-path': '/404',
redirect: 'manual',
},
})
const res = await fetchViaHTTP(
appPort,
pathname,
undefined,
withInvocationId({
headers: {
'x-matched-path': '/404',
redirect: 'manual',
},
})
)
expect(res.status).toBe(404)
expect(res.headers.get('content-type')).toBe(
'text/plain; charset=utf-8'
......
......@@ -10,6 +10,7 @@ import {
findPort,
initNextServerScript,
killApp,
withInvocationId,
} from 'next-test-utils'
import { ChildProcess } from 'child_process'
......@@ -103,14 +104,19 @@ describe('required server files app router', () => {
})
it('should send the right cache headers for an app route', async () => {
const res = await fetchViaHTTP(appPort, '/api/test/123', undefined, {
headers: {
'x-matched-path': '/api/test/[slug]',
'x-now-route-matches': createNowRouteMatches({
slug: '123',
}).toString(),
},
})
const res = await fetchViaHTTP(
appPort,
'/api/test/123',
undefined,
withInvocationId({
headers: {
'x-matched-path': '/api/test/[slug]',
'x-now-route-matches': createNowRouteMatches({
slug: '123',
}).toString(),
},
})
)
expect(res.status).toBe(200)
expect(res.headers.get('cache-control')).toBe('s-maxage=31536000')
})
......@@ -120,7 +126,7 @@ describe('required server files app router', () => {
appPort,
'/optional-catchall/[lang]/[flags]/[[...slug]]',
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/optional-catchall/[lang]/[flags]/[[...slug]]',
'x-now-route-matches': createNowRouteMatches({
......@@ -129,7 +135,7 @@ describe('required server files app router', () => {
slug: 'slug',
}).toString(),
},
}
})
)
expect(res.status).toBe(200)
......@@ -142,7 +148,7 @@ describe('required server files app router', () => {
appPort,
'/optional-catchall/[lang]/[flags]/[[...slug]]',
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/optional-catchall/[lang]/[flags]/[[...slug]]',
'x-now-route-matches': createNowRouteMatches({
......@@ -150,7 +156,7 @@ describe('required server files app router', () => {
flags: 'flags',
}).toString(),
},
}
})
)
expect(res.status).toBe(200)
......@@ -162,14 +168,19 @@ describe('required server files app router', () => {
})
it('should send the right cache headers for an app page', async () => {
const res = await fetchViaHTTP(appPort, '/test/123', undefined, {
headers: {
'x-matched-path': '/test/[slug]',
'x-now-route-matches': createNowRouteMatches({
slug: '123',
}).toString(),
},
})
const res = await fetchViaHTTP(
appPort,
'/test/123',
undefined,
withInvocationId({
headers: {
'x-matched-path': '/test/[slug]',
'x-now-route-matches': createNowRouteMatches({
slug: '123',
}).toString(),
},
})
)
expect(res.status).toBe(200)
expect(res.headers.get('cache-control')).toBe(
's-maxage=3600, stale-while-revalidate=31532400'
......@@ -181,13 +192,18 @@ describe('required server files app router', () => {
})
it('should properly handle prerender for bot request', async () => {
const res = await fetchViaHTTP(appPort, '/isr/first', undefined, {
headers: {
'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)',
'x-matched-path': '/isr/first',
},
})
const res = await fetchViaHTTP(
appPort,
'/isr/first',
undefined,
withInvocationId({
headers: {
'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)',
'x-matched-path': '/isr/first',
},
})
)
expect(res.status).toBe(200)
const html = await res.text()
......@@ -195,28 +211,38 @@ describe('required server files app router', () => {
expect($('#page').text()).toBe('/isr/[slug]')
const rscRes = await fetchViaHTTP(appPort, '/isr/first.rsc', undefined, {
headers: {
'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)',
'x-matched-path': '/isr/first',
},
})
const rscRes = await fetchViaHTTP(
appPort,
'/isr/first.rsc',
undefined,
withInvocationId({
headers: {
'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)',
'x-matched-path': '/isr/first',
},
})
)
expect(rscRes.status).toBe(200)
})
it('should properly handle fallback for bot request', async () => {
const res = await fetchViaHTTP(appPort, '/isr/[slug]', undefined, {
headers: {
'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)',
'x-now-route-matches': createNowRouteMatches({
slug: 'new',
}).toString(),
'x-matched-path': '/isr/[slug]',
},
})
const res = await fetchViaHTTP(
appPort,
'/isr/[slug]',
undefined,
withInvocationId({
headers: {
'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)',
'x-now-route-matches': createNowRouteMatches({
slug: 'new',
}).toString(),
'x-matched-path': '/isr/[slug]',
},
})
)
expect(res.status).toBe(200)
const html = await res.text()
......@@ -224,16 +250,21 @@ describe('required server files app router', () => {
expect($('#page').text()).toBe('/isr/[slug]')
const rscRes = await fetchViaHTTP(appPort, '/isr/[slug].rsc', undefined, {
headers: {
'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)',
'x-now-route-matches': createNowRouteMatches({
slug: 'new',
}).toString(),
'x-matched-path': '/isr/[slug]',
},
})
const rscRes = await fetchViaHTTP(
appPort,
'/isr/[slug].rsc',
undefined,
withInvocationId({
headers: {
'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)',
'x-now-route-matches': createNowRouteMatches({
slug: 'new',
}).toString(),
'x-matched-path': '/isr/[slug]',
},
})
)
expect(rscRes.status).toBe(200)
})
......@@ -258,9 +289,14 @@ describe('required server files app router', () => {
],
]) {
require('console').error('checking', { path, tags })
const res = await fetchViaHTTP(appPort, path, undefined, {
redirect: 'manual',
})
const res = await fetchViaHTTP(
appPort,
path,
undefined,
withInvocationId({
redirect: 'manual',
})
)
expect(res.status).toBe(200)
expect(res.headers.get('x-next-cache-tags')).toBe(tags)
}
......@@ -273,9 +309,14 @@ describe('required server files app router', () => {
'/api/ssr/first',
'/api/ssr/second',
]) {
const res = await fetchViaHTTP(appPort, path, undefined, {
redirect: 'manual',
})
const res = await fetchViaHTTP(
appPort,
path,
undefined,
withInvocationId({
redirect: 'manual',
})
)
expect(res.status).toBe(200)
expect(res.headers.get('x-next-cache-tags')).toBeFalsy()
}
......@@ -292,9 +333,9 @@ describe('required server files app router', () => {
appPort,
path,
{ hello: 'world' },
{
withInvocationId({
redirect: 'manual',
}
})
)
expect(res.status).toBe(200)
expect(res.headers.get('x-next-cache-tags')).toBeFalsy()
......@@ -306,11 +347,11 @@ describe('required server files app router', () => {
appPort,
'/search/[key]',
{ key: 'searchParams', nxtPkey: 'params' },
{
withInvocationId({
headers: {
'x-matched-path': '/search/[key]',
},
}
})
)
const html = await res.text()
......@@ -318,4 +359,80 @@ describe('required server files app router', () => {
expect($('dd[data-params]').text()).toBe('params')
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)
})
})
......@@ -13,6 +13,7 @@ import {
killApp,
renderViaHTTP,
waitFor,
withInvocationId,
} from 'next-test-utils'
import nodeFetch from 'node-fetch'
import { ChildProcess } from 'child_process'
......@@ -136,22 +137,32 @@ describe('required server files i18n', () => {
})
it('should not apply locale redirect in minimal mode', async () => {
const res = await fetchViaHTTP(appPort, '/', undefined, {
redirect: 'manual',
headers: {
'accept-language': 'fr',
},
})
const res = await fetchViaHTTP(
appPort,
'/',
undefined,
withInvocationId({
redirect: 'manual',
headers: {
'accept-language': 'fr',
},
})
)
expect(res.status).toBe(200)
expect(await res.text()).toContain('index page')
const resCookie = await fetchViaHTTP(appPort, '/', undefined, {
redirect: 'manual',
headers: {
'accept-language': 'en',
cookie: 'NEXT_LOCALE=fr',
},
})
const resCookie = await fetchViaHTTP(
appPort,
'/',
undefined,
withInvocationId({
redirect: 'manual',
headers: {
'accept-language': 'en',
cookie: 'NEXT_LOCALE=fr',
},
})
)
expect(resCookie.status).toBe(200)
expect(await resCookie.text()).toContain('index page')
})
......@@ -170,9 +181,14 @@ describe('required server files i18n', () => {
it('should set correct SWR headers with notFound gsp', async () => {
await next.patchFile('standalone/data.txt', 'show')
const res = await fetchViaHTTP(appPort, '/gsp', undefined, {
redirect: 'manual',
})
const res = await fetchViaHTTP(
appPort,
'/gsp',
undefined,
withInvocationId({
redirect: 'manual',
})
)
expect(res.status).toBe(200)
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate=31535999'
......@@ -181,9 +197,14 @@ describe('required server files i18n', () => {
await waitFor(2000)
await next.patchFile('standalone/data.txt', 'hide')
const res2 = await fetchViaHTTP(appPort, '/gsp', undefined, {
redirect: 'manual',
})
const res2 = await fetchViaHTTP(
appPort,
'/gsp',
undefined,
withInvocationId({
redirect: 'manual',
})
)
expect(res2.status).toBe(404)
expect(res2.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate=31535999'
......@@ -193,9 +214,14 @@ describe('required server files i18n', () => {
it('should set correct SWR headers with notFound gssp', async () => {
await next.patchFile('standalone/data.txt', 'show')
const res = await fetchViaHTTP(appPort, '/gssp', undefined, {
redirect: 'manual',
})
const res = await fetchViaHTTP(
appPort,
'/gssp',
undefined,
withInvocationId({
redirect: 'manual',
})
)
expect(res.status).toBe(200)
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate=31535999'
......@@ -203,9 +229,14 @@ describe('required server files i18n', () => {
await next.patchFile('standalone/data.txt', 'hide')
const res2 = await fetchViaHTTP(appPort, '/gssp', undefined, {
redirect: 'manual',
})
const res2 = await fetchViaHTTP(
appPort,
'/gssp',
undefined,
withInvocationId({
redirect: 'manual',
})
)
await next.patchFile('standalone/data.txt', 'show')
expect(res2.status).toBe(404)
......@@ -215,14 +246,24 @@ describe('required server files i18n', () => {
})
it('should render SSR page correctly', async () => {
const html = await renderViaHTTP(appPort, '/gssp')
const html = await renderViaHTTP(
appPort,
'/gssp',
undefined,
withInvocationId()
)
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
expect($('#gssp').text()).toBe('getServerSideProps page')
expect(data.hello).toBe('world')
const html2 = await renderViaHTTP(appPort, '/gssp')
const html2 = await renderViaHTTP(
appPort,
'/gssp',
undefined,
withInvocationId()
)
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
......@@ -232,7 +273,12 @@ describe('required server files i18n', () => {
})
it('should render dynamic SSR page correctly', async () => {
const html = await renderViaHTTP(appPort, '/dynamic/first')
const html = await renderViaHTTP(
appPort,
'/dynamic/first',
undefined,
withInvocationId()
)
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
......@@ -240,7 +286,12 @@ describe('required server files i18n', () => {
expect($('#slug').text()).toBe('first')
expect(data.hello).toBe('world')
const html2 = await renderViaHTTP(appPort, '/dynamic/second')
const html2 = await renderViaHTTP(
appPort,
'/dynamic/second',
undefined,
withInvocationId()
)
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
......@@ -251,7 +302,12 @@ describe('required server files i18n', () => {
})
it('should render fallback page correctly', async () => {
const html = await renderViaHTTP(appPort, '/fallback/first')
const html = await renderViaHTTP(
appPort,
'/fallback/first',
undefined,
withInvocationId()
)
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
......@@ -260,7 +316,13 @@ describe('required server files i18n', () => {
expect(data.hello).toBe('world')
await waitFor(2000)
const html2 = await renderViaHTTP(appPort, '/fallback/first')
const html2 = await renderViaHTTP(
appPort,
'/fallback/first',
undefined,
withInvocationId()
)
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
......@@ -269,7 +331,12 @@ describe('required server files i18n', () => {
expect(isNaN(data2.random)).toBe(false)
expect(data2.random).not.toBe(data.random)
const html3 = await renderViaHTTP(appPort, '/fallback/second')
const html3 = await renderViaHTTP(
appPort,
'/fallback/second',
undefined,
withInvocationId()
)
const $3 = cheerio.load(html3)
const data3 = JSON.parse($3('#props').text())
......@@ -280,7 +347,9 @@ describe('required server files i18n', () => {
const { pageProps: data4 } = JSON.parse(
await renderViaHTTP(
appPort,
`/_next/data/${next.buildId}/en/fallback/third.json`
`/_next/data/${next.buildId}/en/fallback/third.json`,
undefined,
withInvocationId()
)
)
expect(data4.hello).toBe('world')
......@@ -288,22 +357,32 @@ describe('required server files i18n', () => {
})
it('should render SSR page correctly with x-matched-path', async () => {
const html = await renderViaHTTP(appPort, '/some-other-path', undefined, {
headers: {
'x-matched-path': '/gssp',
},
})
const html = await renderViaHTTP(
appPort,
'/some-other-path',
undefined,
withInvocationId({
headers: {
'x-matched-path': '/gssp',
},
})
)
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
expect($('#gssp').text()).toBe('getServerSideProps page')
expect(data.hello).toBe('world')
const html2 = await renderViaHTTP(appPort, '/some-other-path', undefined, {
headers: {
'x-matched-path': '/gssp',
},
})
const html2 = await renderViaHTTP(
appPort,
'/some-other-path',
undefined,
withInvocationId({
headers: {
'x-matched-path': '/gssp',
},
})
)
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
......@@ -317,11 +396,11 @@ describe('required server files i18n', () => {
appPort,
'/some-other-path?nxtPslug=first',
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/dynamic/[slug]',
},
}
})
)
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
......@@ -334,11 +413,11 @@ describe('required server files i18n', () => {
appPort,
'/some-other-path?nxtPslug=second',
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/dynamic/[slug]',
},
}
})
)
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
......@@ -352,11 +431,11 @@ describe('required server files i18n', () => {
appPort,
'/some-other-path?nxtPslug=second',
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/dynamic/[slug]?slug=%5Bslug%5D.json',
},
}
})
)
const $3 = cheerio.load(html3)
const data3 = JSON.parse($3('#props').text())
......@@ -368,14 +447,19 @@ describe('required server files i18n', () => {
})
it('should render fallback page correctly with x-matched-path and routes-matches', async () => {
const html = await renderViaHTTP(appPort, '/fallback/first', undefined, {
headers: {
'x-matched-path': '/fallback/first',
'x-now-route-matches': createNowRouteMatches({
slug: 'first',
}).toString(),
},
})
const html = await renderViaHTTP(
appPort,
'/fallback/first',
undefined,
withInvocationId({
headers: {
'x-matched-path': '/fallback/first',
'x-now-route-matches': createNowRouteMatches({
slug: 'first',
}).toString(),
},
})
)
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
......@@ -383,14 +467,19 @@ describe('required server files i18n', () => {
expect($('#slug').text()).toBe('first')
expect(data.hello).toBe('world')
const html2 = await renderViaHTTP(appPort, `/fallback/[slug]`, undefined, {
headers: {
'x-matched-path': '/fallback/[slug]',
'x-now-route-matches': createNowRouteMatches({
slug: 'second',
}).toString(),
},
})
const html2 = await renderViaHTTP(
appPort,
`/fallback/[slug]`,
undefined,
withInvocationId({
headers: {
'x-matched-path': '/fallback/[slug]',
'x-now-route-matches': createNowRouteMatches({
slug: 'second',
}).toString(),
},
})
)
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
......@@ -407,11 +496,11 @@ describe('required server files i18n', () => {
{ slug: 'first' }
).toString()}`,
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/dynamic/[slug]',
},
}
})
)
const { pageProps: data } = await res.json()
......@@ -423,14 +512,14 @@ describe('required server files i18n', () => {
appPort,
`/_next/data/${next.buildId}/en/fallback/[slug].json`,
undefined,
{
withInvocationId({
headers: {
'x-matched-path': `/_next/data/${next.buildId}/en/fallback/[slug].json`,
'x-now-route-matches': createNowRouteMatches({
slug: 'second',
}).toString(),
},
}
})
)
const { pageProps: data2 } = await res2.json()
......@@ -444,12 +533,12 @@ describe('required server files i18n', () => {
appPort,
'/catch-all/[[...rest]]',
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/catch-all/[[...rest]]',
'x-now-route-matches': '',
},
}
})
)
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
......@@ -462,14 +551,14 @@ describe('required server files i18n', () => {
appPort,
'/catch-all/[[...rest]]',
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/catch-all/[[...rest]]',
'x-now-route-matches': createNowRouteMatches({
rest: 'hello',
}).toString(),
},
}
})
)
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
......@@ -483,14 +572,14 @@ describe('required server files i18n', () => {
appPort,
'/catch-all/[[...rest]]',
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/catch-all/[[...rest]]',
'x-now-route-matches': createNowRouteMatches({
rest: 'hello/world',
}).toString(),
},
}
})
)
const $3 = cheerio.load(html3)
const data3 = JSON.parse($3('#props').text())
......@@ -506,11 +595,11 @@ describe('required server files i18n', () => {
appPort,
`/_next/data/${next.buildId}/en/catch-all.json`,
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/en/catch-all/[[...rest]]',
},
}
})
)
const { pageProps: data } = await res.json()
......@@ -522,14 +611,14 @@ describe('required server files i18n', () => {
appPort,
`/_next/data/${next.buildId}/en/catch-all/[[...rest]].json`,
undefined,
{
withInvocationId({
headers: {
'x-matched-path': `/_next/data/${next.buildId}/en/catch-all/[[...rest]].json`,
'x-now-route-matches': createNowRouteMatches({
rest: 'hello',
}).toString(),
},
}
})
)
const { pageProps: data2 } = await res2.json()
......@@ -541,14 +630,14 @@ describe('required server files i18n', () => {
appPort,
`/_next/data/${next.buildId}/en/catch-all/[[...rest]].json`,
undefined,
{
withInvocationId({
headers: {
'x-matched-path': `/_next/data/${next.buildId}/en/catch-all/[[...rest]].json`,
'x-now-route-matches': createNowRouteMatches({
rest: 'hello/world',
}).toString(),
},
}
})
)
const { pageProps: data3 } = await res3.json()
......@@ -567,9 +656,14 @@ describe('required server files i18n', () => {
'/fallback/another/',
'/fallback/another',
]) {
const res = await fetchViaHTTP(appPort, path, undefined, {
redirect: 'manual',
})
const res = await fetchViaHTTP(
appPort,
path,
undefined,
withInvocationId({
redirect: 'manual',
})
)
expect(res.status).toBe(200)
}
......@@ -582,11 +676,11 @@ describe('required server files i18n', () => {
{
path: 'hello/world',
},
{
withInvocationId({
headers: {
'x-matched-path': '/gssp',
},
}
})
)
const $ = cheerio.load(html)
expect(JSON.parse($('#router').text()).query.path).toEqual([
......@@ -604,18 +698,23 @@ describe('required server files i18n', () => {
path: '%c0.%c0.',
})
),
{
withInvocationId({
headers: {
'x-matched-path': '/dynamic/[slug]',
},
}
})
)
expect(res.status).toBe(400)
expect(await res.text()).toContain('Bad Request')
})
it('should bubble error correctly for gip page', async () => {
const res = await fetchViaHTTP(appPort, '/errors/gip', { crash: '1' })
const res = await fetchViaHTTP(
appPort,
'/errors/gip',
{ crash: '1' },
withInvocationId()
)
expect(res.status).toBe(500)
expect(await res.text()).toBe('Internal Server Error')
......@@ -629,7 +728,12 @@ describe('required server files i18n', () => {
})
it('should bubble error correctly for gssp page', async () => {
const res = await fetchViaHTTP(appPort, '/errors/gssp', { crash: '1' })
const res = await fetchViaHTTP(
appPort,
'/errors/gssp',
{ crash: '1' },
withInvocationId()
)
expect(res.status).toBe(500)
expect(await res.text()).toBe('Internal Server Error')
await check(
......@@ -642,7 +746,12 @@ describe('required server files i18n', () => {
})
it('should bubble error correctly for gsp page', async () => {
const res = await fetchViaHTTP(appPort, '/errors/gsp/crash')
const res = await fetchViaHTTP(
appPort,
'/errors/gsp/crash',
undefined,
withInvocationId()
)
expect(res.status).toBe(500)
expect(await res.text()).toBe('Internal Server Error')
await check(
......@@ -656,7 +765,12 @@ describe('required server files i18n', () => {
it('should bubble error correctly for API page', async () => {
errors = []
const res = await fetchViaHTTP(appPort, '/api/error')
const res = await fetchViaHTTP(
appPort,
'/api/error',
undefined,
withInvocationId()
)
expect(res.status).toBe(500)
expect(await res.text()).toBe('Internal Server Error')
await check(
......@@ -682,11 +796,11 @@ describe('required server files i18n', () => {
}
)
),
{
withInvocationId({
headers: {
'x-matched-path': '/optional-ssp/[[...rest]]',
},
}
})
)
const html = await res.text()
......@@ -701,7 +815,7 @@ describe('required server files i18n', () => {
appPort,
'/en/optional-ssg/[[...rest]]',
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/en/optional-ssg/[[...rest]]',
'x-now-route-matches': createNowRouteMatches(
......@@ -711,7 +825,7 @@ describe('required server files i18n', () => {
}
).toString(),
},
}
})
)
const html = await res.text()
......@@ -725,7 +839,7 @@ describe('required server files i18n', () => {
appPort,
'/en/[slug]/social/[[...rest]]',
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/en/[slug]/social/[[...rest]]',
'x-now-route-matches': createNowRouteMatches(
......@@ -733,7 +847,7 @@ describe('required server files i18n', () => {
{ nextLocale: 'en' }
).toString(),
},
}
})
)
const html = await res.text()
......@@ -750,12 +864,12 @@ describe('required server files i18n', () => {
appPort,
'/optional-ssg/[[...rest]]',
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/optional-ssg/[[...rest]]',
'x-now-route-matches': 'nxtPrest=en%2Fes%2Fhello%252Fworld',
},
}
})
)
const html = await res.text()
......@@ -780,11 +894,11 @@ describe('required server files i18n', () => {
}
)
),
{
withInvocationId({
headers: {
'x-matched-path': '/api/optional/[[...rest]]',
},
}
})
)
const json = await res.json()
......@@ -793,12 +907,17 @@ describe('required server files i18n', () => {
})
it('should match the index page correctly', async () => {
const res = await fetchViaHTTP(appPort, '/', undefined, {
headers: {
'x-matched-path': '/index',
},
redirect: 'manual',
})
const res = await fetchViaHTTP(
appPort,
'/',
undefined,
withInvocationId({
headers: {
'x-matched-path': '/index',
},
redirect: 'manual',
})
)
const html = await res.text()
const $ = cheerio.load(html)
......@@ -806,12 +925,17 @@ describe('required server files i18n', () => {
})
it('should match the root dyanmic page correctly', async () => {
const res = await fetchViaHTTP(appPort, '/slug-1', undefined, {
headers: {
'x-matched-path': '/[slug]',
},
redirect: 'manual',
})
const res = await fetchViaHTTP(
appPort,
'/slug-1',
undefined,
withInvocationId({
headers: {
'x-matched-path': '/[slug]',
},
redirect: 'manual',
})
)
const html = await res.text()
const $ = cheerio.load(html)
......@@ -819,20 +943,25 @@ describe('required server files i18n', () => {
})
it('should have the correct asPath for fallback page', async () => {
const res = await fetchViaHTTP(appPort, '/en/fallback/[slug]', undefined, {
headers: {
'x-matched-path': '/en/fallback/[slug]',
'x-now-route-matches': createNowRouteMatches(
{
slug: 'another',
},
{
nextLocale: 'en',
}
).toString(),
},
redirect: 'manual',
})
const res = await fetchViaHTTP(
appPort,
'/en/fallback/[slug]',
undefined,
withInvocationId({
headers: {
'x-matched-path': '/en/fallback/[slug]',
'x-now-route-matches': createNowRouteMatches(
{
slug: 'another',
},
{
nextLocale: 'en',
}
).toString(),
},
redirect: 'manual',
})
)
const html = await res.text()
const $ = cheerio.load(html)
......@@ -844,20 +973,25 @@ describe('required server files i18n', () => {
})
it('should have the correct asPath for fallback page locale', async () => {
const res = await fetchViaHTTP(appPort, '/fr/fallback/[slug]', undefined, {
headers: {
'x-matched-path': '/fr/fallback/[slug]',
'x-now-route-matches': createNowRouteMatches(
{
slug: 'another',
},
{
nextLocale: 'fr',
}
).toString(),
},
redirect: 'manual',
})
const res = await fetchViaHTTP(
appPort,
'/fr/fallback/[slug]',
undefined,
withInvocationId({
headers: {
'x-matched-path': '/fr/fallback/[slug]',
'x-now-route-matches': createNowRouteMatches(
{
slug: 'another',
},
{
nextLocale: 'fr',
}
).toString(),
},
redirect: 'manual',
})
)
const html = await res.text()
const $ = cheerio.load(html)
......
......@@ -10,6 +10,7 @@ import {
initNextServerScript,
killApp,
retry,
withInvocationId,
} from 'next-test-utils'
import { ChildProcess } from 'node:child_process'
......@@ -144,14 +145,19 @@ describe.skip('required server files app router', () => {
require('console').error('requesting', outputSegmentPath)
const res = await fetchViaHTTP(appPort, outputSegmentPath, undefined, {
headers: {
'x-matched-path': '/isr/[slug].segments/_tree.segment.rsc',
'x-now-route-matches': createNowRouteMatches({
slug: 'first',
}).toString(),
},
})
const res = await fetchViaHTTP(
appPort,
outputSegmentPath,
undefined,
withInvocationId({
headers: {
'x-matched-path': '/isr/[slug].segments/_tree.segment.rsc',
'x-now-route-matches': createNowRouteMatches({
slug: 'first',
}).toString(),
},
})
)
expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toBe('text/x-component')
......@@ -167,14 +173,19 @@ describe.skip('required server files app router', () => {
})
it('should properly stream resume with Next-Resume', async () => {
const res = await fetchViaHTTP(appPort, '/delayed', undefined, {
headers: {
'x-matched-path': '/delayed',
'next-resume': '1',
},
method: 'POST',
body: delayedPostpone,
})
const res = await fetchViaHTTP(
appPort,
'/delayed',
undefined,
withInvocationId({
headers: {
'x-matched-path': '/delayed',
'next-resume': '1',
},
method: 'POST',
body: delayedPostpone,
})
)
expect(res.status).toBe(200)
......@@ -199,13 +210,18 @@ describe.skip('required server files app router', () => {
})
it('should properly handle prerender for bot request', async () => {
const res = await fetchViaHTTP(appPort, '/isr/first', undefined, {
headers: {
'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)',
'x-matched-path': '/isr/first',
},
})
const res = await fetchViaHTTP(
appPort,
'/isr/first',
undefined,
withInvocationId({
headers: {
'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)',
'x-matched-path': '/isr/first',
},
})
)
expect(res.status).toBe(200)
const html = await res.text()
......@@ -213,28 +229,38 @@ describe.skip('required server files app router', () => {
expect($('#page').text()).toBe('/isr/[slug]')
const rscRes = await fetchViaHTTP(appPort, '/isr/first.rsc', undefined, {
headers: {
'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)',
'x-matched-path': '/isr/first',
},
})
const rscRes = await fetchViaHTTP(
appPort,
'/isr/first.rsc',
undefined,
withInvocationId({
headers: {
'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)',
'x-matched-path': '/isr/first',
},
})
)
expect(rscRes.status).toBe(200)
})
it('should properly handle fallback for bot request', async () => {
const res = await fetchViaHTTP(appPort, '/isr/[slug]', undefined, {
headers: {
'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)',
'x-now-route-matches': createNowRouteMatches({
slug: 'new',
}).toString(),
'x-matched-path': '/isr/[slug]',
},
})
const res = await fetchViaHTTP(
appPort,
'/isr/[slug]',
undefined,
withInvocationId({
headers: {
'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)',
'x-now-route-matches': createNowRouteMatches({
slug: 'new',
}).toString(),
'x-matched-path': '/isr/[slug]',
},
})
)
expect(res.status).toBe(200)
const html = await res.text()
......@@ -242,16 +268,21 @@ describe.skip('required server files app router', () => {
expect($('#page').text()).toBe('/isr/[slug]')
const rscRes = await fetchViaHTTP(appPort, '/isr/[slug].rsc', undefined, {
headers: {
'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)',
'x-now-route-matches': createNowRouteMatches({
slug: 'new',
}).toString(),
'x-matched-path': '/isr/[slug]',
},
})
const rscRes = await fetchViaHTTP(
appPort,
'/isr/[slug].rsc',
undefined,
withInvocationId({
headers: {
'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)',
'x-now-route-matches': createNowRouteMatches({
slug: 'new',
}).toString(),
'x-matched-path': '/isr/[slug]',
},
})
)
expect(rscRes.status).toBe(200)
})
......@@ -267,7 +298,7 @@ describe.skip('required server files app router', () => {
// route backed by PPR.
`/_next/data/${next.buildId}/index.json`,
undefined,
{
withInvocationId({
method: 'POST',
headers: {
'x-matched-path': '/[...catchAll]',
......@@ -277,7 +308,7 @@ describe.skip('required server files app router', () => {
'next-resume': '1',
},
body: postponed,
}
})
)
// 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', () => {
appPort,
'/rewrite-with-cookie',
undefined,
{
withInvocationId({
method: 'POST',
headers: {
'x-matched-path': '/rewrite/first-cookie',
'next-resume': '1',
},
body: rewritePostpone,
}
})
)
expect(res.status).toBe(200)
......@@ -327,15 +358,20 @@ describe.skip('required server files app router', () => {
it('should still render when postponed is corrupted with Next-Resume', async () => {
const random = Math.random().toString(36).substring(2)
const res = await fetchViaHTTP(appPort, '/dyn/' + random, undefined, {
method: 'POST',
headers: {
'x-matched-path': '/dyn/[slug]',
'next-resume': '1',
},
// This is a corrupted postponed JSON payload.
body: '{',
})
const res = await fetchViaHTTP(
appPort,
'/dyn/' + random,
undefined,
withInvocationId({
method: 'POST',
headers: {
'x-matched-path': '/dyn/[slug]',
'next-resume': '1',
},
// This is a corrupted postponed JSON payload.
body: '{',
})
)
expect(res.status).toBe(200)
......@@ -369,9 +405,14 @@ describe.skip('required server files app router', () => {
],
]) {
require('console').error('checking', { path, tags })
const res = await fetchViaHTTP(appPort, path, undefined, {
redirect: 'manual',
})
const res = await fetchViaHTTP(
appPort,
path,
undefined,
withInvocationId({
redirect: 'manual',
})
)
expect(res.status).toBe(200)
expect(res.headers.get('x-next-cache-tags')).toBe(tags)
......@@ -390,9 +431,14 @@ describe.skip('required server files app router', () => {
'/api/ssr/first',
'/api/ssr/second',
]) {
const res = await fetchViaHTTP(appPort, path, undefined, {
redirect: 'manual',
})
const res = await fetchViaHTTP(
appPort,
path,
undefined,
withInvocationId({
redirect: 'manual',
})
)
expect(res.status).toBe(200)
expect(res.headers.get('x-next-cache-tags')).toBeFalsy()
......@@ -416,9 +462,9 @@ describe.skip('required server files app router', () => {
appPort,
path,
{ hello: 'world' },
{
withInvocationId({
redirect: 'manual',
}
})
)
expect(res.status).toBe(200)
......@@ -433,14 +479,19 @@ describe.skip('required server files app router', () => {
})
it('should handle RSC requests', async () => {
const res = await fetchViaHTTP(appPort, '/dyn/first.rsc', undefined, {
headers: {
'x-matched-path': '/dyn/[slug]',
'x-now-route-matches': createNowRouteMatches({
slug: 'first',
}).toString(),
},
})
const res = await fetchViaHTTP(
appPort,
'/dyn/first.rsc',
undefined,
withInvocationId({
headers: {
'x-matched-path': '/dyn/[slug]',
'x-now-route-matches': createNowRouteMatches({
slug: 'first',
}).toString(),
},
})
)
expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toEqual('text/x-component')
......@@ -466,14 +517,14 @@ describe.skip('required server files app router', () => {
appPort,
'/rewrite/second-cookie.rsc',
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/rewrite/[slug]',
'x-now-route-matches': createNowRouteMatches({
slug: 'second-cookie',
}).toString(),
},
}
})
)
expect(res.status).toBe(200)
......@@ -508,17 +559,22 @@ describe.skip('required server files app router', () => {
// Then let's get the Dynamic RSC request and verify that the random value
// is present in the response by passing the postponed state.
res = await fetchViaHTTP(appPort, '/rewrite/second-cookie.rsc', undefined, {
method: 'POST',
headers: {
'x-matched-path': '/rewrite/[slug]',
'x-now-route-matches': createNowRouteMatches({
slug: 'second-cookie',
}).toString(),
'next-resume': '1',
},
body: secondCookiePostpone,
})
res = await fetchViaHTTP(
appPort,
'/rewrite/second-cookie.rsc',
undefined,
withInvocationId({
method: 'POST',
headers: {
'x-matched-path': '/rewrite/[slug]',
'x-now-route-matches': createNowRouteMatches({
slug: 'second-cookie',
}).toString(),
'next-resume': '1',
},
body: secondCookiePostpone,
})
)
expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toEqual('text/x-component')
......@@ -551,14 +607,19 @@ describe.skip('required server files app router', () => {
})
it('should handle revalidating the fallback page', async () => {
const res = await fetchViaHTTP(appPort, '/postpone/isr/[slug]', undefined, {
headers: {
'x-matched-path': '/postpone/isr/[slug]',
// We don't include the `x-now-route-matches` header because we want to
// test that the fallback route params are correctly set.
'x-now-route-matches': '',
},
})
const res = await fetchViaHTTP(
appPort,
'/postpone/isr/[slug]',
undefined,
withInvocationId({
headers: {
'x-matched-path': '/postpone/isr/[slug]',
// We don't include the `x-now-route-matches` header because we want to
// test that the fallback route params are correctly set.
'x-now-route-matches': '',
},
})
)
expect(res.status).toBe(200)
......
......@@ -15,6 +15,7 @@ import {
renderViaHTTP,
retry,
waitFor,
withInvocationId,
} from 'next-test-utils'
import { ChildProcess } from 'child_process'
......@@ -177,12 +178,12 @@ describe('required server files', () => {
appPort,
'/route-resolving/import/first',
undefined,
{
withInvocationId({
redirect: 'manual',
headers: {
'x-matched-path': '/route-resolving/import/[slug]',
},
}
})
)
expect(res.status).toBe(307)
expect(new URL(res.headers.get('location'), 'http://n').pathname).toBe(
......@@ -201,11 +202,16 @@ describe('required server files', () => {
await next.renameFile(toRename, `${toRename}.bak`)
try {
const res = await fetchViaHTTP(appPort, '/auto-static', undefined, {
headers: {
'x-matched-path': '/auto-static',
},
})
const res = await fetchViaHTTP(
appPort,
'/auto-static',
undefined,
withInvocationId({
headers: {
'x-matched-path': '/auto-static',
},
})
)
expect(res.status).toBe(500)
await check(() => stderr, /Invariant: failed to load static page/)
......@@ -230,9 +236,14 @@ describe('required server files', () => {
])(
`should have correct cache-control for $case`,
async ({ path, dest, cacheControl }) => {
const res = await fetchViaHTTP(appPort, path, undefined, {
redirect: 'manual',
})
const res = await fetchViaHTTP(
appPort,
path,
undefined,
withInvocationId({
redirect: 'manual',
})
)
expect(res.status).toBe(307)
expect(new URL(res.headers.get('location'), 'http://n').pathname).toBe(
dest
......@@ -243,9 +254,9 @@ describe('required server files', () => {
appPort,
`/_next/data/${next.buildId}${path}.json`,
undefined,
{
withInvocationId({
redirect: 'manual',
}
})
)
expect((await dataRes.json()).pageProps).toEqual({
__N_REDIRECT: dest,
......@@ -260,11 +271,11 @@ describe('required server files', () => {
appPort,
`/_next/data/${next.buildId}/catch-all.json`,
{},
{
withInvocationId({
headers: {
'x-matched-path': `/_next/data/${next.buildId}/catch-all.json`,
},
}
})
)
expect(res.status).toBe(200)
......@@ -277,11 +288,11 @@ describe('required server files', () => {
appPort,
`/_next/data/${next.buildId}/catch-all/next.js.json`,
{},
{
withInvocationId({
headers: {
'x-matched-path': `/_next/data/${next.buildId}/catch-all/next.js.json`,
},
}
})
)
expect(res.status).toBe(200)
......@@ -307,9 +318,14 @@ describe('required server files', () => {
])(
`should have correct cache-control for $case`,
async ({ path, dest, cacheControl }) => {
const res = await fetchViaHTTP(appPort, path, undefined, {
redirect: 'manual',
})
const res = await fetchViaHTTP(
appPort,
path,
undefined,
withInvocationId({
redirect: 'manual',
})
)
expect(res.status).toBe(404)
expect(res.headers.get('cache-control')).toBe(cacheControl)
......@@ -317,16 +333,21 @@ describe('required server files', () => {
appPort,
`/_next/data/${next.buildId}${path}.json`,
undefined,
{
withInvocationId({
redirect: 'manual',
}
})
)
expect(dataRes.headers.get('cache-control')).toBe(cacheControl)
}
)
it('should have the correct cache-control for props with no revalidate', async () => {
const res = await fetchViaHTTP(appPort, '/optional-ssg/props-no-revalidate')
const res = await fetchViaHTTP(
appPort,
'/optional-ssg/props-no-revalidate',
undefined,
withInvocationId()
)
expect(res.status).toBe(200)
expect(res.headers.get('cache-control')).toBe('s-maxage=31536000')
const $ = cheerio.load(await res.text())
......@@ -337,7 +358,8 @@ describe('required server files', () => {
const dataRes = await fetchViaHTTP(
appPort,
`/_next/data/${next.buildId}/optional-ssg/props-no-revalidate.json`,
undefined
undefined,
withInvocationId()
)
expect(dataRes.status).toBe(200)
expect(res.headers.get('cache-control')).toBe('s-maxage=31536000')
......@@ -350,7 +372,7 @@ describe('required server files', () => {
;(process.env.IS_TURBOPACK_TEST ? it.skip : it)(
'should warn when "next" is imported directly',
async () => {
await renderViaHTTP(appPort, '/gssp')
await renderViaHTTP(appPort, '/gssp', undefined, withInvocationId())
await check(
() => stderr,
/"next" should not be imported directly, imported in/
......@@ -430,9 +452,14 @@ describe('required server files', () => {
})
it('should de-dupe HTML/data requests', async () => {
// Create a shared invocation ID for /gsp - both HTML and JSON requests share same x-invocation-id
const gspOpts = withInvocationId()
const res = await fetchViaHTTP(appPort, '/gsp', undefined, {
...gspOpts,
redirect: 'manual',
headers: {
...gspOpts.headers,
// ensure the nextjs-data header being present
// doesn't incorrectly return JSON for HTML path
// during prerendering
......@@ -450,6 +477,7 @@ describe('required server files', () => {
`/_next/data/${next.buildId}/gsp.json`,
undefined,
{
...gspOpts,
redirect: 'manual',
}
)
......@@ -458,9 +486,14 @@ describe('required server files', () => {
const { pageProps: props2 } = await res2.json()
expect(props2.gspCalls).toBe(props.gspCalls)
// Create a separate shared invocation ID for /index - different x-invocation-id
const indexOpts = withInvocationId()
const res3 = await fetchViaHTTP(appPort, '/index', undefined, {
...indexOpts,
redirect: 'manual',
headers: {
...indexOpts.headers,
'x-matched-path': '/index',
},
})
......@@ -474,6 +507,7 @@ describe('required server files', () => {
`/_next/data/${next.buildId}/index.json`,
undefined,
{
...indexOpts,
redirect: 'manual',
}
)
......@@ -482,30 +516,6 @@ describe('required server files', () => {
expect(props4.gspCalls).toBe(props3.gspCalls)
})
it('should cap de-dupe previousCacheItem expires time', async () => {
const res = await fetchViaHTTP(appPort, '/gsp-long-revalidate', undefined, {
redirect: 'manual',
})
expect(res.status).toBe(200)
const $ = cheerio.load(await res.text())
const props = JSON.parse($('#props').text())
expect(props.gspCalls).toBeDefined()
await waitFor(1000)
const res2 = await fetchViaHTTP(
appPort,
`/_next/data/${next.buildId}/gsp-long-revalidate.json`,
undefined,
{
redirect: 'manual',
}
)
expect(res2.status).toBe(200)
const { pageProps: props2 } = await res2.json()
expect(props2.gspCalls).not.toBe(props.gspCalls)
})
it('should not 404 for onlyGenerated on-demand revalidate in minimal mode', async () => {
const previewProps = JSON.parse(
await next.readFile('standalone/.next/prerender-manifest.json')
......@@ -515,12 +525,12 @@ describe('required server files', () => {
appPort,
'/optional-ssg/only-generated-1',
undefined,
{
withInvocationId({
headers: {
'x-prerender-revalidate': previewProps.previewModeId,
'x-prerender-revalidate-if-generated': '1',
},
}
})
)
expect(res.status).toBe(200)
})
......@@ -529,9 +539,14 @@ describe('required server files', () => {
await waitFor(2000)
await next.patchFile('standalone/data.txt', 'show')
const res = await fetchViaHTTP(appPort, '/gsp', undefined, {
redirect: 'manual',
})
const res = await fetchViaHTTP(
appPort,
'/gsp',
undefined,
withInvocationId({
redirect: 'manual',
})
)
expect(res.status).toBe(200)
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate=31535999'
......@@ -540,9 +555,14 @@ describe('required server files', () => {
await waitFor(2000)
await next.patchFile('standalone/data.txt', 'hide')
const res2 = await fetchViaHTTP(appPort, '/gsp', undefined, {
redirect: 'manual',
})
const res2 = await fetchViaHTTP(
appPort,
'/gsp',
undefined,
withInvocationId({
redirect: 'manual',
})
)
expect(res2.status).toBe(404)
expect(res2.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate=31535999'
......@@ -552,9 +572,14 @@ describe('required server files', () => {
it('should set correct SWR headers with notFound gssp', async () => {
await next.patchFile('standalone/data.txt', 'show')
const res = await fetchViaHTTP(appPort, '/gssp', undefined, {
redirect: 'manual',
})
const res = await fetchViaHTTP(
appPort,
'/gssp',
undefined,
withInvocationId({
redirect: 'manual',
})
)
expect(res.status).toBe(200)
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate=31535999'
......@@ -562,9 +587,14 @@ describe('required server files', () => {
await next.patchFile('standalone/data.txt', 'hide')
const res2 = await fetchViaHTTP(appPort, '/gssp', undefined, {
redirect: 'manual',
})
const res2 = await fetchViaHTTP(
appPort,
'/gssp',
undefined,
withInvocationId({
redirect: 'manual',
})
)
await next.patchFile('standalone/data.txt', 'show')
expect(res2.status).toBe(404)
......@@ -574,14 +604,24 @@ describe('required server files', () => {
})
it('should render SSR page correctly', async () => {
const html = await renderViaHTTP(appPort, '/gssp')
const html = await renderViaHTTP(
appPort,
'/gssp',
undefined,
withInvocationId()
)
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
expect($('#gssp').text()).toBe('getServerSideProps page')
expect(data.hello).toBe('world')
const html2 = await renderViaHTTP(appPort, '/gssp')
const html2 = await renderViaHTTP(
appPort,
'/gssp',
undefined,
withInvocationId()
)
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
......@@ -591,7 +631,12 @@ describe('required server files', () => {
})
it('should render dynamic SSR page correctly', async () => {
const html = await renderViaHTTP(appPort, '/dynamic/first')
const html = await renderViaHTTP(
appPort,
'/dynamic/first',
undefined,
withInvocationId()
)
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
......@@ -599,7 +644,12 @@ describe('required server files', () => {
expect($('#slug').text()).toBe('first')
expect(data.hello).toBe('world')
const html2 = await renderViaHTTP(appPort, '/dynamic/second')
const html2 = await renderViaHTTP(
appPort,
'/dynamic/second',
undefined,
withInvocationId()
)
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
......@@ -610,7 +660,12 @@ describe('required server files', () => {
})
it('should render fallback page correctly', async () => {
const html = await renderViaHTTP(appPort, '/fallback/first')
const html = await renderViaHTTP(
appPort,
'/fallback/first',
undefined,
withInvocationId()
)
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
......@@ -619,7 +674,13 @@ describe('required server files', () => {
expect(data.hello).toBe('world')
await waitFor(2000)
const html2 = await renderViaHTTP(appPort, '/fallback/first')
const html2 = await renderViaHTTP(
appPort,
'/fallback/first',
undefined,
withInvocationId()
)
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
......@@ -628,7 +689,12 @@ describe('required server files', () => {
expect(isNaN(data2.random)).toBe(false)
expect(data2.random).not.toBe(data.random)
const html3 = await renderViaHTTP(appPort, '/fallback/second')
const html3 = await renderViaHTTP(
appPort,
'/fallback/second',
undefined,
withInvocationId()
)
const $3 = cheerio.load(html3)
const data3 = JSON.parse($3('#props').text())
......@@ -639,7 +705,9 @@ describe('required server files', () => {
const { pageProps: data4 } = JSON.parse(
await renderViaHTTP(
appPort,
`/_next/data/${next.buildId}/fallback/third.json`
`/_next/data/${next.buildId}/fallback/third.json`,
undefined,
withInvocationId()
)
)
expect(data4.hello).toBe('world')
......@@ -647,22 +715,32 @@ describe('required server files', () => {
})
it('should render SSR page correctly with x-matched-path', async () => {
const html = await renderViaHTTP(appPort, '/some-other-path', undefined, {
headers: {
'x-matched-path': '/gssp',
},
})
const html = await renderViaHTTP(
appPort,
'/some-other-path',
undefined,
withInvocationId({
headers: {
'x-matched-path': '/gssp',
},
})
)
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
expect($('#gssp').text()).toBe('getServerSideProps page')
expect(data.hello).toBe('world')
const html2 = await renderViaHTTP(appPort, '/some-other-path', undefined, {
headers: {
'x-matched-path': '/gssp',
},
})
const html2 = await renderViaHTTP(
appPort,
'/some-other-path',
undefined,
withInvocationId({
headers: {
'x-matched-path': '/gssp',
},
})
)
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
......@@ -676,11 +754,11 @@ describe('required server files', () => {
appPort,
'/some-other-path?nxtPslug=first',
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/dynamic/[slug]',
},
}
})
)
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
......@@ -693,11 +771,11 @@ describe('required server files', () => {
appPort,
'/some-other-path?nxtPslug=second',
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/dynamic/[slug]',
},
}
})
)
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
......@@ -711,11 +789,11 @@ describe('required server files', () => {
appPort,
'/some-other-path?nxtPslug=second',
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/dynamic/[slug]',
},
}
})
)
const $3 = cheerio.load(html3)
const data3 = JSON.parse($3('#props').text())
......@@ -727,14 +805,19 @@ describe('required server files', () => {
})
it('should render fallback page correctly with x-matched-path and routes-matches', async () => {
const html = await renderViaHTTP(appPort, '/fallback/first', undefined, {
headers: {
'x-matched-path': '/fallback/first',
'x-now-route-matches': createNowRouteMatches({
slug: 'first',
}).toString(),
},
})
const html = await renderViaHTTP(
appPort,
'/fallback/first',
undefined,
withInvocationId({
headers: {
'x-matched-path': '/fallback/first',
'x-now-route-matches': createNowRouteMatches({
slug: 'first',
}).toString(),
},
})
)
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
......@@ -742,14 +825,19 @@ describe('required server files', () => {
expect($('#slug').text()).toBe('first')
expect(data.hello).toBe('world')
const html2 = await renderViaHTTP(appPort, `/fallback/[slug]`, undefined, {
headers: {
'x-matched-path': '/fallback/[slug]',
'x-now-route-matches': createNowRouteMatches({
slug: 'second',
}).toString(),
},
})
const html2 = await renderViaHTTP(
appPort,
`/fallback/[slug]`,
undefined,
withInvocationId({
headers: {
'x-matched-path': '/fallback/[slug]',
'x-now-route-matches': createNowRouteMatches({
slug: 'second',
}).toString(),
},
})
)
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
......@@ -760,14 +848,19 @@ describe('required server files', () => {
})
it('should favor valid route params over routes-matches', async () => {
const html = await renderViaHTTP(appPort, '/fallback/first', undefined, {
headers: {
'x-matched-path': '/fallback/first',
'x-now-route-matches': createNowRouteMatches({
slug: 'fallback/first',
}).toString(),
},
})
const html = await renderViaHTTP(
appPort,
'/fallback/first',
undefined,
withInvocationId({
headers: {
'x-matched-path': '/fallback/first',
'x-now-route-matches': createNowRouteMatches({
slug: 'fallback/first',
}).toString(),
},
})
)
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
......@@ -775,14 +868,19 @@ describe('required server files', () => {
expect($('#slug').text()).toBe('first')
expect(data.hello).toBe('world')
const html2 = await renderViaHTTP(appPort, `/fallback/second`, undefined, {
headers: {
'x-matched-path': '/fallback/[slug]',
'x-now-route-matches': createNowRouteMatches({
slug: 'fallback/second',
}).toString(),
},
})
const html2 = await renderViaHTTP(
appPort,
`/fallback/second`,
undefined,
withInvocationId({
headers: {
'x-matched-path': '/fallback/[slug]',
'x-now-route-matches': createNowRouteMatches({
slug: 'fallback/second',
}).toString(),
},
})
)
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
......@@ -793,24 +891,34 @@ describe('required server files', () => {
})
it('should favor valid route params over routes-matches optional', async () => {
const html = await renderViaHTTP(appPort, '/optional-ssg', undefined, {
headers: {
'x-matched-path': '/optional-ssg',
'x-now-route-matches': '',
},
})
const html = await renderViaHTTP(
appPort,
'/optional-ssg',
undefined,
withInvocationId({
headers: {
'x-matched-path': '/optional-ssg',
'x-now-route-matches': '',
},
})
)
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
expect(data.params).toEqual({})
const html2 = await renderViaHTTP(appPort, `/optional-ssg`, undefined, {
headers: {
'x-matched-path': '/optional-ssg',
'x-now-route-matches': createNowRouteMatches({
slug: 'another',
}).toString(),
},
})
const html2 = await renderViaHTTP(
appPort,
`/optional-ssg`,
undefined,
withInvocationId({
headers: {
'x-matched-path': '/optional-ssg',
'x-now-route-matches': createNowRouteMatches({
slug: 'another',
}).toString(),
},
})
)
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
......@@ -825,11 +933,11 @@ describe('required server files', () => {
slug: 'first',
}).toString()}`,
undefined,
{
withInvocationId({
headers: {
'x-matched-path': `/dynamic/[slug]`,
},
}
})
)
const { pageProps: data } = await res.json()
......@@ -841,14 +949,14 @@ describe('required server files', () => {
appPort,
`/_next/data/${next.buildId}/fallback/[slug].json`,
undefined,
{
withInvocationId({
headers: {
'x-matched-path': `/_next/data/${next.buildId}/fallback/[slug].json`,
'x-now-route-matches': createNowRouteMatches({
slug: 'second',
}).toString(),
},
}
})
)
const { pageProps: data2 } = await res2.json()
......@@ -862,12 +970,12 @@ describe('required server files', () => {
appPort,
'/catch-all/[[...rest]]',
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/catch-all/[[...rest]]',
'x-now-route-matches': '',
},
}
})
)
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
......@@ -880,14 +988,14 @@ describe('required server files', () => {
appPort,
'/catch-all/[[...rest]]',
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/catch-all/[[...rest]]',
'x-now-route-matches': createNowRouteMatches({
rest: 'hello',
}).toString(),
},
}
})
)
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
......@@ -901,14 +1009,14 @@ describe('required server files', () => {
appPort,
'/catch-all/[[...rest]]',
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/catch-all/[[...rest]]',
'x-now-route-matches': createNowRouteMatches({
rest: 'hello/world',
}).toString(),
},
}
})
)
const $3 = cheerio.load(html3)
const data3 = JSON.parse($3('#props').text())
......@@ -925,11 +1033,11 @@ describe('required server files', () => {
`/_next/data/${next.buildId}/catch-all.json`,
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/catch-all/[[...rest]]',
},
}
})
)
const { pageProps: data } = await res.json()
......@@ -941,14 +1049,14 @@ describe('required server files', () => {
appPort,
`/_next/data/${next.buildId}/catch-all/[[...rest]].json`,
undefined,
{
withInvocationId({
headers: {
'x-matched-path': `/_next/data/${next.buildId}/catch-all/[[...rest]].json`,
'x-now-route-matches': createNowRouteMatches({
rest: 'hello',
}).toString(),
},
}
})
)
const { pageProps: data2 } = await res2.json()
......@@ -960,14 +1068,14 @@ describe('required server files', () => {
appPort,
`/_next/data/${next.buildId}/catch-all/[[...rest]].json`,
undefined,
{
withInvocationId({
headers: {
'x-matched-path': `/_next/data/${next.buildId}/catch-all/[[...rest]].json`,
'x-now-route-matches': createNowRouteMatches({
rest: 'hello/world',
}).toString(),
},
}
})
)
const { pageProps: data3 } = await res3.json()
......@@ -986,9 +1094,14 @@ describe('required server files', () => {
'/fallback/another/',
'/fallback/another',
]) {
const res = await fetchViaHTTP(appPort, path, undefined, {
redirect: 'manual',
})
const res = await fetchViaHTTP(
appPort,
path,
undefined,
withInvocationId({
redirect: 'manual',
})
)
expect(res.status).toBe(200)
}
......@@ -1001,11 +1114,11 @@ describe('required server files', () => {
{
path: 'hello/world',
},
{
withInvocationId({
headers: {
'x-matched-path': '/gssp',
},
}
})
)
const $ = cheerio.load(html)
expect(JSON.parse($('#router').text()).query).toEqual({
......@@ -1020,33 +1133,43 @@ describe('required server files', () => {
{
path: '%c0.%c0.',
},
{
withInvocationId({
headers: {
'x-matched-path': '/dynamic/[slug]',
},
}
})
)
expect(res.status).toBe(400)
expect(await res.text()).toContain('Bad Request')
})
it('should have correct resolvedUrl from rewrite', async () => {
const res = await fetchViaHTTP(appPort, '/to-dynamic/post-1', undefined, {
headers: {
'x-matched-path': '/dynamic/[slug]',
},
})
const res = await fetchViaHTTP(
appPort,
'/to-dynamic/post-1',
undefined,
withInvocationId({
headers: {
'x-matched-path': '/dynamic/[slug]',
},
})
)
expect(res.status).toBe(200)
const $ = cheerio.load(await res.text())
expect($('#resolved-url').text()).toBe('/dynamic/post-1')
})
it('should have correct resolvedUrl from rewrite with added query', async () => {
const res = await fetchViaHTTP(appPort, '/to-dynamic/post-2', undefined, {
headers: {
'x-matched-path': '/dynamic/[slug]',
},
})
const res = await fetchViaHTTP(
appPort,
'/to-dynamic/post-2',
undefined,
withInvocationId({
headers: {
'x-matched-path': '/dynamic/[slug]',
},
})
)
expect(res.status).toBe(200)
const $ = cheerio.load(await res.text())
expect($('#resolved-url').text()).toBe('/dynamic/post-2')
......@@ -1058,11 +1181,11 @@ describe('required server files', () => {
appPort,
`/_next/data/${next.buildId}/dynamic/post-2.json`,
{ slug: 'post-2' },
{
withInvocationId({
headers: {
'x-matched-path': '/dynamic/[slug]',
},
}
})
)
expect(res.status).toBe(200)
const json = await res.json()
......@@ -1070,7 +1193,12 @@ describe('required server files', () => {
})
it('should bubble error correctly for gip page', async () => {
const res = await fetchViaHTTP(appPort, '/errors/gip', { crash: '1' })
const res = await fetchViaHTTP(
appPort,
'/errors/gip',
{ crash: '1' },
withInvocationId()
)
expect(res.status).toBe(500)
expect(await res.text()).toBe('Internal Server Error')
......@@ -1080,7 +1208,12 @@ describe('required server files', () => {
})
it('should bubble error correctly for gssp page', async () => {
const res = await fetchViaHTTP(appPort, '/errors/gssp', { crash: '1' })
const res = await fetchViaHTTP(
appPort,
'/errors/gssp',
{ crash: '1' },
withInvocationId()
)
expect(res.status).toBe(500)
expect(await res.text()).toBe('Internal Server Error')
......@@ -1090,7 +1223,12 @@ describe('required server files', () => {
})
it('should bubble error correctly for gsp page', async () => {
const res = await fetchViaHTTP(appPort, '/errors/gsp/crash')
const res = await fetchViaHTTP(
appPort,
'/errors/gsp/crash',
undefined,
withInvocationId()
)
expect(res.status).toBe(500)
expect(await res.text()).toBe('Internal Server Error')
......@@ -1100,7 +1238,12 @@ describe('required server files', () => {
})
it('should bubble error correctly for API page', async () => {
const res = await fetchViaHTTP(appPort, '/api/error')
const res = await fetchViaHTTP(
appPort,
'/api/error',
undefined,
withInvocationId()
)
expect(res.status).toBe(500)
expect(await res.text()).toBe('Internal Server Error')
......@@ -1114,11 +1257,11 @@ describe('required server files', () => {
appPort,
'/optional-ssp',
{ nxtPrest: '', another: 'value' },
{
withInvocationId({
headers: {
'x-matched-path': '/optional-ssp/[[...rest]]',
},
}
})
)
const html = await res.text()
......@@ -1133,11 +1276,11 @@ describe('required server files', () => {
appPort,
'/optional-ssg',
{ nxtPrest: '', another: 'value' },
{
withInvocationId({
headers: {
'x-matched-path': '/optional-ssg/[[...rest]]',
},
}
})
)
const html = await res.text()
......@@ -1187,7 +1330,7 @@ describe('required server files', () => {
headers: {
'x-matched-path': `/_next/data/${next.buildId}/optional-ssg/[[...rest]].json`,
'x-now-route-matches': '',
'x-vercel-id': 'cle1::',
'x-invocation-id': 'cle1::',
},
},
{
......@@ -1195,7 +1338,7 @@ describe('required server files', () => {
headers: {
'x-matched-path': `/_next/data/${next.buildId}/optional-ssg/[[...rest]].json`,
'x-now-route-matches': '',
'x-vercel-id': 'cle1::',
'x-invocation-id': 'cle1::',
},
},
{
......@@ -1203,7 +1346,7 @@ describe('required server files', () => {
headers: {
'x-matched-path': `/optional-ssg/[[...rest]]`,
'x-now-route-matches': '',
'x-vercel-id': 'cle1::',
'x-invocation-id': 'cle1::',
},
},
]
......@@ -1211,6 +1354,7 @@ describe('required server files', () => {
for (const req of reqs) {
console.error('checking', req)
const res = await fetchViaHTTP(appPort, req.path, req.query, {
...withInvocationId(),
headers: req.headers,
})
......@@ -1233,12 +1377,12 @@ describe('required server files', () => {
appPort,
'/optional-ssg/[[...rest]]',
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/optional-ssg/[[...rest]]',
'x-now-route-matches': 'nxtPrest=en%2Fes%2Fhello%252Fworld',
},
}
})
)
const html = await res.text()
......@@ -1254,11 +1398,11 @@ describe('required server files', () => {
appPort,
'/api/optional',
{ nxtPrest: '', another: 'value' },
{
withInvocationId({
headers: {
'x-matched-path': '/api/optional/[[...rest]]',
},
}
})
)
const json = await res.json()
......@@ -1271,11 +1415,11 @@ describe('required server files', () => {
appPort,
'/api/optional/index',
{ nxtPrest: 'index', another: 'value' },
{
withInvocationId({
headers: {
'x-matched-path': '/api/optional/[[...rest]]',
},
}
})
)
const json = await res.json()
......@@ -1284,12 +1428,17 @@ describe('required server files', () => {
})
it('should match the index page correctly', async () => {
const res = await fetchViaHTTP(appPort, '/', undefined, {
headers: {
'x-matched-path': '/index',
},
redirect: 'manual',
})
const res = await fetchViaHTTP(
appPort,
'/',
undefined,
withInvocationId({
headers: {
'x-matched-path': '/index',
},
redirect: 'manual',
})
)
const html = await res.text()
const $ = cheerio.load(html)
......@@ -1297,12 +1446,17 @@ describe('required server files', () => {
})
it('should match the root dynamic page correctly', async () => {
const res = await fetchViaHTTP(appPort, '/slug-1', undefined, {
headers: {
'x-matched-path': '/[slug]',
},
redirect: 'manual',
})
const res = await fetchViaHTTP(
appPort,
'/slug-1',
undefined,
withInvocationId({
headers: {
'x-matched-path': '/[slug]',
},
redirect: 'manual',
})
)
const html = await res.text()
const $ = cheerio.load(html)
......@@ -1311,12 +1465,17 @@ describe('required server files', () => {
slug: 'slug-1',
})
const res2 = await fetchViaHTTP(appPort, '/[slug]', undefined, {
headers: {
'x-matched-path': '/[slug]',
},
redirect: 'manual',
})
const res2 = await fetchViaHTTP(
appPort,
'/[slug]',
undefined,
withInvocationId({
headers: {
'x-matched-path': '/[slug]',
},
redirect: 'manual',
})
)
const html2 = await res2.text()
const $2 = cheerio.load(html2)
......@@ -1327,12 +1486,17 @@ describe('required server files', () => {
})
it('should have correct asPath on dynamic SSG page correctly', async () => {
const res = await fetchViaHTTP(appPort, '/an-ssg-path', undefined, {
headers: {
'x-matched-path': '/[slug]',
},
redirect: 'manual',
})
const res = await fetchViaHTTP(
appPort,
'/an-ssg-path',
undefined,
withInvocationId({
headers: {
'x-matched-path': '/[slug]',
},
redirect: 'manual',
})
)
const html = await res.text()
const $ = cheerio.load(html)
......@@ -1353,12 +1517,17 @@ describe('required server files', () => {
]
for (const check of toCheck) {
console.warn('checking', check)
const res = await fetchViaHTTP(appPort, check.pathname, undefined, {
headers: {
'x-matched-path': check.matchedPath,
},
redirect: 'manual',
})
const res = await fetchViaHTTP(
appPort,
check.pathname,
undefined,
withInvocationId({
headers: {
'x-matched-path': check.matchedPath,
},
redirect: 'manual',
})
)
const html = await res.text()
const $ = cheerio.load(html)
......@@ -1370,7 +1539,12 @@ describe('required server files', () => {
})
it('should read .env files and process.env', async () => {
const res = await fetchViaHTTP(appPort, '/api/env')
const res = await fetchViaHTTP(
appPort,
'/api/env',
undefined,
withInvocationId()
)
const envVariables = await res.json()
......@@ -1387,7 +1561,12 @@ describe('required server files', () => {
it('should run middleware correctly', async () => {
const standaloneDir = join(next.testDir, 'standalone')
const res = await fetchViaHTTP(appPort, '/')
const res = await fetchViaHTTP(
appPort,
'/',
undefined,
withInvocationId()
)
expect(res.status).toBe(200)
expect(await res.text()).toContain('index page')
......@@ -1405,7 +1584,9 @@ describe('required server files', () => {
const resImageResponse = await fetchViaHTTP(
appPort,
'/a-non-existent-page/to-test-with-middleware'
'/a-non-existent-page/to-test-with-middleware',
undefined,
withInvocationId()
)
expect(resImageResponse.status).toBe(200)
......@@ -1418,11 +1599,11 @@ describe('required server files', () => {
appPort,
`/_next/data/${nanoid()}/index.json`,
undefined,
{
withInvocationId({
headers: {
'x-matched-path': '/[teamSlug]/[project]/[id]/[suffix]',
},
}
})
)
expect(res.status).toBe(404)
......
......@@ -96,7 +96,7 @@ describe('minimal-mode-response-cache', () => {
vary: 'rsc, next-router-state-tree, next-router-prefetch',
'x-now-route-matches': '1=compare&rsc=1',
'x-matched-path': '/app-blog/compare.rsc',
'x-vercel-id': '1',
'x-invocation-id': '1',
rsc: '1',
}
const res1 = await fetchViaHTTP(
......@@ -126,7 +126,7 @@ describe('minimal-mode-response-cache', () => {
vary: 'rsc, next-router-state-tree, next-router-prefetch',
'x-now-route-matches': '1=app-another&rsc=1',
'x-matched-path': '/app-another.rsc',
'x-vercel-id': '1',
'x-invocation-id': '1',
rsc: '1',
}
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