Back

Show HN: Self-Host Next.js in Production

84 points5 hoursgithub.com
Etheryte4 hours ago

I haven't really followed Next.js and the related ecosystem closely, what does this project do exactly? If I look at Next.js docs [0], they have three options for self-hosting: using a Node server, using a Docker container or as an SPA. Is this project a wrapper around one of those or is it something else entirely? Or to perhaps ask differently, to me it seems you can already self-host Next.js, what's the value add of this specific project?

[0] https://nextjs.org/docs/pages/building-your-application/depl...

grrowl4 hours ago

You can "self-host" as in `next build` and `next serve`, but it runs as a monolothic runtime. Next-on-vercel compiles each api route to a serverless function and supports various additional flavours of server side rendering of pages. Open Next is being able to run like Next-on-vercel but on AWS, your own infra, Cloudflare Workers, and others.

dvnguyen2 hours ago

Gotcha. That means if I run Next for just frontend + server side rendering stuff then I can always self host it?

presentation47 minutes ago

Sort of, so long as you don’t care about more complex hosting strategies eg hosting some/all of your server side code on edge servers when users visit your web app - as opposed to always sending them to a server in a set location in the world as a “normal” server architecture would typically do. Alongside not caring about prerendering and caching some pages and not others, that kind of thing

greyskull4 hours ago

In the company I just left, I actually went through the process two or so months ago of migrating their Vercel deployment to AWS. I evaluated several options that are listed on the website and on GitHub, and we landed on using OpenNext via SST, it was a low-pain effort, especially given the CTO's desire to also migrate off of Next.js.

As other commenters have touched on - my understanding is the purpose of OpenNext is to package the output artifacts of a Next build in a way that can be deployed to a serverless environment, analogous to how Vercel does it. The supporting projects like SST and the other links in the repo are to take those OpenNext artifacts and deploy them to infrastructure generally in an opinionated way - additionally supporting some of the "extra" features described in the repository.

The last project I was working on was to then migrate from SST to Fargate, as a persistent process (serverful?) deployment was preferable for various reasons. In that scenario, we would just be running the built in server using the Next.js standalone deployment mode (effectively a `node index.js`). We didn't need the extra functionality covered by OpenNext.

kcrwfrd_3 hours ago

What’s the CTO’s motivation for migrating off of Next.js? And to what?

pier252 hours ago

> especially given the CTO's desire to also migrate off of Next.js

To Remix?

taikon4 hours ago

The reason this project is important is because Next.js is hard to deploy if you're not using Vercel to host your app. You can easily run into issues that are hard to resolve. It's creates lock-in with Vercel and is one of the reasons why Vercel has a bad rep.

leerob4 hours ago

(I work on Next.js)

The OpenNext project serves two purposes. First, it was to document some of the quirks when self-hosting Next.js. As of Next.js 15, the majority of these have been fixed, thanks to their feedback. The second is to provide an adapter for serverless Next.js deployments, similar to Vercel. Their adapter takes the Next.js output and transforms it to work in a serverless environment.

We're working with the maintainers of OpenNext to patch up some of the remaining quirks for deploying Next.js serverless and exploring how to make the maintenance of these community adapters easier.

If you want to self-host Next.js on a server, like a VPS or Node.js server, all features of Next.js are supported[1] and you do not need to use OpenNext. Our self-hosting docs include 10 example repos for deploying to different providers and a video walking through all features and options of Next[2].

[1]: https://nextjs.org/docs/app/building-your-application/deploy... [2]: https://www.youtube.com/watch?v=sIVL4JMqRfc

inlined18 minutes ago

(I work on Firebase App Hosting, which also supports Next.js directly rather than OpenNext)

I want to +1 that Vercel has been really improving their portability. We still have Next.js specific code in Firebase, and having different architectures can sometimes make us have to figure out the best mapping of their features, but Vercel has made a noted effort to improve encapsulation of Vercel-specific features and portability has improved. I’m honestly surprised that OpenNext, which is not a new project, seems to have gained a lot of attention in the immediate past.

atlex23 hours ago

Hello! On this topic: Could you please look into making the path ‘next/image’ uses for caching images user definable? Currently I can’t use Google app engine standard because the directory it uses is write protected. The only real solution seems to be custom image providers, which is a drag, so I’m on App Engine Flex spending way more than I probably should :-)

leerob2 hours ago

The way I would recommend handling this is using the `loaderFile`: https://nextjs.org/docs/app/api-reference/components/image#l...

dawnerd1 hour ago

Confused as well since we self host in docker and it's no different than any other node app. People keep posting that there's gotchas and weird setups but... it just works well for us.

willio581 hour ago

Ultimately what you’re doing is scalable for most use cases. You can even horizontally scale that setup on the fly to mimicking what you would get from using next in a serverless environment.

jeremy_k4 hours ago

I believe the intent of OpenNext is to allow you to run your Next.js applications in a serverless manner, same as Vercel.

hysan3 hours ago

Not sure why this is posted as a Show HN. This project has been around for a long time and I don’t think OP is related in any way to it.

bilalq2 hours ago

This in interesting, but while OpenNext is agnostic about the hosting layer, it seems opinionated about the IaC layer. SST is great, but I still use CDK for everything and have no appetite to maintain a snowflake project with a different IaC tool than everything else.

I see that the docs do link to other projects built on top of OpenNext that support other tools, but they seem stagnant and stuck on old versions of OpenNext (which effectively means stuck on old versions of Next.js).

I get that the people behind the project have limited bandwidth, and if they're going to support a tool, they'd first and foremost support the one they use and maintain themselves, but still.

hi_hi3 hours ago

To clarify in the specific case of deploying to cloudflare workers, you can use cloudflares next-on-pages at the expense of a constrained "Edge" runtime.

- https://opennext.js.org/cloudflare#supported-nextjs-runtimes

You don't _need_ opennextjs to _not_ run on Vercel, but it does provide more flexibility than depending on Cloudflare alone (I don't know if there are any tradeoffs to consider as I haven't used it)

If you're new to this stack, I'd recommend taking some time with a toy app to get use to the architecture and frameworks, especially between running locally and deploying to a host. There are some differences that need to be aligned to get it working across all of them, especially when you introduce server actions.

slashnode3 hours ago

I've been surprised by the number of companies adopting nextjs as their defacto framework without concern for the uncomfortable reality that the framework is built to be hosted on vercel with self hosting largely supported as an afterthought. It's great that opennext exists, but it really shouldn't _have_ to exist (as I believe some of the maintainers have publicly stated)

jmathai3 hours ago

I’m uncomfortable using most JS frameworks. The churn is so high and something new comes out every couple of years.

At the risk of sounding like a grumpy old developer, I am currently using minimal JS, native when possible and jquery as needed. The UX is just fine. Some pages have interactive JS but most reload the page. I have a hard time imagining this isn’t an option for most web apps.

switz2 hours ago

It doesn't have to exist - I run next.js self hosted and it's arguably better than hosting on vercel (no cold starts, dedicated cpu, next to my api server/db).

paxys2 hours ago

I don't know why people keep saying this. There are some features of Next.js that are better supported on Vercel. That doesn't mean that you have to use Vercel. In fact if you are just building a static React site without any server components (so, 99% of sites created with Next.js) then it makes literally no difference where you deploy it.

e-clinton3 hours ago

Deploying next.js on containers, AWS Amplify, digital ocean, Netlify, etc is trivial. Such a strange statement to read.

nicce3 hours ago

It is all about the scale.

threetonesun3 hours ago

Next, as is, works fine self hosted? Certainly not everyone needs serverless functions split out like Vercel does.

I’m more surprised because it often feels like Next is slowly rediscovering the last 20 years of web frameworks in janky, somewhat broken steps.

willsmith722 hours ago

Remix is growing, and when react router 7 lets you add SSR into any react SPA, I would guess next's market share will slowly shrink in part for that reason

afro884 hours ago

This is great, but my first question is: what is the infrastructure deployed? Is this a cool resume filler, or is it a serious alternative to Vercel? Seeing something around the infra and security in the readme would be great for production grade hosting

greyskull3 hours ago

OpenNext is just for packaging the Next.js build artifacts. The infrastructure is defined by projects that deploy those artifacts, examples here: https://opennext.js.org/aws/get_started

Some of them are, for example, Terraform projects that list the specific infra. I have experience with the SST deployment, whose website unfortunately doesn't do a great job of listing the infra architecture.

theogravity4 hours ago

This is awesome! We run Next.js on our own server as we use node-specific APIs in various backend-y places of Next (meaning edge deployment is out) and something like this would definitely help us!

jonplackett4 hours ago

This is very cool.

Do you have any ideal the cost difference of running this VS NextJS for any particular workload?

I use NextJS on Vercel for so many projects but mostly smaller things since I know it can get very expensive after scaling up.

greyskull4 hours ago

The biggest cost for us on Vercel (several hundred dollars a month) was Image Optimization, and that was because the app was being majorly inefficient with images, in part due to some default behavior in Next.js that we found unfriendly [0], and in part due to negligence. That being said, it wasn't "cheap" by any means outside of that, still hundreds a month for something that I would not consider a high traffic application (I wish I could remember more specific numbers).

Migrating to OpenNext using SST, I think we got the bills for compute and asset serving down to like $15/day or something (granted, we spent expensive engineer time on the migration).

[0] https://nextjs.org/docs/app/api-reference/components/image#s...

syndicatedjelly3 hours ago

That’s insanely expensive for a low traffic web app. Why should anyone use Next.js, given a choice? Are the handful of milliseconds shaved off for the end user worth the cost?

rc_kas2 hours ago

Oooh! I need this. Thanks!

syndicatedjelly4 hours ago

What does this offer, that a Dockerized Next.js application running on ECS doesn’t? What are the downsides to using this? Does it stay up-to-date with Next.js, or is there slippage as the maintainers of this project keep up with new updates to Next.js?

greyskull3 hours ago

It offers packaging for deploying to a serverless environment (e.g. Lambda) analogous to how Vercel does it.

The last question is salient, and it's possible for OpenNext to break and have to catch up to changes in Next.js, though I believe there's some more direct collaboration. I'd say that's the biggest downside - it's not guaranteed compatibility.

I did a migration recently (comments elsewhere in this post), and I don't recall the specific issue, but I _do_ recall running into at least one scenario where OpenNext had made a decision that impacted - in a way that was visible to me and undesirable - how Next.js functioned. That's not a criticism, there's tradeoffs.

CharlieDigital3 hours ago

How would it compare to running as serverless containers (rather than ECS) like Google Cloud Run or Azure Container Apps (true scale to 0)?

It seems like using serverless containers would meet most of the same objectives so I'm not clear where the delineation is here.

Onavo5 hours ago

Any plans to add support for? https://github.com/awslabs/llrt

It would also be nice to have a V8/deno/bun based edge hosting option that supports the Next.js edge and middleware code splitting instead of using lambda@edge's node setup (which is slow and expensive). That's the missing piece for most homebrew next.js "edge" setups. Production CDNs like Clouflare and Supabase all offer this.

thdxr4 hours ago

llrt is extremely experimental and early - probably not the right fit

the latter is being worked on and there is a cloudflare adapter in that github org

Onavo4 hours ago

I am aware of the CloudFlare adapter. I am referring to when running as a standard AWS-only SST app.

revskill2 hours ago

Nextjs has a flaw archtecture fo the ease of starting a project. It will bite u in the long run.