v12.3.4 - How should I handle 404 and middleware.ts?
Unanswered
Minskin posted this in #help-forum
MinskinOP
Hello,
I'm currently working on a NextJS App using pages routing. It was written back in the 12.x days and included a middleware.ts file. We have found that the middleware.ts file causes issues with our redirecting when a internal page is not found.
Example:
The page for a link has been deleted in the backend.
Prefetch returns a 404.
Clicking on the link redirects to a blank page.
Refreshing the page redirects to 404 page.
After deleting middleware.ts:
Clicking on the link redirects to 404 page.
I can not find any set documentation on how a middleware file should handle this sort of issue. Is there any way to send the prefetch result or make sure the response is a 404 and is treated as such in v12? I've seen a few solutions in v13. with for example notFound() but this is not available in 12.x.
Our middlware.ts file is very simple:
function middleware(req: NextRequest) {
let response = NextResponse.next()
response = allowIFrame(req, response)
return response
}
The allowIFrame function simply checks a few things in the method and path, it returns a slightly modified response, but leaves status uneffected.
Console.log(response) right after the let response = ... line gives the following on the 404 page:
{
cookies: {},
url: '',
body: null,
bodyUsed: false,
headers: { x-middleware-next: '1' },
ok: true,
redirected: false,
status: 200,
statusText: '',
type: 'default'
}
status is obviously wrong.
Does this mean middleware.ts should perform a prefetch or check before returning status? or is there a way to integrate a response from next/link that would allow for the response to correctly result in a 404?
I'm currently working on a NextJS App using pages routing. It was written back in the 12.x days and included a middleware.ts file. We have found that the middleware.ts file causes issues with our redirecting when a internal page is not found.
Example:
The page for a link has been deleted in the backend.
Prefetch returns a 404.
Clicking on the link redirects to a blank page.
Refreshing the page redirects to 404 page.
After deleting middleware.ts:
Clicking on the link redirects to 404 page.
I can not find any set documentation on how a middleware file should handle this sort of issue. Is there any way to send the prefetch result or make sure the response is a 404 and is treated as such in v12? I've seen a few solutions in v13. with for example notFound() but this is not available in 12.x.
Our middlware.ts file is very simple:
function middleware(req: NextRequest) {
let response = NextResponse.next()
response = allowIFrame(req, response)
return response
}
The allowIFrame function simply checks a few things in the method and path, it returns a slightly modified response, but leaves status uneffected.
Console.log(response) right after the let response = ... line gives the following on the 404 page:
{
cookies: {},
url: '',
body: null,
bodyUsed: false,
headers: { x-middleware-next: '1' },
ok: true,
redirected: false,
status: 200,
statusText: '',
type: 'default'
}
status is obviously wrong.
Does this mean middleware.ts should perform a prefetch or check before returning status? or is there a way to integrate a response from next/link that would allow for the response to correctly result in a 404?
5 Replies
@joulev likely a bug of middleware. just upgrade to next 13, then next 14, the bug is likely already fixed.
you can use the pages router in next 13/14 too
MinskinOP
hey thanks for your response, this is unfortunately not a possibility at this point as an upgrade would force quite a few changes in the whole project.
@Minskin hey thanks for your response, this is unfortunately not a possibility at this point as an upgrade would force quite a few changes in the whole project.
There are very few breaking changes in the pages router when you upgrade to 13 and 14, iirc all of them can be done by automated codemods.
You do not have to migrate to the app router.
You do not have to migrate to the app router.
If you don’t upgrade, I don’t see any other way for you to fix a framework bug
@joulev There are very few breaking changes in the pages router when you upgrade to 13 and 14, iirc all of them can be done by automated codemods.
You do not have to migrate to the app router.
MinskinOP
I understand and there are plans for this migration in the future, it is not something doable in the nearest timeframe. Thanks for the encouragement though.