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 bette...
For further actions, you may consider blocking this person and/or reporting abuse
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
/api
at the root of the NextJS app.Create a file
api/hello.js
Then we can access this API route when we make request to
http://localhost:3000/api/hello
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:so basically:
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/pages
for reserved for "frontend" andsrc/api
reserved 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 ofpages/
. I'm surprised that it still works even when it is outside ofpages/
. 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 💜