Next.js Discord

Discord Forum

Is it not allowed to use `request.url` inside of a `route.ts` handler?

Answered
Havana posted this in #help-forum
Open in Discord
HavanaOP
Why is using request.url inside of a route.ts handler cause this error during yarn run build? 🤔

DynamicServerError: Dynamic server usage: request.url
    at Object.staticGenerationBailout (/workspace/my-next/.next/server/chunks/654.js:206:21)
    at handleReqBailout (/workspace/my-next/.next/server/chunks/601.js:2365:23)
    at Object.get (/workspace/my-next/.next/server/chunks/601.js:2373:13)
    at validateGET (/workspace/my-next/.next/server/app/api/dataset/categories/route.js:152:47)
    at /workspace/my-next/.next/server/app/api/dataset/categories/route.js:173:29
    at /workspace/my-next/.next/server/app/api/dataset/categories/route.js:82:26
    at /workspace/my-next/.next/server/chunks/601.js:2659:43
    at /workspace/my-next/.next/server/chunks/601.js:3530:36
    at NoopContextManager.with (/workspace/my-next/.next/server/chunks/601.js:531:30)
    at ContextAPI.with (/workspace/my-next/.next/server/chunks/601.js:201:58) {
  digest: 'DYNAMIC_SERVER_USAGE'
}


The handler looks something like this:
export async function GET(request: NextRequest) {
  const { searchParams } = new URL(request.url)
  // other stuff ...
}
Answered by Havana
I realized the error is only logged during the build, but it doesn't fail. Adding export const dynamic = 'force-dynamic' made the error message disappear, but this still doesn't make sense to me.

So, eventually I narrowed the code inside that route.ts to this:

import { NextRequest, NextResponse } from 'next/server'

export async function GET(request: NextRequest) {
  try {
    const { searchParams } = new URL(request.url)
    // other code that may potentioally throw an exception
    // ...
    return NextResponse.json({ searchParams })
  } catch (e) {
    console.log(e)
    return NextResponse.json({ error: true })
  }
}


And if I omit the console.log(e), the error message disappears (without adding the force-dynamic flag). I'm guessing this how the App Router determines if a route is static or dynamic when it's set to auto.

The reason, I have the try / catch is because I have a function named withExceptionHandler which wraps the route handler and is supposed to catch exceptions like validation exceptions and return a custom response instead of a server error.

The way I solved this, is to not console.log() exceptions that are of different instances than the ones I'm handling, and to re throw them.
View full answer

4 Replies

Is this an error that fails the build or is it just being logged during the build?
HavanaOP
I realized the error is only logged during the build, but it doesn't fail. Adding export const dynamic = 'force-dynamic' made the error message disappear, but this still doesn't make sense to me.

So, eventually I narrowed the code inside that route.ts to this:

import { NextRequest, NextResponse } from 'next/server'

export async function GET(request: NextRequest) {
  try {
    const { searchParams } = new URL(request.url)
    // other code that may potentioally throw an exception
    // ...
    return NextResponse.json({ searchParams })
  } catch (e) {
    console.log(e)
    return NextResponse.json({ error: true })
  }
}


And if I omit the console.log(e), the error message disappears (without adding the force-dynamic flag). I'm guessing this how the App Router determines if a route is static or dynamic when it's set to auto.

The reason, I have the try / catch is because I have a function named withExceptionHandler which wraps the route handler and is supposed to catch exceptions like validation exceptions and return a custom response instead of a server error.

The way I solved this, is to not console.log() exceptions that are of different instances than the ones I'm handling, and to re throw them.
Answer
This is an intended error that turns the route it’s thrown in from a static route (default) into a dynamic route. You can ignore that error during builds. It’s incorrectly not muted and probably will be muted soon