Files
next.js/errors/next-prerender-sync-request.md
T
Josh StoryandGitHub 1f462140b9 [dynamicIO] refine error messaging for sync API access (#71467)
Temporarily removing the dev warnings. I've moved these to be errors in
the build side. In a follow up PR I will use prerendering during dev to
trigger these errors and report them as dev errors as well.

This also updates how we track sync dynamic errors. The main goal is to
have a stack trace of the sync function and separately report all the
component stacks that might contain this function call. It's not
fool-proof b/c the sync api may be called in a way that is not observed
by SSR. The whole debugging story here will improve dramatically with
owner stacks but that is currently experimental only in React.

The plan for future updates is to add a prospective render for SSR so we
can flush out module scope issues like calling Math.random while lazy
initializing a module. This is asserted for RSC in this change (see new
tests) but won't wouldn't pass for SSR b/c we only do single pass
prerendering at the moment.

After that I will add in the prerender-while-render in dev and utilize
the prerender errors to power dev as well.
2024-10-18 15:08:50 -07:00

1.6 KiB

title
title
Cannot access Request information synchronously in route.js without awaiting connection first

Why This Error Occurred

When dyanmicIO is enabled Next.js treats most access to Request information as explicitly dynamic. However routes have a Request object and you can read values like the current pathname from that object synchronously. If you need to access Request information in a route handler you should first call await connection() to inform Next.js that everything after this point is explicitly dynamic.

This is only needed in GET handlers because GET is the only handler Next.js will attempt to prerender.

Possible Ways to Fix It

Look for access of the Request object in your route.js and add await connection() before you access any properties of the Request.

Before:

export default function GET(request) {
  const requestHeaders = request.headers
  return ...
}

After:

import { connection } from 'next/server'

export default async function GET(request) {
  await connection()
  const requestHeaders = request.headers
  return ...
}

It is unexpected that you would run the codemod and not successfully convert all instances of params and searchParams to async or have a marker string to help you locate unconverted cases. If you do find yourself in this situation please report this to Next.js on Github.