Next.js Discord

Discord Forum

Static API Routes

Answered
stanls posted this in #help-forum
Open in Discord
Hello, I've come across something interesting yesterday with the static API routes in Next.js. I'm not sure if it's supposed to behave this way, since it was not documented in the docs as well (but there's also a possibility that I might have missed it). I'm using the Next.js 13 with the App Router.

I have a route handler with the following code and will be executed when I call GET /api/data/subdata
import { NextResponse } from 'next/server'
import connectToDatabase from '@lib/mongoose'
const Data = require('@lib/models/data')

export async function GET(request: Request) {
    await connectToDatabase()

    let data = await Data.getData()
    return NextResponse.json(data)
}

Upon initial load of the page, it was able to get the data without any problem. But where there is a change in the data through CRUD operation, and I want to retrieve an updated version of the data, calling this API route will not give me the updated data. This happened in the production environment. Initially, I thought this was just a cache problem with React Query or SWR, but I even did the manual data fetching, and it's still not getting updated. Interestingly, it works just fine in local.

Upon running the build locally, I noticed that this API route has become static, and looking further, all static API routes shown after build have common similarity, where they didn't use the request parameter. So I modified the code to the following, just so that the request parameter is being used, and it WORKS! 😅
export async function GET(request: Request) {
    await connectToDatabase()
    const _ = new URL(request.url)

    let data = await Data.getData()
    return NextResponse.json(data)
}

Was it supposed to behave this way? Does this mean when the request parameter is not used in the route handler, Next.js automatically marked that route as a static API route?
Answered by DirtyCajunRice | AppDir
yes. it needs to be able to discern its dynamic somehow. you can also just export dynamic = "force-dynamic"; instead of having an unused request variable
View full answer

2 Replies

@stanls Hello, I've come across something interesting yesterday with the static API routes in Next.js. I'm not sure if it's supposed to behave this way, since it was not documented in the docs as well (but there's also a possibility that I might have missed it). I'm using the Next.js 13 with the App Router. I have a route handler with the following code and will be executed when I call `GET /api/data/subdata` import { NextResponse } from 'next/server' import connectToDatabase from '@lib/mongoose' const Data = require('@lib/models/data') export async function GET(request: Request) { await connectToDatabase() let data = await Data.getData() return NextResponse.json(data) } Upon initial load of the page, it was able to get the data without any problem. But where there is a change in the data through CRUD operation, and I want to retrieve an updated version of the data, calling this API route will not give me the updated data. This happened in the production environment. Initially, I thought this was just a cache problem with React Query or SWR, but I even did the manual data fetching, and it's still not getting updated. Interestingly, it works just fine in local. Upon running the build locally, I noticed that this API route has become **static**, and looking further, all static API routes shown after build have common similarity, where **they didn't use the `request` parameter**. So I modified the code to the following, just so that the `request` parameter is being used, and it WORKS! 😅 export async function GET(request: Request) { await connectToDatabase() const _ = new URL(request.url) let data = await Data.getData() return NextResponse.json(data) } Was it supposed to behave this way? Does this mean when the `request` parameter is not used in the route handler, Next.js automatically marked that route as a static API route?
yes. it needs to be able to discern its dynamic somehow. you can also just export dynamic = "force-dynamic"; instead of having an unused request variable
Answer