This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
middleware + pages routes + getInitialProps setting 404 = results in hard route #68511
Labels
bug
Issue was opened via the bug report template.
Middleware
Related to Next.js Middleware.
Pages Router
Related to Pages Router.
Link to the code that reproduces this issue
https://github.com/adanperez/nextjs-issue-pages-middlware-404
To Reproduce
Setup
This linked repo demonstrates an issue we are seeing with the following setup:
getInitialProps
Issue
/
->/soft
will do a soft route to the new page as expected./
->/dynamic/test
will do a hard route to the new page which is NOT expected.Current vs. Expected behavior
Current behavior
The following URL of
/_next/data/development/dynamic/test.json?slug=test
is routed to the pagesrc/pages/dynamic/[...slug].tsx
because of Next.js middleware.This unexpected URL does not match the expected path structure we look for in the
getInitialProps
function of oursrc/pages/dynamic/[...slug].tsx
page.We do not find the correct ID of
test
because of the extra path structure/_next/data/development
which gives us the result ofdata
instead.We return a
404
here since the result of our pretend DB lookup is not found when our ID is nottest
.The returning
404
from the/_next/data/development/dynamic/test.json?slug=test
request causes middleware/Next.js to hard route to this page instead of soft routing.Expected behavior
We expect a soft route instead of a hard route when returning a 404.
Thing to note
This 404 / hard route behavior does not happen with Next.js version 14.1.4
Provide environment information
Operating System: Platform: darwin Arch: arm64 Version: Darwin Kernel Version 23.5.0: Wed May 1 20:12:58 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6000 Available memory (MB): 32768 Available CPU cores: 10 Binaries: Node: 20.10.0 npm: 10.2.3 Yarn: N/A pnpm: 9.4.0 Relevant Packages: next: 14.2.5 // Latest available version is detected (14.2.5). eslint-config-next: 14.2.5 react: 18.3.1 react-dom: 18.3.1 typescript: 5.5.4 Next.js Config: output: N/A
Which area(s) are affected? (Select all that apply)
Middleware, Pages Router
Which stage(s) are affected? (Select all that apply)
next dev (local), Other (Deployed)
Additional context
This 404 / hard route behavior does not happen with Next.js version 14.1.4.
The text was updated successfully, but these errors were encountered: