next.js/test/e2e/app-dir/cache-components/app/routes/io-mixed/route.ts
route.ts26 lines702 B
import type { NextRequest } from 'next/server'

import { unstable_cache as cache } from 'next/cache'

import { getSentinelValue } from '../../getSentinelValue'

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)
Quest for Codev2.0.0
/
SIGN IN