11 ms·
Static as a Server
- ktpsns 1y agoThis got popular in JS toolkits a few years ago, at least. For instance, Svelte(kit) also has a static output variant.
- switz 1y agoIt's unfortunate that there is so much misinformation about what react server components really are, but it's not necessarily the fault of either party. The name is confusing (names are hard), the architecture is new (people don't want to learn it), and it lends itself to conspiracy theories (that aren't true). But it really is a magnificent piece of technology. Because they're called "Server Components" people think that "server" means run-time, but as a friend pointed out, 15 years ago people were running wordpress servers and caching pages ahead-of-time. As Dan mentions here: "server" doesn't imply it has to execute at run-time. But there are also massive advantages to running a server at run-time that seem lost on people. I do think over time the concepts behind RSCs will filter out into most web frameworks because they are so powerful. It's the best functionality of the old-world SSR languages (PHP, rails) combined with the best functionality of the new-world client frameworks (React). You get to pick and choose when to lean on either, and they work together through composition. I wish people were a bit more patient and spent a bit more time trying to understand these concepts before bashing them. Part of that is the fault of names, communication, and lack of documentation. But the underlying technology is rigid and strong. It's here to stay, even if it arrives in other forms.
- danabramov 1y agoI think it’s fair to say that a lot of the negative reception was also due to 1) No easy way to try outside a framework (now there is with Parcel RSC, but eventual first-class support in Vite will likely be the real tipping point). 2) Poor developer experience in the only official integration (Next.js). Both due to build performance problems (which Turbopack helps with but it still hasn’t fully shipped), due to bad error messages (massively improved recently), and due to confusing overly aggressive caching (being reworked to much more intuitive now but the rework won’t ship for some time yet). Time will tell but I’m optimistic that as there are more places to try them, and the integrations are higher-quality, people will see their strong sides too.
- mikesurowiec 1y agoThanks for pointing out Parcel RSC. I just read through the docs and they do a great job of explaining RSCs from a place I can understand. In contrast to NextJS where it’s unclear where the framework stops https://parceljs.org/recipes/rsc/ https://parceljs.org/recipes/rsc/
- steve_adams_86 1y ago"unclear where the framework stops" is a great way to phrase that issue. It's something I've run into with NextJS in a few contexts. I really appreciate when frameworks leverage standards and/or indicate the boundaries of the framework as much as possible.
- agos 1y agoI'll add to the confusing caching: Next deciding to monkey patch fetch() to add their caching, and Next relying on a React Canary version left in some (surely in me) a taste that RSC were half baked, which is weird since they've been around for like, five years?
- nicce 1y agoI think this was pretty good at explaining the impact: https://www.youtube.com/watch?v=zqhE-CepH2g https://www.youtube.com/watch?v=zqhE-CepH2g Convinced me to start new project with React just because of the RSCs.
- danabramov 1y agoThanks for reminding, that’s a great talk!
- Vinnl 1y ago> It's the best functionality of the old-world SSR languages (PHP, rails) combined with the best functionality of the new-world client frameworks (React). And one of my pet peeves is people seeing the former and dismissing it as "everything old is new again" without considering the gains we've made in the meantime with the latter.
- skydhash 1y agoI remember meteor.js!
- owebmaster 1y ago> I do think over time the concepts behind RSCs will filter out into most web frameworks because they are so powerful I don't think that is true. React had one great feature that gives it its name: reactivity. People kept using it despite the bad abstractions added later like hooks, graphql and now RSC. The difference is that now react fatigue is way bigger than the hype, nobody loses a job by stating that RSC is terrible.
- turtlebits 1y agoToo many devs rely on a single tool to try to do everything, at the cost of complexity. The amount of knowledge you need to use any Javascript framework is IMO, insane, let alone try to keep up with the latest and greatest. The enshittification of the web partly is shipping giant JS bundles for no reason. (Developer needs > Consumer)
- draw_down 1y ago[dead]
- atoko 1y agoI wonder if the omission of React Context in this example is intentional. Do you think Context is compatible with suspense? In the sense that posts is being passed to components as props three times over. Is it because each component is expected to abstract over async, relying on the promise state?
- danabramov 1y agoNot sure what you mean — we’re just reading some files from the disk and passing that down. It doesn’t need Suspense because everything is static (so no loading indicators are needed). If this was dynamic then I’d probably still not add a loading indicator because you expect a blog index to “pop in” with the rest of the page rather than behind a spinner.
- atoko 1y agoThanks! More concisely: it’s not always the case that prop drilling is possible within a component hierarchy. In a more involved application you store this object in context. Is what you are describing compatible with this pattern? How does this inform the design of RSCs and as a developer, how can I expect this to affect me?
- danabramov 1y agoThis depends on how you need to use this object. One way would be to put it into Client context near the top, then it will be available in Client context below. So if it’s just for the Client stuff, your existing approach works. For data fetching, a more common approach is not to do any “passing down” at all. Instead, have components that need that data below read that data independently. Slap React.cache around the function that gets the data so that all calls are reused across a single request. Then the first call kicks it off and the other calls will wait for the same Promise under the hood. And you can kick off preloading somewhere up above in the tree if you want to eagerly begin. That should cover most cases.
- deleted 1y ago
- revskill 1y agoIf you put a cache around all GET API handler, you're faster than static.
- NewEntryHN 1y agoMost servers will natively use caching policies for serving static content as well.
- danabramov 1y agoPoint taken, but I more or less consider this “static”. The distinction is artificial (of course there’s always a server behind the scenes), the question is just which server you’re programming against in the abstraction stack of the majority of your program. Hybrid approach often includes “incremental regeneration”, i.e. partial builds on-demand with caching. So that fits very well there. But it’s an implementation detail. The programming model you’re writing against can just use an “invalidate” function without concerning itself with the outer “actual server” layer.
- raddan 1y agoI understand that somebody might want to generate static pages from code that generates it dynamically, but I fail to appreciate _why_. Are people using this for a handful of pages they want to load quickly and whose contents rarely change, or are people building entire static sites using things like React? If it's the latter... uh... why? It's been awhile since I was a web developer, so maybe my pain threshold is inappropriately low. I think Jekyll is fine and use it pretty regularly.
- draw_down 1y ago[dead]
- switz 1y agoIt's because inevitably there comes a time where you want some pages (or even sub-pages) to be static, and other pages (or parts) of your application to be dynamic. If the question is "why" the inverse is "why not"? Dan's blog is rendered as static, works without javascript and still lets him write complex client-side components when he calls for it. And if one day he decides to add a page that renders content dynamically, he can do so in the same patterns and framework that he's already using. The goal is developer-choice. No need to pick static or dynamic holistically, have access to both when you need them. No need to pick between hydrating the entire website vs. rendering exclusively on the server. No need to pick between writing client-side amenable code or server-only code. How many platforms have a "marketing" website for / and a platform website for /dashboard? No need to split them, just use a framework that accommodates both seamlessly. It's more powerful, even though it does come with a learning curve.
- danabramov 1y agoTo give a concrete example, I’ll probably add some dynamic stuff at some point in the future, like commenting with Bluesky or such. It’s nice not to switch tools for that.
- dakiol 1y agoI think the reason to split the marketing page from the dashboard one is that you can deploy one without the other. I would actually prefer to have all the marketing stuff in its own repo away from any dashboard code.
- throwaway314155 1y agoI am having trouble understanding this article's premise: ``` RSC means React Server Components. And yet, although this blog is built with RSC, it is statically served from a Cloudflare CDN using their free static hosting plan. It costs me exactly zero. Zero. How is this possible? Aren’t these React Server Components? ``` Why is any of that confusing? The very first thing I think of when someone says "React Server Components" is, well, server side rendering of react components. What else could it possibly be? Is anyone who is an established React developer really confused by this?
- roywiggins 1y agothis post describes not really running RSC on the server, but instead running it on the developer's laptop, taking the resulting HTML, and pushing that to a server. the more common use case of RSC is where it is actually being run server-side as requests come in, perhaps behind a cache, not baking everything down into a static bundle once
- danabramov 1y agoIf you think of “server side rendering”, you might be assuming that it requires runtime Node.js hosting (which is usually paid). But that’s not the case — my blog is statically generated during deployment. Like Jekyll. So the point of the post is to show why it’s not a contradiction. In modern tools, “static” is often an output mode of “server” frameworks rather than a separate category of tools.
- owebmaster 1y ago> If you think of “server side rendering”, you might be assuming that it requires runtime Node.js hosting (which is usually paid) Because that is what SSR mean. Not to be confused with SSG which is the case for your blog.
- danabramov 1y agoSure. I’m just trying to answer the parent’s question? I wasn’t the one to bring “SSR” into the conversation.
- snambi 1y ago[flagged]
- danabramov 1y agoI’ve tried to make a very specific point about static shifting from a category of tools to an output mode of server tools (running server request/response model ahead of time). What’s the clickbait there?
- schwartzworld 1y agoDon’t engage with trolls, dude.
- ricardobeat 1y ago[flagged]
- deleted 1y ago[deleted]
- omnimus 1y agoIt’s not like this is not used anymore. Majority of CMSes that are actually good are written in PHP. And every single one of them will have various html caching drivers. From simple file caches, to redis, varnish or outside cdns like cloudflare.
- rco8786 1y agoOne more reason to be confused about nextjs
- danabramov 1y agoAre you confused about Astro? It follows the same model.
- kenanfyi 1y agoGood that you say that. I believe the model is more important here than the implementation detail, framework or language of choice. Plus, I think Astro wins over Next in terms of documentation and the general developer experience. Next has the bad vibes, unfortunately because of all Vercel thing. At least for me. I believe if Next would position itself somewhere like Astro, people would be more open to learn and dig it. I don‘t exactly know if it‘s 100% true, but I feel like if I invest my time in Next, I will be locked-in by the Vercel world of things. And no one likes that.
- danabramov 1y agoWell, maybe “the same” is an exaggeration because Astro islands don’t nest very well. That’s what RSC fixes. RSC isn’t tied to Next, and Next isn’t tied to Vercel. I hear you about the vibes but thankfully that’s not my problem. I’m just trying to explain the model.
- deleted 1y ago[deleted]
- exogen 1y agoThis is also how I build most of my static sites, usually deployed to GitHub Pages. For example, here’s the demo page for a library I recently developed: https://exogen.github.io/turbo-colormap/ https://exogen.github.io/turbo-colormap/ Or a more complicated app: https://exogen.github.io/t2-model-skinner/ https://exogen.github.io/t2-model-skinner/ Are all of Next.js’ features overkill for such sites? Sure, but the convenience such frameworks provide is worth it. And the reason to prefer it over something like Vite is simply routing, which Vite doesn’t cover out of the box, so as soon as I want to add a second page, I now have another problem to solve. Next.js’ best feature is simply that you’re up and running with `npm i react react-dom next` and step two is just writing the pages.
- _benton 1y agoI'm curious if Next has full support for Cloudflare or if this is the only way to run Next w/RSC off of Vercel.
- igorbark 1y agoI too have had to use Next but didn't want to feel locked into Vercel. this is the biggest effort I'm aware to run Next with a full-ish feature set outside of Vercel: https://opennext.js.org/ https://opennext.js.org/. Supports AWS, Cloudflare, and Netlify. You can also run Next as a normal node webserver. I've only used the Cloudflare integration and it was a bit janky but worked (and seems to be entering 1.0 soon so may be less janky). AFAIK this is completely unsupported by the Next team, but would love to be proven wrong!
- leerob 1y ago(I'm on the Next.js team) We're working with Cloudflare on an official adapter. https://github.com/vercel/next.js/discussions/77740 https://github.com/vercel/next.js/discussions/77740
- igorbark 1y agothat's great to hear, thank you for the update :)
- seabrookmx 1y agoNext works great as a vanilla node app. What features are you missing from Vercel's platform out of curiosity?
- igorbark 1y agothe biggest headache i had in particular was different ways of handling environment variables, but the different adapters at OpenNext have had a rolling list of caveats/unsupported features for as long as i've been following the project so i didn't want to outright say "full". hopefully the effort on Next's side to build a standardized adapter API will help with this!
- zeropointsh 1y agoMy exact thoughts[1] when it comes to hosting, things should be static, although I seek joy in Astro and Markdown. [1]https://zeropoint.sh/blog/astro-as-static/ https://zeropoint.sh/blog/astro-as-static/
- exiguus 1y agoThink about product stock that change or you have a website with a cms and 100 Editors that push changes every 5min. If you have 5k pages. Then its likely, that builds take longer then 5min. And if you want to have a up-to-date website, you have to build it every 5min. This is where ISR and SSR comes into play, to update the pre-rendering on the server.
- nicbou 1y agoMy website has about 700 pages. A full rebuild takes around 10 seconds.
- perilunar 1y agoWhy would you rebuild the site every time an editor updates a page? Just update the page that was edited. Even better, don't pre-render anything until the first time it's requested, then render, serve, and cache it.
- exiguus 1y agoThat's a good suggestion, and it works well if you know which sites have changed before building. > Even better, don't pre-render anything until the first time it's requested, then render, serve, and cache it. This is essentially what Incremental Static Regeneration (ISR) does. But, mostly you pre-render it, because else, the first user has to wait to long for the page. ISR is also re-rendering for the next user. The current user has the old version and the page is re-rendering in the background for the next user. The JAMstack architecture is advantageous because it allows for the use of simple servers—essentially S3 buckets that can serve static files. This setup requires less security patching and reduces costs for server hardware. Additionally, the server essentially acts as your cache, allowing you to distribute it widely.
- nhumrich 1y agoThis model already has a name: pre-rendered.
- exiguus 1y agoNo, you can not pre-render a shop site that needs product stock for UI updates. For this business requirement the classic JAMStack approach does not work. You need ISR or SSR. And you can combine pre-render with ISR for example.
- deleted 1y ago[deleted]
- turtlebits 1y agoI dont get it. Yes it's a static site, but it's only text and you're sending 100kb+ of JS over the wire. Is there any reason why you need React?
- igorbark 1y agoyou don't need to send 100kb+ of JS over the wire to build a static site in react: for example https://vike.dev https://vike.dev supports static HTML-only output for a site built with React. as for "why React", speaking just for myself it's really nice to just have one tool that can do everything (static HTML-only, static with JS, SPA, SSR) and not have to context switch or potentially even have to split my site into two projects just because I want to hop between one or the other approach. and React has the biggest mindshare and ecosystem.
- owebmaster 1y ago> you don't need to send 100kb+ of JS over the wire to build a static site in react It is telling that the blog of the fw creator ships 500kb of JS/CSS/HTML to display text on a screen. > it's really nice to just have one tool that can do everything (static HTML-only, static with JS, SPA, SSR) WebComponents (+lit-html)
- perilunar 1y agoYep. 411 KB of JS (126 KB minified). 525 KB of fonts! 40KB of HTML. 1.1MB (704 KB minified) total for approximately 4.3 KB of actual text. That's a crap:content ratio of 256:1
- azemetre 1y agoWow this is a great metric to use.
- danabramov 1y agoDisable JS and see that my site loads just fine. Fonts are nice-to-have. Interactive examples (on other pages) are nice-to-have. None of this is render-blocking.
- exiguus 1y agoBasically this started with vercel / next.js. And now react support server components itself. Its nice, if you have business requirements that you can't handle with the classic JAMStack approach. For example a shop, where the stock of products updates the UI (e.g. there is a sold-out label and the add to cart button should be disabled if out of stock). Lets say we use next. After a code update or with a cron job, you build the site every 6h. 3k pages (every product, categories etc.). Then, you use server componts and ISR with a ttl of 10min. ISR 10min means, the page get rebuild on the server after 10min with the next request and button gets disabled if the product is out of stock. Yes, you don't need this, and you can directly call an API, but, thats bad for the user, because they have to wait for the API request (and UI update) and bad for you, because you have as many API request, you have as page visits. So, server components bring value with the ttl / revalidate option of the page. A framework like next/vercel brings value if you want to maintain you page over the next 10 years with more then 1 developer.
- girvo 1y ago> This builds on an insight that seems obvious in retrospect: you can take any “server” framework and get a “static” site out of it by running its “server” during the build and hitting it with a request for every page you want to generate, and then storing the responses on disk I'm showing my age, but we used to do this using Wordpress ages ago. Like 15 years ago or more haha.
- sally_glance 1y agoWe did, but we were not able to run our PHP in the browser or prerender our jQuery on the server.
- girvo 1y ago> pretender our jQuery on the server. That came later, but yes we could (we did some wild stuff with v8js in PHP). And I'm convinced that anything in the blog requires either feature, though I get that more complex systems might.
- sally_glance 1y agoYeah, I remember all the fun that I had with PhantomJS & friends... My point was that isomorphic/universal/whatever it's called now JS and declarative component frameworks do have some advantages over our WP setups from 15 years ago. Agreed that you should still choose the right tool for the right job though.
- Loic 1y agoWith Moveable Type we even generated .php files with prerendered content in it from the database and using the PHP part of it to add server side dynamic functionalities. This was a very nice balance between a static and a fully dynamic website.
- danabramov 1y agoI didn’t mean to imply this is something new per se. But many people today are unaware of this technique. It is seeing some resurgence but not as much as I would have liked. Hence the article.
- lukeyoo 1y ago[dead]
- KronisLV 1y ago> First, the “hybrid” approach reduces tooling fragmentation—why have two ecosystems when the overlap is so large? The difference is just when the code runs. Because you end up with two subsets of supported features in the same technology, even in the linked page: https://nextjs.org/docs/pages/guides/static-exports https://nextjs.org/docs/pages/guides/static-exports Supported Features The majority of core Next.js features needed to build a static site are supported, including: Dynamic Routes when using getStaticPaths Prefetching with next/link Preloading JavaScript Dynamic Imports Any styling options (e.g. CSS Modules, styled-jsx) Client-side data fetching getStaticProps getStaticPaths Unsupported Features Features that require a Node.js server, or dynamic logic that cannot be computed during the build process, are not supported: Internationalized Routing API Routes Rewrites Redirects Headers Middleware Incremental Static Regeneration Image Optimization with the default loader Draft Mode getStaticPaths with fallback: true getStaticPaths with fallback: 'blocking' getServerSideProps To me, this feels about as awkward as React moving from class based components to function and hook based ones, dragging a lot of the large libraries alongside with it, meaning that if you tried using class based ones for a while you'd run into cases where some of your dependencies would only offer documentation for the functional approach. Mixing approaches that differ so much inevitably leads to awkwardness. I'd prefer a SPA library/framework without awkward sharp edges and a separate SSR solution, each specialized at what they do, neither trying to do everything. That way the toolchains remain simpler, there's fewer abstractions at play and fewer things to keep in mind. But if one tool to rule them all works for you, then great!
- hombre_fatal 1y ago(God why is this 8 paragraphs. This is what you get for being my first HN post of the day with a cuppa fresh coffee in hand. Shame on you for being the first post I saw while firmly rocking on my hobby horse.) Dunno, the Next.js hybrid approach seems both trivial to understand the trade-offs of, but also optimal: You get to use the same tooling, but if you don't use any of the dynamic stuff like the router (stuff you'd obviously need to use a server for), then you get to output static html files. Or, you can start with an SSG site and then decide that you want server-side logic, and all you do is opt-in to some server-side logic. This works well because instead of traditional SSGs like Jekyll, Next.js has the nice UX/DX of writing a dynamic server where it has a build step that crawls all your routes to generate static files (the right way to do SSG imo). So when you decide you want to run on a server, you just deploy it on the server instead of using the crawl step. Framing this into a confusing subset vs superset distinction kinda obscures what's really going on. It's like writing an Express app but opting in to SSG by using a build tool that wget-crawls your local server. The downside of Next.js is that client-side "rehydration" is inherently complex, not that it can enumerate your routes to generate static files. It's just that when you build a system that can render server html templates to strings and then rehydrate them in the browser, it's trivial to build the tool that saves those server templates into static html files. And I think you're imagining that you could only do such a thing if you designed for it from the beginning, essentially bending over your abstraction to force that feature, when it's actually a trivial entailment of the system in the first place.
- joshcsimmons 1y agoReact is a technique not a technology.
- owebmaster 1y agoYou guys gotta understand that Next.js is killing React. Just read reddit and HN, nobody asked for those features - we know it is being pushed just to make money, but a library/framework should not be guided by VC objectives.