New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Middleware NextResponse.rewrite
404ing when rewriting "/" with another route when deployed to Vercel
#33044
Comments
Changing |
Judging from the error in the console, it might be a duplicate of #32549 |
@balazsorban44 seems similar. Here is my updated middleware:
Essentially, I am trying to perform an AB test using the original home route ("/") and a variant ("/variant-home-refresh"). I am able to see the variant if I navigate to the route directly, however any time the original route is rewritten, the home page 404s and any links to "/" do not work throughout the site (logo, etc.). I definitely think this could be related to i18n. |
Just to follow up. Updated middleware for those who come by this:
|
NextResponse.rewrite
404ing when rewriting "/" with another route
NextResponse.rewrite
404ing when rewriting "/" with another routeNextResponse.rewrite
404ing when rewriting "/" with another route when deployed to Vercel
Could you add a deployed minimal reproduction @wadehammes? 🙏 And to clarify, this is only an issue when deployed, but works locally? |
I don't think I'll have time to do that any time soon but will try. |
Yes works locally, not deployed. Stack is latest Next, i18n configured with two locales, Contentful CMS. Redirect works like a charm, rewrite causes 404. |
I am facing the same issue. Redirect works, and rewrite causes a 404. The issue happens only when deployed to vercel. |
This comment has been minimized.
This comment has been minimized.
not sure if this is related by we were experiencing rewrites failing to do anything when navigating client side using next/link - upgraded next.js to 12.0.10 and converted our rewrite logic to middleware and still seeing the same issue. client side navigation does not rewrite correctly, whereas refreshing the page will trigger the rewrite - in both cases the middleware is triggered and |
I had the same issue, 404 pages only when deploying on Vercel and the problem was that i was not passing the basePath to the import { NextRequest, NextResponse } from 'next/server'
export default function middleware(req: NextRequest) {
const { pathname, basePath } = req.nextUrl
const hostname = req.headers.get('host')
// does not work
// `/_hosts/${hostname}${pathname}`
return NextResponse.rewrite(
new URL(
`${basePath}/_hosts/${hostname}${pathname}`,
req.nextUrl.origin,
).toString(),
)
} The same problem probably exists when using i18n, you need to pass the base paths manually PS: i made a full repo with an example of this working here |
This didn't work for me unfortunately. Still getting a 404 using the approach above. |
@wadehammes you have to add the locale to the path like this return NextResponse.rewrite(
new URL(
`${basePath}/${locale}/_hosts/${hostname}${pathname}`,
req.nextUrl.origin,
).toString(),
) You can skip the basePath if you are not using it I made a working example here (locales branch) |
I had same 404 issue and it was because I did not put locale in the pathname as @remorses mentioned. For example,
It was pretty tricky to find this solution. Probably better to mention this in documentation at least. @steven-tey |
Can confirm this works, thanks @youminkim:
|
Hey guys! 👋 Got the same issue. Middleware works locally but I keep getting 404 for the rewritten pages once the app is deployed to vercel. I managed to create a repo for the issue that can be found here: Also the mini-app has been deployed here: If you have any questions regarding the repo - feel free to reach out! Hope this helps 😁 |
Not working for me unfortunately |
We are having the same issue for a site we first published using docker containers. We redirect this slug to '/collection/[...slug]' as we want to use the URL's existing in our previous setup.
|
Hi! We've been working a lot on improving the dev experience and the API for middleware and I've been testing this example with the most recent canary that was just published to make sure it doesn't happen anymore. There seems to be a few duplicates of this issue so I'm going to also share some context that could be useful in those. First of all, note that Middleware is designed to provide the maximum freedom to users so there are some things that we can't make implicit like, for example, preserving the original locale since we don't know if the user is trying to remove the locale or keep it. This plus the fact that we require absolute URLs for when Middleware is invoked implies that the user must express a rewrite always using a full URL. To make things more convenient we are providing within From the OPs code I understand the intention is to preserve the locale so import { NextRequest, NextResponse } from "next/server";
const CONSTANTS = {
RH_HOME_PAGE_TEST_COOKIE: 'ab-test-cookie',
RH_HOME_PAGE_VARIANT_1_ROUTE: 'index-a',
RH_HOME_PAGE_ORIGINAL_ROUTE: 'index'
}
export function middleware(req: NextRequest) {
// Home page AB testing
if (req.nextUrl.pathname === "/") {
let variant = req.cookies.get(CONSTANTS.RH_HOME_PAGE_TEST_COOKIE);
// Determine the variant via a random split
if (!variant) {
variant = Math.random() >= 0.5 // 50/50 chance
? CONSTANTS.RH_HOME_PAGE_VARIANT_1_ROUTE
: CONSTANTS.RH_HOME_PAGE_ORIGINAL_ROUTE;
}
// If variant is not original, redefine route to variant
req.nextUrl.pathname = variant === CONSTANTS.RH_HOME_PAGE_VARIANT_1_ROUTE
? `/${CONSTANTS.RH_HOME_PAGE_VARIANT_1_ROUTE}`
: "/"
const res = NextResponse.rewrite(req.nextUrl);
res.cookies.set(CONSTANTS.RH_HOME_PAGE_TEST_COOKIE, variant, {
expires: new Date(Date.now() + 3600 * 365 * 24 * 1000)
})
return res;
}
} Note that the rewrite target will be I've tested this both locally and in production and it works good. Tried out:
Everything worked fine in Vercel, Server and Dev modes. |
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Run
next info
(available from version 12.0.8 and up)What version of Next.js are you using?
12.0.8-canary.18
What version of Node.js are you using?
14.18.0
What browser are you using?
Firefox
What operating system are you using?
Windows
How are you deploying your application?
Vercel
Describe the Bug
Using middleware, NextResponse.rewrite is not correctly rewriting a route with another existing route. Doing so results in a 404.
I am using i18n, but #31174 does not fix it.
Seems related to #30749
Here is my middleware file:
My next.config:
Expected Behavior
The rewrite should happen as it does locally on Vercel and not 404.
The text was updated successfully, but these errors were encountered: