In the previous series ( How to slay Habr in a hurry ) launched a project based on the Create React App (CRA). But this is a SPA , which is not very suitable when indexing is required in search engines. Need Server Side Rendering (SSR). And preferably out of the box, and not on the knee. It is extremely wasteful to spend resources on the independent development of basic technologies. How to choose a platform with SSR support? In practice, of course, the POC . I will try to implement a CRUD with an input form on the Material-UI , considering the candidates: React Starter Kit (RSK), NEXT.js and Electrode (not to be confused with Electron ).
RSK has its own bicycle - universal-router
. Uploading external data to Redux in server rendering is offered via the express (or koa, or hapi) configuration.
NEXT.js approached the question of routing in an original way, suggesting the use of file names in the pages folder. Inside the usual React-components with a static method getInitialProps
, in which it is proposed to perform the loading of external data in server rendering. If you need to customize the processing of requests, then this is done in the most natural way - through the configuration of express (or koa, or hapi).
Electrode, unlike other platforms considered, does not force to refuse redux-router
. This means that at the time of the implementation of the frontend, you can return to the development of the CRA. All the magic of server rendering inside the electrode-redux-router-engine
.
It is very interesting to see how the site zeit.co is loaded , which works on NEXT.js. When you open the page of the list of posts, 15 MB of traffic comes, but further transitions to the pages of the posts occur instantly - everything is cached in the browser. However, this disgrace can be disabled through the prefetch
property of the Link
component. Offline-first is promised in NEXT.js ( with reservations ), but it has already been implemented in Electrode. SSR caching: there is an example in NEXT.js, but in Electrode the solution is in production .
In RSK not less interesting behavior is noticed. When I switch the list items in the history of the URL entry line in Chrome, I observe the execution of the routes on the server, although I have not yet confirmed my choice. As a result, the finished page is issued from the server faster.
NEXT.js and Electrode hide their implementation, but allow you to configure the assembly. CRA prohibits configuring the assembly only through the eject (people fought half a year for the right to screw the SCSS), but there is a way out . In RSK, only fork, you need to keep track of the updates of the project and use their hands - the fee for full access to the source code of the platform.
RSK (like CRA) kills by loading a new browser page during the initial build, NEXT.js and Electrode do not have this drawback.
Electrode very slowly performs the initial assembly.
NEXT.js offers a great service for deploying now , and it is not tied to a specific platform.
In RSK, terrible error output, in addition, Source Maps debugging works incorrectly, the cursor moves a couple of lines below. In Electrode, it is simply impossible to understand what went wrong if the error is on the server side. In NEXT.js, the situation is much better, and they promise to finalize it in the near future. CRA is the standard here, and I want to say a special thank you for displaying ESLint messages in the process of working on the project. By the way, RSK turned the ESLint checks into a form of torture, I cut out the pre-commit.
NEXT.js captivates with a variety of recipes, although some important questions are still unanswered, but the dynamics of the project’s development are encouraging. RSK and Electrode complement the overall set well. On the example of CSRF : nothing at all in NEXT.js, a holiday in Electrode, in RSK found a reference to csurf .
Yesterday, March 27, NEXT.js 2.0 was released, six months after the start. Dvizhuha around NEXT.js much more compared to RSK and Electrode. Judging by the stars on GitHub, NEXT.js is catching up with CRA, given the difference in age.
Behind the technology are specific people. This is also an important point of choice. NEXT.js develops ZEIT - the founder of Guillermo Rauch was the technical director and co-founder of LearnBoost, remember TJ Holowaychuk?
Many projects are guided by the recommendations of the Presentational and Container Components , but the distinguished author admits in footnotes that the concept of separation is controversial, and the components can be mixed. And if so, why drag a suitcase without a handle? It is more convenient to store all project components in one shared folder. What are the advantages:
When the project grows up, you should split it up into private npm-packages, encapsulating the implementation. But not to grow a tree of subfolders inside the folder of components - to develop and maintain this significantly more difficult. Verified
Further more. I tried CSS-Modules - I refused BEM, but after styled-jsx I can’t look at CSS-Modules anymore. Why bind each root element of a component through className, for the sake of which, switching the attention context between files is not clear.
Reaching enlightenment with the common folder of components, applied ducks-pattern . But this is not all, by calling right now, you will get the opportunity to completely abandon the constants for binding actions-reducers using the redux-act package:
// Create an action creator (description is optional) const add = createAction('add some stuff'); const increment = createAction('increment the state'); const decrement = createAction('decrement the state'); // Create a reducer const counterReducer = createReducer({ [increment]: (state) => state + 1, [decrement]: (state) => state - 1, [add]: (state, payload) => state + payload, }, 0); // <-- This is the default state
Add js-hyperclick plugin .
Add a package to the project:
$ yarn add babel-plugin-module-resolver -D
.babelrc
{ "presets": [ "next/babel" ], "plugins": [ ["module-resolver", { "root": [""] }] ] }
package.json
{ "moduleRoots": [ "" }
example:
import MyComponent from 'components/MyComponent' import MyPage from 'pages/MyPage'
By the way, NEXT.js eliminates the pointless src folder in the project. Just did not pay attention before, but how is it good without her!
And I just wanted to validate the form fields. But 462 open issues are hinted at. I could not make myself. Service code is obtained more than without redux-form. And you need to think not only about the behavior of the form, but how to make it work with the help of this beautiful wrapper. In the morgue. It is impossible to please everyone and everything. It needs to offer a solution for each application. I had a similar sad experience with the Meteor bike AutoForm. At first glance - great. You describe the config and it gives you the form itself! For two fields it works. But when the forms are large, yes with related fields. Bozhechki. Brakes terribly. Buggy. And again, forcing you to climb under the hood with a sledgehammer. The author scored on pull requests. There is a fork - you hang up on support a big heap of "universal" code. Is it necessary?
This is a longstanding dispute with a technical but small but proud web studio, that no UI frameworks can replace manual work. It is clear that the development of interfaces is bread and butter. But I am a strong supporter of the use of ready-made solutions. I can well imagine what could be the elaboration of details, having experience in implementing the project from scratch on an adult TK from the UI / UX design gurus. The Material Design Guideline is the second cosmic speed. But where is its implementation? What I saw was slag. In addition to the Material-UI . Turned, screwed, finished the file. Well, yes, there are a few shortcomings. More precisely 647 open issues. But the patient is more alive than dead. Demo - form add / edit blog post.
I ran in a circle, and now I can say with confidence that the proposed method of optionally screwing SSR to the SPA is the most correct ( Wishlist formulated a long time ago ).
I was happy when I sat on the CRA. Until then, until I read " What to base React applications on." And then continuous struggle with the complexity of the environment. I read issues - the head swells.
All researched solutions offer their bikes for routing, which can already be thrown away - the latest version of React-Router implements the functionality for SSR .
Source: https://habr.com/ru/post/325088/
All Articles