627 points · by tbassetto · 9y ago · 217 comments
For those wondering what this is, it's basically a slightly more opinionated Create React App [1].
Here are some of the benefits:
- No need to setup complicated tooling
- Server and client (SPA-style) rendering out of the box
- Filesystem for routing using the `pages` directory
- `getInitialProps` component lifecycle to encapsulate data requirements per component
- Automatic code splitting for each route
- If it works with React, it works with Next.js
Here are some of the issues we encountered:
- Importing non-JS files from `node_modules` (like normalize.css) was not as simple as it could be (#1245 [2])
- Animation transition between pages is still being worked on (#88 [3])
- There are still some inconsistencies between server and client that could improve, like running Webpack on the server (#1245 [2])
- Doing proper scroll restoration when routing (#1309 [4])
We will continue to use it as long as it keeps letting us move fast without having to worry about spending hours setting up React tooling.
[1] https://github.com/facebookincubator/create-react-app
[2] https://github.com/zeit/next.js/issues/1245
How does the page-based router deal with routes like /things/123/subthings/456?
Typescript?
Yes
> How does the page-based router deal with routes like /things/123/subthings/456?
It doesn't, you have to use the programmatic router for that.
> Typescript?
Don't believe so, see this comment in this thread: https://news.ycombinator.com/item?id=13968723
https://github.com/zeit/next.js/tree/master/examples/with-ty...
check out react-overdrive[1], which is also referenced in issue#88 but deserves to be mentioned here imo. The author provides an example[2] what overdrive looks like based on the instagram example[3] from next.js.
[1] https://github.com/berzniz/react-overdrive
Does that mean the top benefit of next.js is to alleviate difficulties with Webpack + Babel + etc?
And server-side rendering which solves common SEO and performance challenges of SPAs. Here's an article showing where server-side rendering is especially useful for SEO reasons:
https://hackernoon.com/react-js-cms-blog-tutorial-e090c69984...
For example, the only custom change we had to make was to add Webpack's `DefinePlugin` to expose `.env` variables in the client. It was extremely easy though (3 LOC) and we didn't need a full Webpack config.
It appears the screencast video is to blame, it is not only huge (~70MB), but it keeps getting downloaded instead of just replaying. I just checked the console again and the page is now up over 300MB downloaded. Glad I wasn't on mobile data!
Update: I downloaded the video and ran it through ffmpeg to see how much space could be saved... Original size 72.2MB, new size 1.7MB. Screencasts obviously compress very well, but this was pretty surprising. You could easily optimize this down further and probably half the size yet again.
ffmpeg -i hello-world_2.mp4 -vcodec libx264 -preset veryfast smaller.mp4Question, do you have caching disabled when devtools is open? If so that'll cause it to continually re-download. Looking at the markup it shouldn't redownload but devtools will make that happen quite easily.
Completely agreed about the size though; geeze.
As for downloading multiple times: I don't think it's happening though it looks like it.
The server is sending 206 Partial Content for the video. This is why the file downloads multiple ranges if the page is left open for a while. The first response if for a chunk of the file. The next few are for different chunks. Then it repeats.
~8 Mins on the page and I have 5 downloads of the hello-world_2.mp4 that went to the server. I have 4 that were able to be served from the disk cache.
I see Next.js primarily focusing on server-rendered React and then it also supports client-rendering. I see create-react-app is primarily focused on client-javascript. Am I missing something?
Next.js also focuses on client side JavaScript, but happens to render at the server first, so that there's something there for users before the client bundles have downloaded, parsed and been executed.
You could think of it as a more configurable create-react-app that supports server side rendering, with a convention over configuration approach to pages and links.
You are a poet, sir. Best answer.
I really just want something like Handlebars, with a generic adapter for any language for compilation.
Then you can load with javascript a client-side controller if you want to add in interactivity.
---
To expand, I think the idea solution would be the following:
1. Server side view, let's call it "Bars"
2. Bars can be written on the front-end or back-end, does't matter. It'll compile to BarsHTML.
3. If you write Bars on the back-end, then when you serve your page it'll compile Bars into BarsHTML.
3a. If you want to sprinkle JS onto Bars, you'll use BarsControllerJS, an adapter to whatever framework of your choice, to manipulate it. The main difference between this and manipulating the DOM is that the interface to do this is not DOM centric.
4. If you write Bars on the front-end, it's the same as (3), but you get (3a). If you decide you'd rather do server side rendering, you literally just move your views to the server. The controller is already abstracted, so you wouldn't need to do anything else.
test.marko: `<h1 on-click('callMyFunc')>${ input.hello }</h1>`
component.js: `class { callMyFunc(e) { console.log(e); } }`
...gets compiled to test.marko.js which can then be require'd or using a special runtime loader or transformer. It hits a nice sweet spot for me, and is worth checking out.
I will write a more detailed post when it's ready for release. Meanwhile you can check the "How Pragma works" in the intro blog post http://www.laktek.com/2016/11/29/introducing-pragma/
Inspired by an earlier(probably first release) of NextJS, I had attempted TypeScript and server-rendered react here:
http://www.anupshinde.com/posts/react-typescript-server-rend...
[0]: https://github.com/styfle/react-server-example-tsx
[1]: https://github.com/zeit/next.js/tree/master/examples/with-ty...
I feel like React/Angular/Vue/etc apps are becoming "the way the web is done" when so many things would be simpler and more efficient as plain old web sites with server-rendered HTML. It's like everyone decided that because convertibles are cool, they should be used for everything, and are now spending a ton of effort hitching trailers to their convertibles when it would have been much simpler and more effective to just drive a boring minivan in the first place.
But what drives me crazy is sites like Medium [0][1]. It's a shared blog site. To read a one sentence essay on Medium requires downloading over a megabyte of data. And their comment system is still inscrutably terrible.
Content-focused sites should absolutely not be done as SPAs. Server rendered HTML and client-side enhancement will deliver a better experience to the customer and IMO a better developer experience as well.
[0]: http://idlewords.com/talks/website_obesity.htm [1]: https://medium.com/@mceglowski/chickenshit-minimalism-846fc1...
Javascript is not required to read it. Javascript is used to provide extra functionality outside of the download / render cycle, to speed the experience of the page up, and to make future navigation faster.
I understand where you are coming from, but you picked a bad example.
Of course MVC (React, Angular, ...) has its place for web apps and is great when it fits, but I think it's crazy and counter-productive to use complex and costly MVC frameworks for mere content-driven sites (like what most public websites are about).
Even when your application is not an SPA, some areas could still be SPA: editors, etc.
Don't kid yourself, Facebook is much much more than just a forum. It might have been little more than a forum initially, and the amount of javascript it originally had kind of upholds the OP's original statement. It was a pretty slim webapp originally.
A forum is actually a pretty solid choice for an SPA, specifically the isomorphic react kind next.js sponsors. If you disagree or don't need the benefits, most existing forum software is already not an SPA, so no big deal. But a forum as an SPA can enable some more high tech forum solutions that may blur the distinction between forum community and slack community. Once the groundwork is laid out, who knows what kind of alternative architectures or plugins we'll see for it.
Also, when JS is disabled, the server will take you to the intended page and render it for you. Literally the best of both worlds.
Why? Because after the problem of initial load, I think that further web requests are among the most expensive things you can do in terms of user wait time or latency, and that if you only start moving at the very time of user request, then it's too late to prevent a moment user waiting.
I tried to learn it and struggled understanding the new concepts react was bringing in all while people were saying "It's easy! Everything's a components. HTML,CSS,JS all in one place!" whilst I was struggling to get a simple hello world example to run.
Just recently at my University we were forced to learn React as a part of our course and damn after I finally understood it I fell in love! It's a huge improvement over the normal web-app design work flow.
So, I would urge anyone whose anti-react to honestly give it a good go and force yourself a little to get over the initial learning curve and trust me you'll love it!
In my personal opinion, these reasons are superficial: the reason to use React is that programming UIs declaratively is easier to reason about than imperative UIs. That point can be subtle. It helps to have worked on (and struggled with) an imperatively-programmed UI before you can see how much of a pain it is. And it didn't help that out of the gate, React was pitched with "components" and "virtual DOM," and it took a while for even Facebook to be able to communicate what was so compelling about it.
Any concise answer on how react's declarative UI differs from a 2 way binding approach in angular (or if they have changed it that)?
React's virtual DOM is a hands off implementation detail that forces understanding of MVC. People can't asks= "How do I put the thing in the place", b/c that's automated by the virtual DOM. Instead, they are forced to understand that they need to describe the end state. Which is something that even an older framework like Backbone tried to do, with the end state being you only having to care about your models with the rendering automated.
Progression of (frontend) MVC started with Backbone, with manually wiring up models and view render functions so that the model changes could drive the UI. It's a specific implementation of pub/sub with the published events being a certain set of model changes. (add/change/delete/...)
(Redux is pub/sub with a security guard logging your actions and only giving you specific vetted actions to combine. Also, functional goodness.)
Angular 1 went with a polling approach that negated much of the boilerplate in backbone but was not performant (and bad docs, new things to learn, etc..) but also showed many people a cleaner paradigm of writing UI code. The trap with angular was putting everything in the controller just like the trap with backbone was putting everything in the render function.
React uses a virtual DOM so the thing to render doesn't need developer input. It's automatically inferred from the DOM diffs so the developer can focus purely on describing the UI. This is the forcing function that forces people to put down jquery and actually learn MVC.
Once this is understood, you can get into managing more complex models that actually mutually affect each other that may necessitate something like Redux. (Also, somewhere along the way, OOP become unpopular and functional stuff became hip)
JSX btw, is just an extension of javascript to create DOM elements in a less ugly way. Angular and Vue went from the other direction and extended HTML to support javascript functions. It's "<div v-for="a in apples">{{a}}</div>" vs apples.map(() => <div>red apple</div>)" It's personal preference in many ways. (You'd actually need to know a fair bit of functional js though to write concise non-ugly JSX that doesn't bury the meaning of your UI description)
The main issue I have with react is that it seems much more like a lifestyle than a tool that gets out of the way. When people list out their reasons for liking react I'm mostly just hearing "because facebook, facebook, cool, cool, facebook" just like angular was driven by google before the angular 2 marketing fiasco.
And there IS truth in it. The background philosophy driving React is actually a philosophy of functional programming which has real merit. In the real world though, with many people knowing only how to use jquery, React may help you explain to them how to understand MVC, but you're going to have a much more difficult time shoving the functional lifestyle that comes with it down their throat. But hey, if you're in the facebook sphere of influence or want to be a part of it, go for React. (Also, if you're a new grad who doesn't have the baggage of jquery)
Paradigm shifts often take a much longer time horizon than expected and often require an "earthquake" for newcomers to upset the established ideas. React and Typescript were in background mode for quite a while. The non-backwards compatibility of angular2 drove people to React, and I think Java programmers getting into web development and Satya Nadella being cooler than Balmer gave typescript a good boost.
In the end though, I don't really want to be comparing hundreds of different hammers to build my house. I just want to build a house. And hey, hopefully I have more skills than being a builder so that when entire houses are 3D printed I can still find a way to justify my existence.
Being "anti-react" doesn't mean anti-component approach. You can do same with angular 1.5+, vue, aurelia or others.
I like React (especially with all the new setup and debugging tooling) but whenever I use it, I feel that it is a small step down from Polymer.
I think that unfortunately Polymer 0.5 was launched too early and its reputation has been tainted even though Polymer 2.0 is amazing.
I do know that there are also developers in the Vue.js camp who would never use React out of principle. I spoke with HR people from Facebook in the past and they've told me that there are developers who simply refuse to use React but I think it's more a matter of stubbornness because, all things considered, it is roughly comparable to Polymer and Vue.js.
I'd love browser makers to focus on the basics like rendering, js and asm.js and leave the rest to libs.
The problem is that for many, react does not do that – because you need npm, webpack, babel, systemjs, and so on and so on.
We come from a world where you have a simple .gradle config file, and you press one command, and it installs and everything just works.
No need to configure thousands of transpilers, no need to handle constantly breaking dependencies, because people keep their API stable for years, some even decades (see Bouncycastle, e.g.), etc.
Coming from this style of development where tech actually works well, and is stable, to the JS world of React where everything constantly breaks, is very complicated and over enterprise-ified, etc is quite annoying.
Obviously, on the other hand, the React world provides other massive advantages, such as quick iteration, innovation, etc – but the work to get something working with constant quality for years is entirely different. Because you’ll have to fork all your dependencies, because those won’t be stable.
So if you want your product to be included in Debian Stable, you don’t want to use React. Because you’ll end up forking and maintaining every single of your dependencies-.
You're right in that JS tools do need to get stronger, and over even the last 2-3 years they have. I haven't had mine "constantly breaking", generally once you build a new application and set it up with your standard dependencies. Like an ORM, what ever server middleware, test frameworks, utility libraries, etc. It tends to be pretty stable, unless you try to upgrade something years down the road.
This couldn't be further from the truth. And besides, create-react-app solves that.
> No need to configure thousands of transpilers, no need to handle constantly breaking dependencies, because people keep their API stable for years, some even decades
I have been using react since early 2014, back when it was still very early pre-release. I have had to change exactly one thing w.r.t to the react API. When we decided to add in webpack, again, I had to change exactly one thing there as well -- I switched to a .babelrc file and they included the react transpiler by default.
Until something breaks, and you have to go through all the configs and try to fix it.
I’ve seen https://kukuruku.co/post/hello-real-world-in-php-in-2017/ far too often.
No, that shouldn’t even be a thing. All runtimes, worst case, should be maybe 6 months behind the standard, otherwise I’d want to use a single transpiler.
Going TypeScript -> ES7 -> ES6 -> ES5 is insanity.
> Hand bundling/splitting of code would be faster than browserify, webpack?
Why should that even be necessary? Upload your code file by file, and actually use the browser’s required, or have one single script implementing this as polyfill. Why should I even have to do this?
> Hand finding, curating, tracking of dependencies would be faster than NPM? mmk.
No, but NPM’s tracking is useless when dependencies constantly change their API with every release.
I’ve got so many dependencies via NPM that even change their name and API constantly that many of my projects have been broken after just a year, but all after max 2 years.
I’ve got Java projects that are 5 years old and every dependency is still update, with a stable API.
Their Hackernews clone based on their own SPA framework is much slower than the plain old HTML site.
Cannot blame them, althought the situation is better nowadays, it's still not uncommon to find a spa site where the said things are broken.
I feel like there was a huge reduction in bug-prone madness when React + the Flux pattern started gaining steam. We are still collectively working toward an end (reliable software in the browser) but it's impossible for me not to feel the difference in the past few years, as a dev and a user.
SPAs are sort of a hack. The web was designed for documents that link to each other. Maybe they'll never be perfect until we reach some sort of impossible isomorphic framework utopia, but there is real progress being made
The canonical example: a CRUD page with a dropdown of choices based on entitlement.
Server rendered: in your template somewhere, {for choice in limited_choices} and so on.
SPA: you need to coordinate a REST endpoint that has options for entitlement of just this dropdown, etc. You now need a route for REST, a route for the SPA page, and view code for both.
Don't get me wrong, with unlimited time, build everything as REST services with a client that consumes it. But sometimes server-side rendering is just simpler.
It's a lot faster to load template once and re-render on client with different data.
And we are pretty closely work with JS stuff. But Next.js is inspired by the simplicity of PHP.
### Anti SPA
The whole idea of Next.js is to build a SPA easily. But it's also server rendered by default.
Are you implying that react-way is the only way of doing SPA-s?
> Do you think the web Peaked with Perl-based CGI-scripts
I think your comment is just rude. There are plenty of other options in the JS world - react is just one of them.
Personally, I dislike that React is 100+KB file size, when Preact or Inferno can do 95% of what I want at with 3KB.
(Yes, I know gzipping exists but it's still huge by comparison, and JS parse time is a real issue on underpowered mobile devices)
There were many developers who were raised to strive for segregating logic from display (keep your js away from your HTML) and that MVC was a universal solution to front-end development. From what I've seen angular does these things very well and feels more natural to teams where this mindset is well-established.
Next.js and React Server are making steps towards solving this by bringing concepts like incremental loading and data hydration by default.
I've talked about some of the approaches Facebook used to improve startup performance in the past: https://www.youtube.com/watch?v=b875VAC6r-w
In some very important ways, yes. Specifically, it was still within the contraints of what HTTP and HTML were designed for: the transfer and rendering of marked up documents. Obviously all technology is x steps forward, y steps back, and you aim for some workable linear combination of x and y, but there have definitely been several regressions since the days of Perl-based CGI scripts, and I personally haven't found them worth the advances.
I don't trust their License Agreement when it states they can revoke your License for any reason. Basically, if I get on the wrong side of Facebook, they can pull my License for React without a warning.
Unless they have amended their license since the last time I read it. In which case, I would like to see the updated version. Until then, I will continue to use InfernoJS.
React is another in a long line of Backbone, Angular, Durandal, etc. that ultimately just make stuff a lot more complicated for fairly trivial benefits. It dictates fairly complicated ways of doing simple things with long complicated tool chains that regularly break for obscure reasons that you can debug but ultimately it feels like you're simply wasting your life dealing with it. It's easy for juniors to make ridiculously complicated messes in them. They remind me of the good old days of RAD dev tools. "Oh so easy" they claimed.
And if you're any good, you can enhance traditional web forms and add the same sort of functionality without all the overhead, or complexity. Perhaps I simply have the benefit of over a decade of front-end dev.
As for SPAs, they still live firmly in the uncanny valley. They're poor versions of desktop apps, even desktop apps from the 1990s. Anyone claiming otherwise is simply delusional. Because of javascript and the DOM, they also tend to complexity, so that anything past a glorified to-do app becomes an absolute nightmare to code in.
I use Trello, Asana, Toggl, SendWithUs, etc., etc. They're all, well, a bit shit. All of them have really annoying niggles, fields you click and then might auto-save but might not. Poor desktop interfaces because "mobile first". Tabbing that doesn't work properly. Data that gets out of sync. Or they're sllllooooowwwwwwwwwww.
As for the reason React became so popular? This happened before with terrible frameworks. I may be a cynic, but it feels front end devs have got into some sort of mega-echo chamber where a new framework comes along and everyone jumps on to it because they don't want to seem out-of-date. The front-end world feels like it's in a strange kind of culture at the moment, a type of Emperor's New Clothes, where if you're not using The Latest Framework, you're obviously not worth employing.
Not because it makes you get a product to market quicker. It's a new form of bike shedding.
You sound like you don't really know or understand React. Which is fine, but it doesn't put you in a good position to criticize it intelligently. React isn't popular because of any kind of echo chamber phenomenon. It's popular because of its design; it managed hit a particular very sweet spot combining the various principles of composition, modularity, functional purity and internally consistent state management.
I do agree that web apps aren't as good as desktop apps. But that's not the fault of SPAs.
I know you can use react on top of rails (with postgres, mysql, etc), but then I start feeling too many weird abstractions going on and have a hard time following the bouncing ball. React claims to be just the V part of MVC which is fine, but when I'm trying to quickly build up a full stack application rails just felt the simplest way to do so.
Bringing this up not because I think there is anything wrong with React and I know they are solving a pure front end problem, but this is more of a question to what's the most elegant full stack solution that would be comparable to RoR solution with any of these new hot js frameworks? If there is I think I'd be more than happy to startup some projects that way.
I work on a couple of startups at the moment, one of which that has an admin interface in a SPA, one doesn't. I can throw in new reports, functionality, etc. in the non-SPA one with less than 1/2 day's worth of work, sometimes even in minutes. The SPA one takes days to do similar functionality.
It's just all so bloody complicated getting it working together. Make a new end point, create a new DTO, make sure you're not leaking anything from the end-point, duplicate validation logic, wire up view models, have it not work completely for an hour or two of debugging because you forget to add one line to some gulp file or something equally ridiculous.
And then you throw in something like React and they want you to do it X way with Y thinking and Z plugin doesn't like you're returning the wrong content type or data structure or something but doesn't throw an error about it, logs it as some sort of warning, or if there is an error it's deep in the guts of 10 nested 1 line self-referential call in the library.
It just adds so many moving parts, functionality ends up sprawled over 10 different files, it's hard to grok or reason about, hard to know if you've done everything you need to.
I find it simply unpleasant to work in that way. I just want to write the UI and the logic and be gone, not dealing with frameworks barfing out bizarre warnings or mysteriously broken builds.
Actually, nothing fills me with more dread in a SPA than when I click a button or something and nothing happens. The worst problem programming in SPAs is when things simply just don't work and there's no error at all because you didn't quite invoke the magic framework in quite the correct way. Yay SPAs.
I'm pretty clear on how to use client-side react with redux, it's just the server side part that would include stuff like user authentication seems confusing to me so far, since I haven't seen a clear implementation of that yet.
Thanks!
The challenge seems to be that while the server side rendered bit would work fine - it could query a DB locally after authenticating a users cookie (for example)...
If the client routed to a page (not using the server rendering) - you would need to have duplicated code to handle how the client can fetch that data, as it cannot query the DB directly.
My one criticism of this is: Component CSS is cancer. I've worked on a large scale javascript project and it was riddled with duplicated CSS in every component, all in the name of being conflict-free.
You know what I call that? Not knowing how to scope your styles properly with something like BEM. http://getbem.com/
I've worked extensively with Aphrodite (disclaimer, my employer maintains it) and have found it works with React to make a pretty good workflow for SPAs. It does require some buy-in from everyone involved with the codebase since you'll have Aphrodite-defined styles clashing with stuff in external stylesheets, and there are occasional edge cases where you'd like to use some crazy CSS selector that Aphrodite doesn't support, but this is often a sign that you should be restructuring your component in a more straightforward way.
The style reuse and composability story is very good, since all styles are wrapped around bare JS objects which are super easy to manipulate and the ES6 spread syntax [2] makes it all look very elegant. If it's important to you to keep styles and markup in different files, you can do that too as long as you're happy giving them all .js/.jsx/.ts extensions.
In general it is frustrating to me that total buy-in into the React model comes at the cost of support for actual standards like Web Components, but I never want to write UI in a non-declarative way again so I'm hoping someone smarter than I can figure out how to bring these two worlds together so we can let the browser properly namespace our CSS rather than relying on randomly generated class name infixes as Aphrodite does.
[1] https://engineering.hellofresh.com/the-css-in-js-battle-89c3...
[2] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
It's just as easy to duplicate CSS in BEM as it is with encapsulated components, it's simply an indication you haven't reached the right granularity of components yet. Finding the right unit of reuse for any project is hard, it's not something that's magically solved just by saying "use X".
'sane defaults' and 'PHP of old' are not things that go in a sentence together cough magic quotes cough
The modern embedded server is what's beautiful.
Save your work before trying, though.
Greetings
When we built it, we had out of the box:
+ Tools (our name for components)
+ Pages (to support all web standards)
You place tools on pages, the framework does the rest:
+ It loads JS and CSS on demand
+ It adds/removes tools from pages automatically as you navigate
+ Support for web standards, HTML5 history w fallbacks, etc.
+ Tools can contain other tools
+ You have events for when all parent tools have activated (onActivate) and when all child tools activated (onInit)
+ JS and CSS had to be namespaced by convention from day 1, by module and tool name.
We use events instead of virtual DOM. We didn't use fancy JSX or preprocessors to do it. It's all written in ES4 JS and runs on every modern browser and IE8.
But the problems are very similar in scope.
Here is a showcase of what is possible: https://vimeo.com/208438090
And here are some tutorial videos on pages and tools: https://qbix.com/platform/tutorials
Really? 3.1 million developers? I'm not saying I don't believe, but wow, how?
edit: actually I am saying either I don't believe or you've miscalculated somehow.
I just don't trust their modified License Agreement.
Very interesting approach!
What are the pros and the cons? Someone that has used both should help us here.
RBP is actually more opinionated. It's "best* practices to the extreme" at the cost of productivity and lots of boilerplate. It's also a little hostile towards server-side rendering.
I enjoyed RBP and learned from it, but we switched to next.js (with styled-components and apollo-client) and things have been far smoother. To be honest I can't really think of a reason to use RBP over Next.js.
The creator @mxstbr has tweeted positively about Next.js, has other projects to look after (e.g. styled-components), and hasn't updated it since January, so I'm not sure RBP has much of a future anyway.
I though it was a satire, based on its name. Maybe there is some hidden self deprecating joke that went over my head, but Next + JS + 2.0 seemed too buzzwordy to be true.
The tech looks interesting. Still looks to me like a lot of stuff that I don't understand why I would need, but that's an old rant I have with the curent js ecosystem.
You can add these constraints later, but you will be on your own island. Maybe sometime a standard ("best current practices") emerges...