简体   繁体   English

SSR 与 CSR 解释

[英]SSR vs CSR explanation

I've been working as a full stack web developer for over a year now.我作为一名全栈 web 开发人员已经工作了一年多了。 Nextjs/golang is our stack. Nextjs/golang 是我们的堆栈。 Today I realized that I have no idea whether we use CSR or SSR.今天我意识到我不知道我们是使用 CSR 还是 SSR。 I think we use CSR but I'm not sure.我想我们使用 CSR,但我不确定。 I've gone down rabbitholes on this topic like 100 times but its never stuck.我已经在这个话题上陷入困境 100 次了,但从来没有卡住。

First question: When they say server side, do they mean on the backend?第一个问题:当他们说服务器端时,是指后端吗? ie golang?即戈朗? or does that mean on the nextjs server?或者这是否意味着在 nextjs 服务器上? Whenever someone says server I think backend but I don't think this is correct.每当有人说服务器时,我认为是后端,但我认为这是不正确的。

Second question: How does client side rendering work?第二个问题:客户端渲染是如何工作的? Like I know the server sends javascript to the client then the client uses the javascript to build the page.就像我知道服务器向客户端发送 javascript 然后客户端使用 javascript 来构建页面。 But all of this javascript must make a bunch of requests to the server to grab data and html right?但是这一切 javascript 必须向服务器发出一堆请求来获取数据和 html 对吗? Like how would the javascript build the page otherwise?否则 javascript 将如何构建页面? Is this why react/nextjs is written in jsx?这就是为什么 react/nextjs 是用 jsx 写的吗? So the server can send all the JSX which is actually just javascript to the client then the client can build the html?所以服务器可以将所有 JSX 发送给客户端,实际上只是 javascript 然后客户端可以构建 html?

Third Question: If CSR has the client build the page, how would this ever work?第三个问题:如果 CSR 让客户构建页面,这将如何运作? Like what about all of the data that needs to be pulled from our database for specific users / etc etc. That can't be done directly from the frontend.就像需要从我们的数据库中为特定用户/等等提取的所有数据一样。这不能直接从前端完成。

I tried reading tons of articles online!我尝试在线阅读大量文章! Hasn't clicked yet还没有点击

You said the essential thing yourself: in client-side rendering, "the server sends javascript to the client, then the client uses the javascript to build the page."您自己说了本质的事情:在客户端呈现中, “服务器向客户端发送 javascript,然后客户端使用 javascript 构建页面。” Hold on to that one point – everything else is secondary.坚持这一点——其他一切都是次要的。 The client "renders."客户端“呈现”

Now that "client-side" rendering capabilities have become so powerful – and, so varied – it has generally become favored.现在,“客户端”渲染功能变得如此强大——而且如此多样——它通常受到青睐。 If you "tell the client what to do and then let him do it," you are more likely to get a consistently favorable outcome for most clients.如果你“告诉客户该做什么,然后让去做”,你就更有可能获得对大多数客户都有利的结果。 Yes, the client will issue perhaps-many AJAX requests in carrying out your instructions.是的,客户端在执行您的指令时可能会发出许多 AJAX 请求。 That is irrelevant.那是无关紧要的。

CSR - server sends HTML that contains links to javascript ( script tags). CSR - 服务器发送 HTML,其中包含指向 javascript( script标签)的链接。 Clients then loads and executes JS (JavaScript typically contains fetching code).然后客户端加载并执行 JS(JavaScript 通常包含获取代码)。 That means that each client will perform several round trips to the server to get HTML and then the data.这意味着每个客户端将执行多次到服务器的往返以获取 HTML 然后是数据。

SSR - server sends HTML and embeds the necessary data in it SSR - 服务器发送 HTML 并在其中嵌入必要的数据在此处输入图像描述

The client already has the data and HTML, so it can render it.客户端已经有了数据和 HTML,所以它可以渲染它。 SSR does fetch on each request, meaning the client still gets the latest data. SSR 会在每次请求时获取数据,这意味着客户端仍会获取最新数据。 Benefits of using SSR compared to CSR is lower load time, it makes the website feel "faster" and also improves the ranking by search engine bots.与 CSR 相比,使用 SSR 的好处是加载时间更短,它使网站感觉“更快”,还提高了搜索引擎机器人的排名。 On the other hand, the server does the rendering, which increases its burden (though fewer requests decreases it).另一方面,服务器进行渲染,这增加了它的负担(尽管更少的请求减少了它)。

SSG is the same as SSR but fetching occurs at build time , the result page is computed only once and returned for each request. SSG 与 SSR 相同,但提取发生在构建时,结果页面仅计算一次并为每个请求返回。 It is possible to use SSG with or without data.可以在有或没有数据的情况下使用 SSG。

Use SSG if possible, then mostly SSR.如果可能,使用 SSG,然后主要使用 SSR。 In some occasions it may be better to use CSR instead of SSR, though I'm not experienced enough to give the answer when.在某些情况下,使用 CSR 而不是 SSR 可能更好,尽管我没有足够的经验来给出答案。

Now answering your questions:现在回答你的问题:

  1. Yes, SSR happens on the server.是的,SSR 发生在服务器上。 If you use fetch function then it will work on client.如果您使用fetch function 那么它将在客户端上运行。 But if you use getServerSideProps or getStatisSideProps then it will work on server.但是,如果您使用getServerSidePropsgetStatisSideProps ,那么它将在服务器上运行。 You can read from the file system, fetch public API or query the database, whatever you do in getStatisSideProps , getServerSideProps will run on the server, before returning the response.您可以从文件系统读取, fetch public API 或查询数据库,无论您在getStatisSideProps中做什么, getServerSideProps都会在服务器上运行,然后返回响应。

  2. Yes, you're correct.是的,你是对的。 Client need the data to render the page, so it has to send requests to server and then render.客户端需要数据来渲染页面,所以它必须向服务器发送请求然后渲染。

  3. The third question is the same as the second.第三个问题与第二个问题相同。 I hope the long answer I gave clarified your confusion.我希望我给出的长答案能澄清你的困惑。

Sorry for long answer.很抱歉回答很长。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM