Open mkbctrl opened 6 months ago
We have a suspect, that next-intl can potentially alter responses in a way the produces this error. For now it's just an unconfirmed assumption, but next-intl is what we added to the stack that was working smoothly earlier on.
We also run into another issue with next-intl, maybe someone with more context will be able to connect those dots faster: https://github.com/amannn/next-intl/issues/728
I'm having this issue too. Some findings: This works:
api.use((req, event, next) => {
return next(); // call next in chain
});
This does Not work:
api.use(async (req, event, next) => {
await next(); // call next in chain
});
Yielding error:
⨯ Error: No response is returned from route handler '......../src/app/api/waitlist/route.ts'. Ensure you return a `Response` or a `NextResponse` in all branches of your handler.
▲ Next.js 14.0.4
This is how I used it before, and I also got this error.
const router = createEdgeRouter<NextRequest, RequestContext>()
router
// A middleware example
.use(async (request, event, next) => {
const start = Date.now()
await next() // call next in chain
const end = Date.now()
console.log(`Request took ${end - start}ms`)
})
.get(async (request) => {
try {
return NextResponse.json([])
} catch (error: any) {
console.log('🚀 ~ GET ~ error:', error)
return NextResponse.json([])
}
})
export async function GET(request: NextRequest, ctx: RequestContext) {
return router.run(request, ctx)
}
Later I changed it to
const router = createEdgeRouter<NextRequest, RequestContext>()
router
// changed here
.use((request, event, next) => {
const start = Date.now()
const end = Date.now()
console.log(`Request took ${end - start}ms`)
return next()
})
.get(async (request) => {
try {
return NextResponse.json([])
} catch (error: any) {
console.log('🚀 ~ GET ~ error:', error)
return NextResponse.json([])
}
})
export async function GET(request: NextRequest, ctx: RequestContext) {
return router.run(request, ctx)
}
Then it's ok
router
.use(async (request, event, next) => {
const start = Date.now()
const end = Date.now()
console.log(`Request took ${end - start}ms`)
return (await next())
})
.get(async (request) => {
return Response.json([])
})
}
Also works for me in Nextjs 14.1 while keeping the the first function async.
Since we need to return next()
, I moved the timer to .finally()
since I haven't found a way to have it in the middleware.
router
.use(async (request, event, next) => {
return (await next())
})
.get(async (request) => {
return Response.json([])
})
}
export const GET = (request: NextRequest, ctx: RequestContext) => {
const start = Date.now();
return router.run(request, ctx).finally(() => {
const end = Date.now();
console.log(`Request took ${end - start}ms`);
});
};
Here is the route handler implementation:
As you can see it's pretty simple GET endpoint. For simplicity, I removed the GET related logic, as it seems it doesn't have anything to do with the problem.
Currently, when I am redirected from my auth form (Supabase Auth) to the
http://localhost:3000/api/v1/auth/callback?code=some-random-values
I get the following error:Which is odd, as I consider
return NextResponse.redirect(origin)
a valid response 🤔Here is the request object to provide more context:
Middlewares are pretty straightforward as well:
It's my attempt to convert my route to next-connect structure, it seems to me it's some sort of an issue with next-connect. However I am not next-connect heavy user, so messing smth up is also an option. It is however my 2nd issue with next-connect while working with the newest next.js 14. While working with older versions I didn't have issues like that.
Here is my regular route, that works without any issues: