News for hackers. Stuff that matters.
Re:Next.js 11 by vincentmarle on Tuesday June 15, 2021 @01:23PM (#27518520)
I've only very recently heard of Next.js, what is the ideal use case for it?
Re:Next.js 11 by gherkinnn on Tuesday June 15, 2021 @02:08PM (#27519222)
As Next ambiguously puts it: "The React Framework for Production" [0]

It's excellent for building page-based sites and apps and comes with lots of nifty features that just seem to work. And, the most part, it's just out of your way.

Regarding the ideal use, I'd say any project that benefits greatly from (partially) pre-rendered content and statically generated pages is best served by Next and its data fetching [1]. I can't think of a FE-based solution that is anywhere near as good. It's also the only sane way to work with the insanity that is JAM stack [2].

Whatever you do, it's a safe bet.

0 - https://nextjs.org

1 - https://nextjs.org/docs/basic-features/data-fetching

2 - https://jamstack.org

Re:Next.js 11 by blowski on Tuesday June 15, 2021 @02:37PM (#27519632)
Worth adding that Next is to React what Nuxt is to Vue, though if you don’t know what Nuxt is, that won’t help.
Re:Next.js 11 by gherkinnn on Tuesday June 15, 2021 @02:50PM (#27519814)
And what Svelte Kit is to Svelte. ;)
Re:Next.js 11 by anichhangani on Tuesday June 15, 2021 @02:24PM (#27519473)
sounds similar to aspnet razor pages. is it?
Re:Next.js 11 by RobertKerans on Tuesday June 15, 2021 @04:44PM (#27521252)
Looking at the razor pages overview & that seems a very reasonable comparison (now you mention that, see PHP, in the form of "just chuck a few files on a server that understands them and off you go, dynamic website")
Re:Next.js 11 by steve_adams_86 on Tuesday June 15, 2021 @01:59PM (#27519063)
It's a fairly guard-railed, easy to deploy, React-based static site generator with dynamic content capabilities as well. It's generally great software. My team uses it for https://instantdomainsearch.com and we get good performance and developer experience overall.

To me it feels like the React "happy path" at the moment if you need a static or partially dynamic site/web application. I suppose the ideal use case is essentially any site you want to serve statically but maintain with React. This could be a blog, graphic design portfolio, or even a small store. It makes it easy for us to embed our React-based search application inside of a site with static content beside it that's easy to maintain and create.

Yeah, we could do this without next, but it provides some convention and well-made tools that generally make the job easier.

Re:Next.js 11 by vincentmarle on Tuesday June 15, 2021 @02:29PM (#27519534)
Cool, I'm a big fan of instantdomainsearch (it's my goto domain search tool) and had no idea how you can lookup the domains that fast. Is it fair to say that Next.js is a big part of that performance?
Re:Next.js 11 by steve_adams_86 on Tuesday June 15, 2021 @05:46PM (#27521923)
Next.js isn't a significant part of the run time performance of actual searching – that's fast because of a lot of custom infrastructure, data pre-processing, and client side code.

Next mostly makes it so pulling the site down and loading the page is snappier without us having to try particularly hard to get it that way. It isn't totally effortless - we've got some tailored pieces in our pipeline, but Next "off the shelf" does an excellent job already, and the work they've done makes customizations easy to implement and maintain.

I'm glad to hear you like it! Seriously, it makes it way more interesting knowing people are enjoying it. Every little tweak and improvement to performance is worth it when people are getting something out of it.

Re:Next.js 11 by bradhilton on Tuesday June 15, 2021 @06:20PM (#27522296)
I also really like instantdomainsearch.com. I don't have any specific feedback, but thank you for your team's work!
Re:Next.js 11 by steve_adams_86 on Tuesday June 15, 2021 @06:45PM (#27522569)
You’re welcome!
Re:Next.js 11 by notjustanymike on Tuesday June 15, 2021 @02:01PM (#27519106)
I've only ever used Gatsby with this, how does it compare?
Re:Next.js 11 by steve_adams_86 on Tuesday June 15, 2021 @02:14PM (#27519318)
Good question.

Our migration was fairly direct. We did it in around a week with some cleanup in the following week. There were analogous conventions throughout just about the entire migration, so they're very similar.

One thing we liked was better TypeScript support. We're big on types and proper support, and most of our company is based on TypeScript and Rust. Gatsby can use TS, but you need to set it up and we came across the odd hiccup in the tooling. Next supports this off the shelf and so far, it shows. We haven't had any issues with compilation anywhere in the pipeline or other tooling.

The way Next handles dynamic pages is different, and in my opinion nicer. It also supports something they call incremental static generation which allows pages to be served statically, but incrementally updated as dependent data changes. For example, you know the team in the lead will be the same for thousands of requests, so you might as well serve that statically until the leading team changes. Next will manage that content update for you while serving the content statically for as long as possible. When we migrated, Gatsby didn't support this feature.

Next handles pages differently in general. It maps your directory structure under /src/pages, so that /src/pages/hello-world.tsx will be yourwebsite.com/hello-world. It allows for dynamic content using a convention in which file names can represent a variable, where src/pages/foo/[bar].tsx will make it so [bar] is a variable in the query intercepted by getServerSideProps when you visit yourwebsite.com/foo/whatever.

Otherwise I think the main snag we had with Gatsby was that the team was slower to address issues, introduced bugs more frequently, and we began to see that it was impeding our progress. Our software was working independently of Gatsby and wasn't the issue, and no matter how closely we worked with the maintainers, we couldn't move the needle on any blocking issues. With Next it has been a much different story. Issues are unblocked routinely and the maintainers seem much more engaged and productive.

There's more, but those are the main differences that come to mind.

Re:Next.js 11 by 48309248302 on Tuesday June 15, 2021 @02:42PM (#27519702)
> Gatsby can use TS, but you need to set it up

I'm not sure how long ago you tried Gatsby, but TypeScript doesn't require setup anymore. Just change the file extensions to .tsx/.ts and it works. It also does incremental builds.

Gatsby handles pages in src/pages the same way except it doesn't use placeholders in the filenames. You generate dynamic pages by writing a function. Check out these videos.

https://egghead.io/courses/why-gatsby-uses-graphql-1c319a1c

Re:Next.js 11 by steve_adams_86 on Tuesday June 15, 2021 @05:48PM (#27521944)
Right, graphql - I'm not sure how I forgot about that. We really disliked this part of Gatsby, but I know some people really like it. I can't criticize it really - it probably comes down to preference.

I guess it's been longer than I thought! Incremental builds wasn't even on the roadmap when we migrated.

Re:Next.js 11 by 48309248302 on Tuesday June 15, 2021 @10:16PM (#27524139)
I guess it depends on what you're building. For me the graphql is one of the most useful features. I've only used Next a little, but much prefer Gatsby. I don't think Next is bad though.
Re:Next.js 11 by notjustanymike on Friday June 18, 2021 @04:20PM (#27554545)
This is a hell of an answer for a small question, I really appreciate your write-up!
Re:Next.js 11 by unknown_error on Tuesday June 15, 2021 @02:52PM (#27519843)
We're using it to build a headless Jamstack (headless CMS backend, React frontend).

Next.js makes the frontend a lot nicer to work with, especially if you want to deploy to a serverless config. Out of the box, it handles things like:

* Data pre-fetching (Next talks to the CMS during build instead of via a clientside request during load), then inlines the JSON response into the page so the React components have access to their initial load data (like the first page in a gallery, or the first set of blog posts) as soon as the page loads. No AJAX required. It's FAST for visitors.

* Handles page routing (like /blog/my-post or /products/24453) by mapping CMS entries into page templates

* Makes the developer experience a lot nicer, with preconfigured but extensible webpack, sass, tailwinds etc. support. Also extends the create-react-app dev server to support all the next features so you can get a good preview of how something works before you build for production

* Adds a bunch of optimizations (CSS, web fonts, tree-shaking) for faster performance

* Has optional modules you can use (authentication, ecommerce, AMP, etc.)

* It renders React + the data into straight HTML+CSS (again, fast) and that means some React components also work without Javascript enabled

Additionally, if you host on Vercel*, you get some additional features:

* Automatic image optimizations (auto-generated srcsets, webp, blurry lazy loads, etc. on the CDN)

* Incremental static regeneration: A backend server monitors the CMS for changes every X seconds, then rebuilds only the affected page (in a few seconds) instead of the whole project

* Complex rewrite rules that would normally be difficult with a static site. Not as powerful as .htaccess or Varnish rules, but good enough for simple cases

In general, the upside is that it turns React from a feature library to a more fully-featured web app framework. Still not as powerful as Angular, but hits that sweet spot where you can easily spin up a website with Next.js in a few minutes, and not have to worry about scaffolding or be overwhelmed by having to get a PhD in Angular before you can start using it.

The downside is that its primary maintainer, Vercel, also runs a hosting service. There is an inherent conflict of interest there because some Next.js are Vercel-only, and these are NOT clearly labeled in the docs. You just have to find out, upon deployment, that some features just invisibly break with no error or warning if you try to self-host or host elsewhere. Everything magically works on Vercel, but it's a crapshoot anywhere else. And it's only getting worse as they keep iterating features like next/image or Next Live but only with Vercel support.

Edit: Sorry about the formatting. I don't know what formatting engine HackerNews uses but it's awkward.

Re:Next.js 11 by dbbk on Tuesday June 15, 2021 @01:56PM (#27519003)
If you have a simple marketing or brochure site, and want to use React, it’s pretty much a no brainer.

Anything more complicated eg a dashboard app, you’re going to be fighting it a lot.

Re:Next.js 11 by thesandlord on Tuesday June 15, 2021 @02:00PM (#27519092)
I do not agree with that assessment at all. Next.js is great for making SPAs and full web apps. I've made three (profitable) projects with Next.js and never felt like I had to fight it
Re:Next.js 11 by steve_adams_86 on Tuesday June 15, 2021 @02:20PM (#27519416)
Likewise, I've found it great at allowing you to embed complex logic into dumb content.

What we needed was convention to deliver static content easily without needing to build it ourselves. But we also needed it to stay out of the way of our business logic. Overall, Next has done a stellar job of that. It allows us to build the stuff that matters to us while leaving what Next is great at to Next and its maintainers.

The vercel platform has also been great for us when staging deployments. It's a little slow, but the integration and ease of use again lets us focus on our own business. I don't care to build out staging infrastructure for mostly static content. It's well within everyone's ability on this team, but it's time consuming in investment and maintenance. Recouping that cost is what Next has been great for, from end to end.

Re:Next.js 11 by gherkinnn on Tuesday June 15, 2021 @02:13PM (#27519305)
The key is being page-based. If you can use the browser and URLs as a state machine for your application, Next is excellent. Its dynamic routing is a breeze to work with.

However, if you build an application where that browser-as-state-machine doesn't hold up, you're better off somewhere else.

Re:Next.js 11 by thesandlord on Tuesday June 15, 2021 @06:42PM (#27522539)
Yeah, you have to follow their design pattern / scaffolding very closely. The tradeoff is less flexibility for faster dev cycles.
Re:Next.js 11 by dbbk on Tuesday June 15, 2021 @02:05PM (#27519168)
Really? I've made multiple attempts and always get stuck as soon as you have nested layouts or routes, which dashboards nearly always do (with master/detail views for instance).
Re:Next.js 11 by dfabulich on Tuesday June 15, 2021 @02:18PM (#27519378)
"Nested layouts" just sounds like nested React components.

React is kinda weird… you have to do subcomponents the "React way" and I think a lot of folks struggle with that; it can feel like fighting. But I don't think Next makes that any easier or harder.

I don't know what you mean by "nested routes." Routes are just URLs that map to files. You can create a directory with files, and, uh, subdirectories with files, if you want…?

Re:Next.js 11 by dbbk on Tuesday June 15, 2021 @04:59PM (#27521411)
In Next, every route blows away the DOM and starts over. You can’t have nested routes like you can in React Router, where say you have different layout shells for logged out / logged in views, and then master/detail views where you navigate down a hierarchical navigation.
Re:Next.js 11 by ranza on Tuesday June 15, 2021 @06:12PM (#27522201)
Well you sort of can. Just have a [view].js file, where the view variable will be the component. So you can have {view === 'details' && <Details />}
Re:Next.js 11 by jamil7 on Tuesday June 15, 2021 @02:34PM (#27519593)
I’m guessing they mean pieces of state based on urls, like which tab is active here or is a modal open or closed kind of thing.
Re:Next.js 11 by tablef on Tuesday June 15, 2021 @02:05PM (#27519170)
Do you have any public project template repos?