NextJS seems to have no way to reconfigure the base path (vercel/next.js#16059).
Some propose a reverse proxy, but because static pages embed the basePrefix into them this also doesn't work well.
The alternative is quite undesirable -- building at runtime.
Here, we use a hopefully unique string /__ENTRYPOINT__
as the entrypoint, this allows us to just replace this string
in all the next-built files at startup when we know the actual entrypoint.
Once building with this approach, the entrypoint can be customized, this also works for images, API endpoints, and more.
Tested on next@^12
NOTE: No longer works in next@^13
Complete repo for testing this: https://github.com/u8sand/test-nextjs-runtime-entrypoint-reconfig
The assumption here is that you're building your NextJS application with Docker, these files would be put in the root of your repo and you build with
docker build -t your-tag .
then you can run with arbitrary entrypoints i.e.
docker run -p 3000:3000 -e NEXT_PUBLIC_ENTRYPOINT=/myentrypoint -it your-tag
@castafab as noted on the post, this trick was tested on
[email protected]
. I just tested and it also works on[email protected]
but onnext@^13
, some change was introduced which mangles the link somehow. Interestingly the link is correct in the page href but gets mangled when next pushes it.It works for
<Link href="/somelocation">
but not for<Image src="/someimage.png">
but the later can be worked around by either importing the image and using the imported image or by using<Image src={`${process.env.NEXT_PUBLIC_ENTRYPOINT}/someimage.png`} />
.I don't have the bandwidth right now to figure out what next changed in
next@^13
-- if anyone else does and figures it out, do let us know. In the meantime, I updated the gist to note in bold that the trick doesn't work onnext@^13
.Also I created this repo: https://github.com/u8sand/test-nextjs-runtime-entrypoint-reconfig with the fix applied to a fresh
create-next-app