Next.js Discord

Discord Forum

Opting out of caching clarification

Answered
Saltwater Crocodile posted this in #help-forum
Open in Discord
Saltwater CrocodileOP
In the following documentation about opting out of caching https://nextjs.org/docs/app/building-your-application/routing/route-handlers#opting-out-of-caching it says,
Using Request object with the GET method.

And in the example code:
export async function GET(request: Request) {
  const { searchParams } = new URL(request.url)
  const id = searchParams.get('id')
  const res = await fetch(`https://data.mongodb-api.com/product/${id}`, {
    headers: {
      'Content-Type': 'application/json',
      'API-Key': process.env.DATA_API_KEY!,
    },
  })
  const product = await res.json()
 
  return Response.json({ product })
}


At which point in the sample code determines that I'm opting out of caching?
Answered by B33fb0n3
The opt out is right on top:
const { searchParams } = new URL(request.url)

In this case, because he use the request object (in this case to get the searchParams)
View full answer

5 Replies

Answer
Saltwater CrocodileOP
@B33fb0n3 Thanks for the reply. I assume that Next is doing something special to know when the request obj is used then.
yep it does analyse the source code during the compile step and determine that automatically
happy to help