Follow the tutorial commit-by-commit, to see the server-side rendering drama unfold with a happy ending!
RUN: npm run start
and then open index.html in the browser.
Webpack helps us to bundle our code with dependencies from npm(such as React), and then transforms the code with Babel, to makeit compatible with ES5.
RUN: npm run start
and go to http://localhost:3000
.
Now we are rendering the same Hello component both on client and server:with express server we pre-render the Hello component on the server, andserver the client with rendered html, and with webpack we continue tobundle client.js into ES5 code that the browser would understand,just as we did at previous step.
Now lets learn to deal with styles. We configure webpack loaders tosupport loading CSS files. This is cool, but there comes one problemwith server-side rendering: styles won't be loaded until all of JS loads,so no styles for devices without JS.
Let's fix this problem with webpack's ExtractTextPlugin plugin: itextracts all CSS styles into one CSS file that we can serve to our client,so our page will instantly look perfectly styled, even without JS.
Everybody loves CSS modules, and the great news is that they come free with Webpack.The bad news is that we can't use them with server-side rendering, as we don't useWebpack when rendering on the server-side.
So at this step we broke everything, and the only way to continue from here, is tostart using Webpack to pre-build code for server-side rendering too, and that'swhat we'll do at the next step.
To save our issue with CSS modules, we make Webpack to render bothour client and our server side code. The best way to do it is touse Webpack's abillity to handle array of configs.
With first config we transform our client-side code (client.js
),just as we were doing before. But with the second config we transformhandleRender
function that we have extracted into serverEntry.js
,so now our server-side rendering code gets processed by Webpack too.There we use css-loader/locals as a CSS loader, to just get class names fromCSS modules, as that's all we need to render html on the server.Also notice how we use target:node
and nodeExternals
.
Great! Now our build is fixed, so we can use CSS modules both during clientand server rendering.
Now let's fetch some data asyncronously. We'll use isomorphic-fetch,as it works both on client and server our of the box.
Fetching data works just fine on the server, but the problem is thaton the server we didn't wait for fetch to finish fetching the databefore sending the response, so our pre-rendered html doesn't have anyasync data when it arrives.Let's try to fix it in the next step.
There are multiple ways to solve async rendering issue. Here we'lluse react-transmit to declaratively define our data dependencies per component,and return rendered html only when all data is resolved.
It works even with nested components, constructing the single promises tree,which is qute cool. It is inspired by Facebook Relay, so if you are familiarwith it, you'll feel right at home.
Got more tips or challenges with server-side rendering of React? Submit a PR!
优点 1. SEO 客户端渲染,页面中只有初始的几个html容器,js生成内容填充到容器中, 爬虫只能识别到初始的html容器,js生成的内容一般不会被识别,而服务端 渲染直接给出html,爬虫可以识别到所有内容 2. 首屏渲染性能 第一次加载过慢,很多客户端渲染的js有一定体积,得等到js加载解析完成才能显示首屏内容,影响体验 缺点 服务端渲染会对服务端造成一定的并发压力 如果使用nodejs进
Akka-http针对Connection的两头都提供了方便编程的Api,分别是Server-Side-Api和Client-Side-Api。通过这两个Api可以大大提高编程效率。当然,上期我们提到过,Http-Server是Akka-http的核心部分,所有系统集成功能都是在Server-Side实现的。Akka-http-Server-Side-Api可以说是最先进的Http-Server
Pasted from: http://weblogs.asp.net/bleroy/archive/2005/12/01/432016.aspx One of the most common mistakes beginner ASP.NET developers make is to call MsgBox.Show from their ASP.NET server-side code.
Server-side processing There are times when reading data from the DOM is simply too slow or unwieldy, particularly when dealing with thousands or millions of data rows. To address this DataTables' ser
The most popular mode for Nuxt. With SSR, also called "universal" or "isomorphic" mode, a Node.js server will be used to deliver HTML based on your Vue components to the client instead of the pure jav
General troubleshooting advice Verify that that your JavaScript configuration is correct for the relevant TinyMCE plugin. Ensure that your firewall has the appropriate ports and rules configured corre
Creating a configuration file Note: Use a plain text editor (such as gedit, vim, emacs or notepad) when creating or editing the application.conf file. Do not use word processors like Microsoft Word or
Server-side component installation Some TinyMCE Enterprise features require a server-side component. If you’re using the TinyMCE cloud solution, we’ve done all the hard work for you and you can skip t
浏览器发送特定的HTTP标头,用于从内部XMLHttpRequest或获取API发起的跨站点请求。它还希望看到具有跨站点响应的特定HTTP标头。HTTP访问控制(CORS)文章中提供了这些头文件的概述,包括启动请求和处理来自服务器的响应的示例JavaScript代码以及对每个头文件的讨论,应该将其作为配套文章这个。本文介绍处理访问控制请求和制定访问控制响应在PHP中。本文的目标受众是服务器程序员或
我们研究了页面生命周期以及页面如何包含各种控件。 页面本身被实例化为控制对象。 所有Web表单基本上都是ASP.NET Page类的实例。 页面类具有以下与内部对象对应的非常有用的属性: Session Application Cache Request Response Server User Trace 我们将在适当的时候讨论这些对象。 在本教程中,我们将探索Server对象,Request对