What is next.js?
Listen to me sing the first week of cutting rod double Jay. React based SSR framework. What is SSR? Question mark, why do you have so many friends? Complete a SSR framework, the difficulty can be high to low, said low because of SSR to do is to pack out two copies of the code, is a server, a client, then the server, user access through the server for the first time, we parse the request and returns the page of the server, the server rendering good page, after arriving in the client, After the client hydrates, the routing management is taken over by the client’s route listening event, and the corresponding component is pulled for re-rendering. However, when it comes to enterprise-level development, we need more specification, performance optimization, and stability. This is where next. Js comes in.
Is next. Js special?
No, it does much of what the usual SSR framework does, but it goes a lot further. The use of next. Js is very simple. After initializing a project with CLI, we will write pages in the Pages directory and place resource files in the public directory according to the convention. But it is best not to let the resource path conflict with the page. Next will scan directories and generate routing tables. Pages directories will be treated as entries in webpack. The client and server can also easily find the corresponding file for route loading. The entry of the page page of the client is optimized, which will be loaded once by the built-in Webpack Loader before coming out for use. Why do you want to do this? The next time a routing event is triggered, it can be read directly from the memory, even without the HTTP cache layer. The performance of routing is not only mentioned now, the built-in link component goes further, the mouse hover will trigger prefetch or entering the visual area will trigger Prefetch, so when we really click to trigger the route jump, because JS has pulled the cache in advance, the experience can be said to be a great ~
What else did next do?
The built-in webpack plugin, for example, clears the require. Cache cache after it is packaged, so that the server doesn’t load old code. There will be when the generation of the page to save, next time when the packaging finished, found that there is no generation of this page, will help you to delete this page and so on. As far as the user is concerned, it doesn’t matter, because next does it all for you internally.
Next. Js in the client entry will unshift a client under the clien directory, this file is the main loading entry of the client, responsible for rendering, pull routing, etc., client routing also uses publish and subscribe mode, you need to load routes, you can, first help you to rely on JS and CSS loading back. Next is very considerate of pre-loaded routes, if you are in 2G or SaveData mode (check MDN, The reduced data usage option on the user agent has set a reduced data usage option on the user agent.) Next also fixed a problem in Webpack where SSR files were loaded multiple times and instantiated. The solution was to add a cache to webpack’s require, and if it was loaded, take it from the cache. What else? Thought of adding more
conclusion
There is no specific details, but this is the main process of next. Js. If you want to understand the routing mechanism like next.