Next.js Discord

Discord Forum

No response is returned from route handler

Answered
Large oak-apple gall posted this in #help-forum
Open in Discord
Avatar
Large oak-apple gallOP
I get this error in one of api endpoints.
This endpoint is only fetched by react query as prefetch and normal query and happens for prefetch only.

I am 95% sure I covered returns for all situations.
If - response, if - response, return cache - try - response, catch - response.

Route handler
import dayjs from 'dayjs'
import { unstable_cache } from 'next/cache'
import type { NextRequest } from 'next/server'

export async function GET(request: NextRequest) {
    const date = request.nextUrl.searchParams.get('date')

    if (!date) {
        return new Response('No date', { status: 400 })
    }

    const isValidDate = dayjs(date).isValid()

    if (!isValidDate) {
        return new Response(`Invalid date, ${date}`, { status: 400 })
    }

    return unstable_cache(async (date: string) => {
        try {
            const res = await fetch(`https://isdayoff.ru/${date}`)
            const dayStatus = await res.text()

            return new Response(dayStatus)
        } catch (error) {
            console.error(error)

            return new Response('Error', { status: 500 })
        }
    })(date)
}


Strange thing 1: changing cached function in any way (even adding console log) fixes it, but reverting this change breaks it again.
Strange thing 2: opening dev tools in browser fixes it. Closing dev tools breaks it again.
Answered by B33fb0n3
delete your .next folder and restart your app. Then try again
View full answer

3 Replies