What is server side rendering of javascript?

前端 未结 3 1755
囚心锁ツ
囚心锁ツ 2021-02-02 10:23

Some javascript frameworks like Dust.js claim that they ALSO support server-side rendering (in addition to client side rendering) as well. Can someone explain how does this wor

相关标签:
3条回答
  • 2021-02-02 10:30

    The 3 main renders methods are:

    Client Side Rendering (CSR)

    Is the default rendering process of modern frontend frameworks like ReactJS, the server just returns an almost empty index.html file, and then the user browser using Javascript loads and builds everything.

    Server Side Rendering (SSR):

    Refers to the process of generating in the server, the HTML content, on-demand for each request. Usually, you’ll accomplish this by setting up a server running something like Express or Next.js (which use NodeJS) that renders your React app with each request — just as with a more traditional PHP or Rails based website.

    Static Site Generator (SSG):

    Both Server Side Rendering and Static Site Generator involve generating HTML for each of your site’s URLs. The difference is that Static rendering happens once at build time, while server rendering happens on-demand, as the user requests each file.

    With static rendering, you need to generate responses for every possible request ahead of time. But what if you’re building something where you can’t predict all possible requests, like a search page? Or what if you have a lot of user generated content, and the response changes on every request? In that case, you’ll need Server Side Rendering.

    *** So which should you use? ***

    It depends.

    if SEO is irrelevant — e.g. an app that lives behind a login screen — then CSR is fine and you just need something like ReactJS

    If you need a good SEO:

    a) If you can predict the content (to generate it in build time) then you need SSG and should choose something like NextJS

    b) If you can’t predict the content/possible request, the server will need to generate the pages on demand, so you need SSR and should choose something like NextJS.

    Note: NextJS allows you to selectively mix in the same project the 3 main rendering forms. They are also other good solutions like Gatsby on the market.

    0 讨论(0)
  • 2021-02-02 10:35

    Server side rendering is converting JavaScript into static html and css. Earlier JS tended to load last to optimise website performance. But the problem was it affected SEO. So server side rendering became the solution to resolve this issue.

    0 讨论(0)
  • 2021-02-02 10:37

    JavaScript can be run on servers using systems like Node.js.

    With regard to Dust.js, a templating engine, it can generate hypertext and HTML on the server and send that content directly to the client's browser. This is typically used to avoid a flash of temporarily empty templates, caused by the browser requiring a split second to populate a view's templates via a framework like Dust.js. The downside is that the page will take slightly longer to load because more work has to be done on the server before sending data to the client.

    Check out this question for the pros and cons of server-side rending. One must choose between slow post-processing (requiring the user's browser to do the work) or slow pre-processing (making the server do the work before the user sees anything).

    0 讨论(0)
提交回复
热议问题