-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
React 在服务端渲染的实现 #1837
React 在服务端渲染的实现 #1837
Conversation
@sqrthree 翻译完成 |
@xx1124961758 这篇已经翻译好了,感兴趣的话可以来校对了哈。 |
@sqrthree 校对认领 |
@xx1124961758 好的呢 🍺 |
@sqrthree 校对认领 |
@CACppuccino 妥妥哒 🍻 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
总体没大问题
TODO/server-side-react-rendering.md
Outdated
|
||
React is best known as a client-side JavaScript framework, but did you know you can (and perhaps should!) render React server-side? | ||
React是最受欢迎的客户端 JavaScript 框架,但你知道吗(可以试试),你可以使用 React 在服务器端进行渲染? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[可以试试]=>[或许更应该试试]
TODO/server-side-react-rendering.md
Outdated
|
||
Suppose you've built a zippy new event listing React app for a client. The app is hooked up to an API built with your favorite server-side tool. A couple weeks later the client tells you that their pages aren't showing up on Google and don't look good when posted to Facebook. Seems solvable, right? | ||
假设你已经在客户端使用 React 构建了一个事件列表 app。该应用程序使用了您最喜欢的服务器端工具构建的API。几周后,用户告诉您,他们的页面没有显示在 Google 上,发布到 Facebook 时也显示不出来。 这些问题似乎是可以解决的,对吧? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[假设你已经在客户端使用 React 构建了一个事件列表 app。] => [假设你为一个客户构建了一个很棒的事件列表 app。]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[假设你为一个客户构建了一个很棒的事件列表 app。] => [假设你为客户构建了一个很棒的事件列表 React app。] 因为后文是 their,所以这里是指客户群体
TODO/server-side-react-rendering.md
Outdated
|
||
You figure out that to solve this you'll need to render your React pages from the server on initial load so that crawlers from search engines and social media sites can read your markup. There is evidence showing that Google sometimes executes javascript and can index the generated content, but not always. So server-side rendering is always recommended if you want to ensure good SEO and compatibility with other services like Facebook, Twitter. | ||
您会发现,要解决这个问题,需要在初始加载时从服务器渲染 React 页面,以便来自搜索引擎和社交媒体网站的爬虫工具可以读取您的标记。有证据表明,Google 有时会执行 javascript 程序并且对生成的内容进行索引,但并不总是的。因此,如果您希望确保与其他服务(如Facebook,Twitter)有良好的SEO兼容性,那么始终建议使用服务器端渲染。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[但并不总是的] => [但并不总是这样]
TODO/server-side-react-rendering.md
Outdated
|
||
You figure out that to solve this you'll need to render your React pages from the server on initial load so that crawlers from search engines and social media sites can read your markup. There is evidence showing that Google sometimes executes javascript and can index the generated content, but not always. So server-side rendering is always recommended if you want to ensure good SEO and compatibility with other services like Facebook, Twitter. | ||
您会发现,要解决这个问题,需要在初始加载时从服务器渲染 React 页面,以便来自搜索引擎和社交媒体网站的爬虫工具可以读取您的标记。有证据表明,Google 有时会执行 javascript 程序并且对生成的内容进行索引,但并不总是的。因此,如果您希望确保与其他服务(如Facebook,Twitter)有良好的SEO兼容性,那么始终建议使用服务器端渲染。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[(如Facebook,Twitter)有良好的SEO兼容性] => [(如 Facebook,Twitter)有良好的 SEO 兼容性] 中英文间的空格
TODO/server-side-react-rendering.md
Outdated
|
||
In this tutorial, we'll take you through a server side rendering example step-by-step. including working around a common roadblock for React apps that talk to APIs. | ||
在本教程中,我们将逐步介绍服务器端的呈现示例。包括围绕与API交流的React应用程序的共同路障。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
注意中英文空格
TODO/server-side-react-rendering.md
Outdated
|
||
The starter code has just one React component, hello.js, that makes an asynchronous request to the ButterCMS API and renders the returned JSON list of blog posts. ButterCMS is an API-based blog engine that's free for personal use, so it's great for testing out a real-life use case. The starter code comes hooked up with an API token, but if you want you can get your own API token by signing into ButterCMS with your GitHub account. | ||
提供的代码中只有一个 React 组件,\`hello.js\`,这个文件将向 [ButterCMS](https://buttercms.com/) 发出异步请求,并渲染返回的 JSON 列表的博文。ButterCMS 是一个基于API的博客引擎,可供个人使用,因此它非常适合测试现实生活中的用例。启动代码中连接着一个 API token,如果你想使用你自己的 API token 可以[使用你的 GitHub 账号登入 ButterCMS](https://buttercms.com/home/)。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
第二行API空格
TODO/server-side-react-rendering.md
Outdated
|
||
``` | ||
"scripts": { | ||
"start": "webpack && babel-node server.js" | ||
}, | ||
``` | ||
|
||
Browse to http://localhost:3000 to view the app. Voila! Your page is now being rendered from the server. But there's a problem. If you view the page source in the browser. You'll notice that the blog posts are still not included in the response. What's going on? If we open up the network tab in Chrome, we'll see that the API request is happening on the client. | ||
浏览 `http://localhost:3000` 查看应用程序。瞧!您的页面现在正在从服务器渲染出来了。但是有个问题, | ||
如果您在浏览器中查看页面源码,您会注意到博客文章仍未包含在回复中。这是怎么回事?如果我们在Chrome中打开网络标签,我们会看到客户端上发生API请求。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[仍未包含在回复中] => [仍未包含在响应中]
TODO/server-side-react-rendering.md
Outdated
|
||
``` | ||
"scripts": { | ||
"start": "webpack && babel-node server.js" | ||
}, | ||
``` | ||
|
||
Browse to http://localhost:3000 to view the app. Voila! Your page is now being rendered from the server. But there's a problem. If you view the page source in the browser. You'll notice that the blog posts are still not included in the response. What's going on? If we open up the network tab in Chrome, we'll see that the API request is happening on the client. | ||
浏览 `http://localhost:3000` 查看应用程序。瞧!您的页面现在正在从服务器渲染出来了。但是有个问题, | ||
如果您在浏览器中查看页面源码,您会注意到博客文章仍未包含在回复中。这是怎么回事?如果我们在Chrome中打开网络标签,我们会看到客户端上发生API请求。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[打开网络标签] => [打开网络面板]
TODO/server-side-react-rendering.md
Outdated
|
||
``` | ||
"scripts": { | ||
"start": "webpack && babel-node server.js" | ||
}, | ||
``` | ||
|
||
Browse to http://localhost:3000 to view the app. Voila! Your page is now being rendered from the server. But there's a problem. If you view the page source in the browser. You'll notice that the blog posts are still not included in the response. What's going on? If we open up the network tab in Chrome, we'll see that the API request is happening on the client. | ||
浏览 `http://localhost:3000` 查看应用程序。瞧!您的页面现在正在从服务器渲染出来了。但是有个问题, | ||
如果您在浏览器中查看页面源码,您会注意到博客文章仍未包含在回复中。这是怎么回事?如果我们在Chrome中打开网络标签,我们会看到客户端上发生API请求。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
中英文空格
TODO/server-side-react-rendering.md
Outdated
|
||
我们做到了!在服务器上使用 React 可能很棘手,尤其是从 API 获取数据时。幸运的是,React社区正在蓬勃发展,并创造了许多有用的工具。如果您对构建在客户端和服务器上渲染的大型 React 应用程序的框架感兴趣,请查看 Walmart Labs 的 [Electrode](https://github.com/electrode-io/electrode) 或 [Next.js](https://github.com/zeit/next.js)。或者如果要在 Ruby 中渲染 React ,请查看 AirBnB 的 [Hypernova](https://github.com/airbnb/hypernova) 。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
React社区 空格
@sqrthree 校对完毕 |
TODO/server-side-react-rendering.md
Outdated
|
||
Next we'll implement server side rendering so that fully generated HTML is sent to the browser. If you want to view all the changes at once, view the diff on GitHub. | ||
接下来,我们将实现服务器端渲染,以便将完全生成的HTML发送到浏览器。如果要同时查看所有更改,请查看[GitHub上的差异](https://github.com/ButterCMS/react-ssr-example/commit/525c625b0f65489050983ed03b52bb7770ce6b7a)。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
请查看[Github上的差异] ---> 请在 Github 上查看文件的差异
这样改下会不会好点儿?
TODO/server-side-react-rendering.md
Outdated
ReactDOMServer.renderToString(<Hello />); | ||
``` | ||
|
||
This returns the HTML for the Hello component, which we inject into the HTML of index.html to generate the full HTML for the page on the server. | ||
这将返回 Hello 组件的 HTML ,我们将其注入到 index.html 的 HTML 中,从而生成服务器上页面的完整 HTML 。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这将返回 Hello 组件的 HTML,我们将其注入到 index.html 的 HTML 中,从而生成服务器上页面的完整 HTML 。
注:HTML与,之间没有空格
@sqrthree 按照校对者提出的建议修改完成,请验收 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
还有一些小问题需要调整下哈,辛苦啦。
TODO/server-side-react-rendering.md
Outdated
@@ -1,34 +1,36 @@ | |||
|
|||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
文件顶部的空行去掉吧,应该是失误导致。
TODO/server-side-react-rendering.md
Outdated
> * 原文地址:[Server-Side React Rendering](https://css-tricks.com/server-side-react-rendering/) | ||
> * 原文作者:[Roger Jin](https://css-tricks.com/author/rogerjin/) | ||
> * 译文出自:[掘金翻译计划](https://github.com/xitu/gold-miner) | ||
> * 本文永久链接:[https://github.com/xitu/gold-miner/blob/master/TODO/server-side-react-rendering.md](https://github.com/xitu/gold-miner/blob/master/TODO/server-side-react-rendering.md) | ||
> * 译者: | ||
> * 译者:[牧云云](https://github.com/MuYunyun) | ||
> * 校对者: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
校对者信息别忘了加上哈。
TODO/server-side-react-rendering.md
Outdated
> * 校对者: | ||
|
||
# Server-Side React Rendering | ||
# React 在服务端渲染的实现 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
麻烦请检查下这个是属于什么问题?文中多次出现了。预览链接
TODO/server-side-react-rendering.md
Outdated
|
||
Next we'll implement server side rendering so that fully generated HTML is sent to the browser. If you want to view all the changes at once, view the diff on GitHub. | ||
接下来,我们将实现服务器端渲染,以便将完全生成的HTML发送到浏览器。如果要同时查看所有更改,请在 [GitHub](https://github.com/ButterCMS/react-ssr-example/commit/525c625b0f65489050983ed03b52bb7770ce6b7a) 上查看文件的差异。 | ||
|
||
To get started, we'll install Express, a Node.js server side application framework: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
遗漏的英文。
TODO/server-side-react-rendering.md
Outdated
|
||
To fix this, we need to make sure the API request completes before the Hello component is rendered. This means making the API request outside of React's component rendering cycle and fetching data before we render the component. We'll take you through this step-by-step, but you can view the complete diff on GitHub. | ||
要解决这个问题,我们需要在渲染 Hello 组件之前确保 API 请求完成。这意味着要使 API 请求跳出 React 的组件渲染循环,并在渲染组件之前获取数据。我们将逐步介绍这一步,但您可以在[GitHub上查看完整的差异]()https://github.com/ButterCMS/react-ssr-example/commit/5fdd453e31ab08dfdc8b44261696d4ed89fbb719。 | ||
|
||
To move data fetching before rendering, we'll install react-transmit: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
还有英文呢。
TODO/server-side-react-rendering.md
Outdated
|
||
To fix this, we need to make sure the API request completes before the Hello component is rendered. This means making the API request outside of React's component rendering cycle and fetching data before we render the component. We'll take you through this step-by-step, but you can view the complete diff on GitHub. | ||
要解决这个问题,我们需要在渲染 Hello 组件之前确保 API 请求完成。这意味着要使 API 请求跳出 React 的组件渲染循环,并在渲染组件之前获取数据。我们将逐步介绍这一步,但您可以在[GitHub上查看完整的差异]()https://github.com/ButterCMS/react-ssr-example/commit/5fdd453e31ab08dfdc8b44261696d4ed89fbb719。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个链接的语法错了 =。= 另外,『GitHub』前后请加一个空格。
TODO/server-side-react-rendering.md
Outdated
|
||
我们做到了!在服务器上使用 React 可能很棘手,尤其是从 API 获取数据时。幸运的是,React 社区正在蓬勃发展,并创造了许多有用的工具。如果您对构建在客户端和服务器上渲染的大型 React 应用程序的框架感兴趣,请查看 Walmart Labs 的 [Electrode](https://github.com/electrode-io/electrode) 或 [Next.js](https://github.com/zeit/next.js)。或者如果要在 Ruby 中渲染 React ,请查看 AirBnB 的 [Hypernova](https://github.com/airbnb/hypernova) 。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
『AirBnB』的写法应该为『Airbnb』,参考自官网。
@sqrthree 已经修复 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
还有一些小问题啦
TODO/server-side-react-rendering.md
Outdated
|
||
Next we'll implement server side rendering so that fully generated HTML is sent to the browser. If you want to view all the changes at once, view the diff on GitHub. | ||
接下来,我们将实现服务器端渲染,以便将完全生成的HTML发送到浏览器。如果要同时查看所有更改,请在 [GitHub](https://github.com/ButterCMS/react-ssr-example/commit/525c625b0f65489050983ed03b52bb7770ce6b7a) 上查看文件的差异。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
『HTML』前后没有空格
TODO/server-side-react-rendering.md
Outdated
|
||
You figure out that to solve this you'll need to render your React pages from the server on initial load so that crawlers from search engines and social media sites can read your markup. There is evidence showing that Google sometimes executes javascript and can index the generated content, but not always. So server-side rendering is always recommended if you want to ensure good SEO and compatibility with other services like Facebook, Twitter. | ||
您会发现,要解决这个问题,需要在初始加载时从服务器渲染 React 页面,以便来自搜索引擎和社交媒体网站的爬虫工具可以读取您的标记。有证据表明,Google 有时会执行 javascript 程序并且对生成的内容进行索引,但并不总是这样。因此,如果您希望确保与其他服务(如 Facebook,Twitter)有良好的SEO兼容性,那么始终建议使用服务器端渲染。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 『Facebook,Twitter』=>『Facebook、Twitter』
- 『SEO』前后加个空格哈
TODO/server-side-react-rendering.md
Outdated
|
||
Let's go through how to add server-side rendering to a basic client rendered React app with Babel and Webpack. Our app will have the added complexity of getting the data from a third-party API. We've provided starter code on GitHub where you can see the complete example. | ||
接下来让我们来看看如何将服务器端渲染添加到一个基本的客户端渲染的使用 Babel 和 Webpack 的 React 应用程序中。我们的应用程序将会因从第三方 API 获取数据而变得有点复杂。我们在GitHub上提供了[相关代码](https://github.com/ButterCMS/react-ssr-example/releases/tag/starter-code),您可以在其中看到完整的示例。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
『GitHub』前后没有空格
TODO/server-side-react-rendering.md
Outdated
|
||
``` | ||
``` js | ||
const document = data.replace(/<div id="app"><\/div>/,`<div id="app">${html}</div>`); | ||
``` | ||
|
||
To start the server, update the start script in package.json and then run npm run start: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
还有英文没删除
已经 merge 啦~ 快快麻溜发布到掘金专栏然后给我发下链接,方便及时添加积分哟。 |
1 |
issue链接1825