Unverified Commit 7418b26d authored by Josh Story's avatar Josh Story Committed by GitHub

Support dynamicIO in middlware routes and generateStaticParams (#70544)

route.ts files (and other routes like metadata routes) still need
dynamicIO semantics when runnign in edge runtime. This change adds
support for configuring dynamicIO for edge routes. It is hard to test
properly because edge routes never statically generate and at the moment
there are no other observable semantics. If we introduce new semantics
that are distinct for dynamicIO that affect dynamic rendering we should
update these tests to assert them.

Similarly generateStaticParams also needs dynamicIO semantics when
configured. Right now it's not quite possible to assert this because
there are no observable semantics. We should have one which is that
fetchCache is not configurable with dynamicIO on however that isn't
implemented yet. This change adds tests but they will need to be updated
once we update the fetchCache behavior
parent 00156f54
......@@ -1424,7 +1424,7 @@ export async function buildAppStaticPaths({
isRevalidate: false,
experimental: {
after: false,
dynamicIO: false,
dynamicIO,
},
buildId,
},
......
......@@ -115,7 +115,7 @@ export class EdgeRouteModuleWrapper {
: undefined,
experimental: {
after: isAfterEnabled,
dynamicIO: false,
dynamicIO: !!process.env.__NEXT_DYNAMIC_IO,
},
buildId: '', // TODO: Populate this properly.
},
......
// TODO once we make fetchCache inert with dynamicIO this test is expected
// to start failing. Right now the force cache causes the fetches to be identical
// and we get only one prebuilt route. once we remove the caching behavior of fetchCache
// when dynamicIO is on we will get more than one route.
// The ideal test wouldn't even use fetchCache but at the moment the default caching for fetch
// is to not cache and so we can't rely on the default to produce a differentiating result.
export const fetchCache = 'default-cache'
export async function generateStaticParams() {
const set = new Set()
set.add(await fetchRandom('a'))
set.add(await fetchRandom('a'))
return Array.from(set).map((value) => {
return {
slug: ('' + value).slice(2),
}
})
}
export default async function Layout({ children, params }) {
return (
<>
<h1>{await params.slug}</h1>
<section>{children}</section>
</>
)
}
const fetchRandom = async (entropy: string) => {
const response = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random?b=' + entropy
)
return response.text()
}
import type { NextRequest } from 'next/server'
import { getSentinelValue } from '../../../../getSentinelValue'
export const runtime = 'edge'
export async function generateStaticParams() {
return [
{
dyn: '1',
},
]
}
export async function GET(
request: NextRequest,
props: { params: Promise<{ dyn: string }> }
) {
const { dyn } = await props.params
return new Response(
JSON.stringify({
value: getSentinelValue(),
type: 'dynamic params',
param: dyn,
})
)
}
import type { NextRequest, UnsafeUnwrappedParams } from 'next/server'
import { getSentinelValue } from '../../../../getSentinelValue'
export const runtime = 'edge'
export async function generateStaticParams() {
return [
{
dyn: '1',
},
]
}
export async function GET(
request: NextRequest,
props: { params: Promise<{ dyn: string }> }
) {
const dyn = (
props.params as unknown as UnsafeUnwrappedParams<typeof props.params>
).dyn
return new Response(
JSON.stringify({
value: getSentinelValue(),
type: 'dynamic params',
param: dyn,
})
)
}
import type { NextRequest } from 'next/server'
import { cookies } from 'next/headers'
import { getSentinelValue } from '../../../getSentinelValue'
export const runtime = 'edge'
export async function GET(request: NextRequest) {
const sentinel = (await cookies()).get('x-sentinel')
return new Response(
JSON.stringify({
value: getSentinelValue(),
type: 'cookies',
'x-sentinel': sentinel,
})
)
}
import type { NextRequest } from 'next/server'
import { headers } from 'next/headers'
import { getSentinelValue } from '../../../getSentinelValue'
export const runtime = 'edge'
export async function GET(request: NextRequest) {
const sentinel = (await headers()).get('x-sentinel')
return new Response(
JSON.stringify({
value: getSentinelValue(),
type: 'headers',
'x-sentinel': sentinel,
})
)
}
import type { NextRequest } from 'next/server'
import { getSentinelValue } from '../../../getSentinelValue'
export const runtime = 'edge'
export async function GET(request: NextRequest) {
const result = JSON.stringify({
value: getSentinelValue(),
message: 'dynamic stream',
})
const part1 = result.slice(0, result.length / 2)
const part2 = result.slice(result.length / 2)
const encoder = new TextEncoder()
const chunks = [encoder.encode(part1), encoder.encode(part2)]
let sent = 0
const stream = new ReadableStream({
async pull(controller) {
controller.enqueue(chunks[sent++])
await new Promise((r) => setTimeout(r, 1))
if (sent === chunks.length) {
controller.close()
}
},
})
return new Response(stream)
}
import type { NextRequest } from 'next/server'
import { getSentinelValue } from '../../../getSentinelValue'
export const runtime = 'edge'
export async function GET(request: NextRequest) {
const search = request.nextUrl.search
return new Response(
JSON.stringify({
value: getSentinelValue(),
search,
})
)
}
import type { NextRequest } from 'next/server'
import { getSentinelValue } from '../../../getSentinelValue'
export const runtime = 'edge'
export async function GET(request: NextRequest) {
const fetcheda = await fetchRandomCached('a')
const fetchedb = await fetchRandomCached('b')
return new Response(
JSON.stringify({
value: getSentinelValue(),
random1: fetcheda,
random2: fetchedb,
})
)
}
const fetchRandomCached = async (entropy: string) => {
const response = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random?b=' + entropy,
{ cache: 'force-cache' }
)
return response.text()
}
import type { NextRequest } from 'next/server'
import { getSentinelValue } from '../../../getSentinelValue'
export const runtime = 'edge'
export async function GET(request: NextRequest) {
const fetcheda = await fetchRandomCached('a')
const fetchedb = await fetchRandomUncached('b')
return new Response(
JSON.stringify({
value: getSentinelValue(),
random1: fetcheda,
random2: fetchedb,
})
)
}
const fetchRandomCached = async (entropy: string) => {
const response = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random?b=' + entropy,
{ cache: 'force-cache' }
)
return response.text()
}
const fetchRandomUncached = async (entropy: string) => {
const response = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random?b=' + entropy
)
return response.text()
}
import type { NextRequest } from 'next/server'
import { unstable_cache as cache } from 'next/cache'
import { getSentinelValue } from '../../../getSentinelValue'
export const runtime = 'edge'
export async function GET(request: NextRequest) {
const messagea = await getCachedMessage('hello cached fast', 0)
const messageb = await getCachedMessage('hello cached slow', 20)
return new Response(
JSON.stringify({
value: getSentinelValue(),
message1: messagea,
message2: messageb,
})
)
}
async function getMessage(echo, delay) {
const tag = ((Math.random() * 10000) | 0).toString(16)
await new Promise((r) => setTimeout(r, delay))
return `${tag}:${echo}`
}
const getCachedMessage = cache(getMessage)
import type { NextRequest } from 'next/server'
import { unstable_cache as cache } from 'next/cache'
import { getSentinelValue } from '../../../getSentinelValue'
export const runtime = 'edge'
export async function GET(request: NextRequest) {
const messagea = await getCachedMessage('hello cached fast', 0)
const messageb = await getMessage('hello uncached slow', 20)
return new Response(
JSON.stringify({
value: getSentinelValue(),
message1: messagea,
message2: messageb,
})
)
}
async function getMessage(echo, delay) {
const tag = ((Math.random() * 10000) | 0).toString(16)
await new Promise((r) => setTimeout(r, delay))
return `${tag}:${echo}`
}
const getCachedMessage = cache(getMessage)
import type { NextRequest } from 'next/server'
import { getSentinelValue } from '../../../getSentinelValue'
export const runtime = 'edge'
export async function GET(request: NextRequest) {
await Promise.resolve()
const response = JSON.stringify({
value: getSentinelValue(),
message: 'microtask',
})
return new Response(response)
}
import type { NextRequest } from 'next/server'
import { getSentinelValue } from '../../../getSentinelValue'
export const runtime = 'edge'
export async function GET(request: NextRequest) {
const response = JSON.stringify({
value: getSentinelValue(),
message: 'stream response',
})
const part1 = response.slice(0, Math.floor(response.length / 2))
const part2 = response.slice(Math.floor(response.length / 2))
const encoder = new TextEncoder()
const chunks = [encoder.encode(part1), encoder.encode(part2)]
let sent = 0
const stream = new ReadableStream({
pull(controller) {
controller.enqueue(chunks[sent++])
if (sent === chunks.length) {
controller.close()
}
},
})
return new Response(stream)
}
import type { NextRequest } from 'next/server'
import { getSentinelValue } from '../../../getSentinelValue'
export const runtime = 'edge'
export function GET(request: NextRequest) {
const response = JSON.stringify({
value: getSentinelValue(),
message: 'stream response',
})
const part1 = response.slice(0, Math.floor(response.length / 2))
const part2 = response.slice(Math.floor(response.length / 2))
const encoder = new TextEncoder()
const chunks = [encoder.encode(part1), encoder.encode(part2)]
let sent = 0
const stream = new ReadableStream({
pull(controller) {
controller.enqueue(chunks[sent++])
if (sent === chunks.length) {
controller.close()
}
},
})
return new Response(stream)
}
import type { NextRequest } from 'next/server'
import { getSentinelValue } from '../../../getSentinelValue'
export const runtime = 'edge'
export async function GET(request: NextRequest) {
const response = JSON.stringify({
value: getSentinelValue(),
message: 'string response',
})
return new Response(response)
}
import type { NextRequest } from 'next/server'
import { getSentinelValue } from '../../../getSentinelValue'
export const runtime = 'edge'
export function GET(request: NextRequest) {
const response = JSON.stringify({
value: getSentinelValue(),
message: 'string response',
})
return new Response(response)
}
import type { NextRequest } from 'next/server'
import { getSentinelValue } from '../../../getSentinelValue'
export const runtime = 'edge'
export async function GET(request: NextRequest) {
await new Promise((r) => setTimeout(r, 10))
const response = JSON.stringify({
value: getSentinelValue(),
message: 'task',
})
return new Response(response)
}
......@@ -2,6 +2,8 @@ import type { NextRequest } from 'next/server'
import { getSentinelValue } from '../../getSentinelValue'
export const runtime = 'edge'
export async function GET(request: NextRequest) {
const fetcheda = await fetchRandomCached('a')
const fetchedb = await fetchRandomUncached('b')
......
......@@ -2481,4 +2481,49 @@ describe('dynamic-io', () => {
}
})
})
if (!isNextDev) {
describe('generateStaticParams', () => {
it('should have dynamicIO semantics inside generateStaticParams', async () => {
// This test is named what we want but our current implementation is not actually correct yet.
// We are asserting current behavior and will update the test when we land the correct behavior
const lines: Array<string> = next.cliOutput.split('\n')
let i = 0
while (true) {
const line = lines[i++]
if (typeof line !== 'string') {
throw new Error(
'Could not find expected route output for /params/generate-static-params/[slug]/page/...'
)
}
if (
line.startsWith('├') &&
line.includes('/params/generate-static-params/[slug]')
) {
let nextLine = lines[i++]
if (WITH_PPR) {
// when PPR is on (in this test suite) we also turn on fallbacks.
// we expect the fallback shell first
expect(nextLine).toContain(
'/params/generate-static-params/[slug]'
)
nextLine = lines[i++]
}
expect(nextLine).toMatch(
/\/params\/generate-static-params\/\d+\/page/
)
nextLine = lines[i++]
// Because we force-cache we only end up with one prebuilt page.
// When dynamicIO semantics are fully respected we will end up with two.
expect(nextLine).not.toMatch(
/\/params\/generate-static-params\/\d+\/page/
)
break
}
}
})
})
}
})
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