I used to use Create React App (CRA) for building new applications (i.e. app.example.com). For landing pages, however, I would use NextJS for better SEO (i.e. www.example.com). Nowadays I will exclusively use NextJS over CRA when starting new applications for the following three reasons.
Reason 1: Monolith
When starting a new application, the main goal is to find product market fit. This means reducing distractions. With Create React App, we might need to build three different applications to test out our idea (Client App, Landing Page, API). With NextJS, we only need to build one app to test out our idea.
NextJS allows us to build our landing page, application, and API server all in a monolith. We can write backend routes that interact with a database inside of the
api/ directory, server side render pages that is important to SEO, and also render things in the client side if necessary.
I want to build a monolith when kickstarting an app without compromising SEO. NextJS is the only tool that allows me to do that so that I can focus on solving problems for customers.
Reason 2: Performance
Google ranks websites with better performance higher in search results. Therefore some say only use NextJS when SEO matters...but it's hard for me to think of a reason why we would not want better performance and SEO regardless of the application. Why sacrifice on SEO when we don't have to? NextJS gives us the best of both worlds; we can decide when to server side render and when to client side render.
Reason 3: Ecosystem
I reach for NextJS when starting any new application because it allows me to build a monolith using a mature framework to validate business ideas without sacrificing on performance.
Top comments (12)
Take a look at Remix, it looks very promising!
There is part of NextJS I don't get and honestly haven't really looked into it. If you are creating an API route. Is there a special way to query that API or is it still going to 'url/API/todo'
It's just like how it worlds with
/pages. Give it a whirl!
Then create folder
/apiat the root of the NextJS app.
Create a file
Then we can access this API route when we make request to
API Routes Introduction
this is one point that causes quite a bit of confusion, but these API routes are actually added inside the
my-app/pages/folder, not alongside it... as per the documentation you linked to:
disclaimer: I am still learning my way around Next.js myself, so I'm far from an expert on the topic, but this point has already caught me out a few times now..
perhaps it would've been more "logical" to have
src/pagesfor reserved for "frontend" and
src/apireserved for "backend"? ..and now suddenly I'm wondering if it might change to that one day.
Oh that was a mistake on my part. The docs do indicate that the
api/should be inside of
pages/. I'm surprised that it still works even when it is outside of
pages/. Thanks for clarifying.
hmm.. I really shouldn't be digging into this now (I'm at work, so I need to do work stuff), but......
after a little searching, I found an interesting part of the NextJS docs, which states:
which goes on to list a caveat for this:
so after a quick chat with my friend Charlie (yes, the unicorn), we have a theory that perhaps there might be something similar with the API routes handling? maybe the
pages/parent folder path is "optional" due to some internal conditional checks and/or default fallbacks?? honestly not sure.
FWIW, I did see something in the configuration side about redirecting
api/URL requests to a custom folder in the project, not sure which tab that was in now though.
anyways, I need to get on with my day job now, so I'll go play with NextJS a bit more when I get home this evening. (:
It's like the fs routing. If you've api/auth/register.js then url will be /api/auth/register. Then you've to check if the request is POST or GET or etc via conditional with req.method.
The title suggests that one should always choose NextJS over CRA, for any project - is that what you mean to say? For sure there are scenarios where you have "app-like" (rather than "site-like") requirements, where NextJS doesn't make much sense?
Then, your point 2 says "Performance", suggesting that NextJS somehow "has better performance" (than a plain React app) ... can you expand on that?
Yes, I am claiming that for building new websites, I would always choose NextJS over CRA. NextJS already provides everything CRA does (client side rendering) but also gives me the option to server side render specific pages. Server side rendering provides better SEO/performance. Server side rendering allows users to see content faster (since it's already rendered), which Google rewards by placing it higher than purely client side rendered apps.
There may be scenarios where we want to render everything on the client side. In this case, it might be overkill to use NextJS if we are never going to use the additional features. However, even for apps where SEO is not important, there are often specific pages that we want good SEO.
One example I can think of where I would use CRA is if I'm building an Electron app. Electron apps mimic native applications where everything is rendered on the client side. The landing page for my Electron app might still be in NextJS, but I can see myself using CRA to kickstart an Electron app...since Electron apps by definition aren't websites and do not show up on search results.
Fair enough! The only thing that confused me is when I thought that you suggested that NextJS somehow magically solves all performance problems ... if your React code is poorly designed or written, causing slow performance, then NextJS won't suddenly make it run fast, I think we'll agree on that.
That's actually really great to use NextJs over CRA as my experience.
Both are great options 💜