Redirect preview environment root path when using `basePath`

Hey!

Already solved this a couple different ways on earlier versions of NextJS, but seems that this undocumented behavior still changes from time to time… And now more recently updating to 15.5.3 seems like it changed once again… The Vercel Support team also wasn’t able to find any workarounds for it.

We have some projects that has basePath configured. But on Vercel and Github the links to the Preview environment always go to the “root” path under that preview domain. So you ALWAYS get a 404 when you have a basePath configured.

I would like to redirect the accesses to the Preview environment root path, to the basePath. For example the link on Vercel/Github is https://something.preview.vercel.app/, and it should redirect to https://something.preview.vercel.app/my-custom-base-path.

In past versions I was already able to solve it using the middleware, which doesn’t seem to work anymore.

And also explored trying to catch that with some special next.config.js redirects, or some custom client side logic on the not-found.tsx.

Any other ideas on how to solve this?

I find it a stressful DX for the team, that sometimes is working on a project that they are not super familiar with, and get a 404 on the preview environment, and takes some time to realize that the basePath is missing on the URL. I would love to get them smartly directed to the correct path.

There’s another community post with 404 debugging tips that might be helpful. Please give these solutions a try and let us know how it goes.

A human should be around soon to offer more advice. But you can also get helpful information quickly by asking v0.

It seems like I was able to get this working using a vercel.json file, with a redirect configured there.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.