Show HN: Self-Host Next.js in Production

95 pointsposted 2 days ago
by vednig

56 Comments

Etheryte

2 days 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...

grrowl

2 days 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.

dvnguyen

2 days ago

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

presentation

2 days 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

taikon

2 days 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.

leerob

2 days 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

atlex2

2 days 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 :-)

inlined

2 days 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.

greyskull

2 days 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_

2 days ago

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

greyskull

2 days ago

Didn't get far enough along to understand the motivations and considered alternatives.

mdhb

2 days ago

Next is actively a bad stack run by an incredibly shady company would be a good start

kcrwfrd_

a day ago

Could you substantiate that?

arez

2 days ago

bad stack in what way? Why is vercel shady? I can understand that the business model is questionable to lock-in people with developing a framework that runs best on their own cloud, but shady would mean fot me, that they do something illegal

pier25

2 days ago

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

To Remix?

greyskull

2 days ago

Didn't get far enough along to understand the motivations and considered alternatives.

user

2 days ago

[deleted]

dawnerd

2 days 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.

willio58

2 days 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_k

2 days ago

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

hysan

2 days 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.

slashnode

2 days 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)

jmathai

2 days 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.

anonzzzies

2 days ago

It is fine to sound like a grumpy old developer. We have clients using nextjs and it might have its place if you have apps in active development with dedicated teams, however for LoB apps for instance that don't change a lot and/or don't have continued dev or a dedicated team over time, it's a bad choice imho. Personally, I consider it a terrible choice always, because of the churn mostly; the current info in SO and LLMs is often wrong because of that churn. Best practices change and you have to pin everything or things auto-break. We get hired for a lot of $ to fix that in emergence settings, but I rather would have that people not use this stuff at all. It's not even a good dev experience imho, but I guess that's personal as either others are resume-devving and lying or just have another idea about what is a nice experience.

switz

2 days 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).

dbbk

2 days ago

I also self-host but it must be said that "no cold starts" and "next to my api server/db" are also supported by Vercel so that's not really a benefit

paxys

2 days 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-clinton

2 days ago

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

nicce

2 days ago

It is all about the scale.

anonzzzies

2 days ago

... which no-one (rounding error in % of companies) needs.

threetonesun

2 days 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.

anonzzzies

2 days ago

> somewhat broken

bit of an understatement there

willsmith72

2 days 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

hi_hi

2 days 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.

dbbk

2 days ago

The very link you posted says you can use the Node runtime, you're not limited to Edge runtime.

bilalq

2 days 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.

afro88

2 days 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

greyskull

2 days 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.

jonplackett

2 days 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.

greyskull

2 days 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...

dbbk

2 days ago

There's pretty much no reason to use Vercel's image optimization, just spend 30 minutes setting up Cloudflare Images and call it a day

syndicatedjelly

2 days 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?

jonplackett

2 days ago

People (me at least) us NextJS for the developer experience. It really is quite good. If you mean why use Vercel, again - great developer experience. Just expensive.

greyskull

2 days ago

1) I don't think it's related to Next, per se, but there may be behavior I didn't build the expertise to comment on. I also know that there were major inefficiencies in the application, so, for example, our P90 latency was (imo) terrible.

2) We'd have to define what constitutes low traffic vs any other arbitrary measure, so it's moot to discuss like this; all I said it wasn't high traffic. You could run it for cheaper, but there wasn't much expertise for self-hosting, for example.

3) For all I remember it may have been half that in daily cost. In any case, miniscule compared to engineer time. What was worse was the prior decision to use serverless aurora rds, that dwarfed everything else in AWS cost - I know this is tangentially related, just saying optimizing that a bit more was not the highest priority, we could do it for cheaper.

theogravity

2 days 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!

rc_kas

2 days ago

Oooh! I need this. Thanks!

syndicatedjelly

2 days 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?

greyskull

2 days 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.

CharlieDigital

2 days 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.

greyskull

2 days ago

OpenNext does model [0] incremental static regeneration, but beyond that I actually don't know, or at least don't recall. OpenNext doesn't do per-route lambdas like vercel does, so it's not like you get any behavior differences there.

I _think_ you can get scale to zero on Lambda by deploying a docker container, too.

[0] https://opennext.js.org/aws/v2/advanced/architecture

Onavo

2 days 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.

thdxr

2 days 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

Onavo

2 days ago

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

revskill

2 days ago

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