- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试为我的应用实现服务器端渲染。它曾经是使用 ReactDom
的客户端渲染。在客户端渲染中一切正常,但现在当我将其设为服务器端渲染时,没有渲染任何 CSS 表和 UI 库(Reactstrap + Material UI)。用户界面完全崩溃。
我正在将我的 App.js(包含所有 UI 的实际前端页面)注入(inject)到我的 html.js(模板)中
我的服务器:
import express from 'express';
import React from 'react';
import { renderToString } from 'react-dom/server'
import App from '../../src/App'
import Html from '../../src/html.js'
const app = express();
// console.log that your server is up and running
app.listen(3001, () => console.log(`Listening on port 3001`));
app.get('/',(req,res) => {
const title="2FA SDK"
const body = renderToString(<App/>)
res.send(
Html({
body,
title,
})
)
})
我的 html.js(只是一个简单的模板):
const Html = ({ body, styles, title }) => `
<!DOCTYPE html>
<html>
<head>
<title>${title}</title>
</head>
<body style="margin:0">
<div id="app">${body}</div>
</body>
</html>
`;
我的 app.js(使用 material ui,最小)
render(){
return(
<div className="container">
<div>
<TextField
label="Application Name"
type="text"
variant="outlined"
onChange={this.handleApplicationName}
/>
</div>
<div>
<FormLabel>Phone</FormLabel>
<div>
<OutlinedInput
value={this.state.textmaskArray[2]}
onChange={(e) => this.handlePhoneNumber(e,2)}
labelWidth={200}
/>
</div>
</div>
<Button variant="contained" color="primary" onClick={this.props.buttonAction}>
</Button>
</div>
使用 reactstrap 的 App.js 部分:
render(){
return(
<CardControl
title={"Code Authention"}
message={"For your security, we need to verify your identity by sending a code to your phone number"}
>
<Container>
<Row>
<Col>
<div>{this.props.phoneList}</div>
</Col>
<Col>
<div>{this.props.methodList}</div>
</Col>
</Row>
<Button className="sm-2" disabled={this.props.disabled} color="info" onClick={this.props.buttonAction}>{this.props.buttonName}</Button>
</Container>
</CardControl>
) }我的 webpack.config.js:
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const HtmlWebPackPlugin = require("html-webpack-plugin");
const nodeExternals = require("webpack-node-externals");
module.exports = [
{
/*Config for backend code*/
entry: './src/server/server.js',
target: 'node',
output: {
filename: 'server.js'
},
externals: [nodeExternals()],
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.html$/,
use: {
loader: "html-loader",
options: { minimize: true }
}
},
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader,"css-loader"]
}
]
},
plugins: [
new HtmlWebPackPlugin({
template: "./public/index.html",
filename:"./index.html"
}),
new MiniCssExtractPlugin({
filename: "[name].css",
chunkFilename:"[id].css"
})
]
},
]
我正在使用什么:Create-react-app
我尝试过的:我安装了 CSS 加载器,但它根本不起作用。我还查阅了 material-ui 关于服务器端渲染的文档,我觉得我只是遗漏了一小部分,他们提供的说明可能有点矫枉过正。
我需要什么帮助:我想弄清楚为什么来自 material-ui 或 bootstrap 的 UI 组件根本没有得到渲染。我怀疑 nodeExternal 排除了它们,但我不确定。
最佳答案
您需要在客户端对呈现的 HTML 进行合成,以便让 React 接管。
创建一个 client.js 文件:
import React from 'react';
import ReactDOM from 'react-dom';
ReactDOM.hydrate(
<App/>,
document.querySelector('#app'),
);
请注意,hydrate 应该与 renderToString
呈现的内容相匹配。
接下来将其添加为您的 webpack 配置中的附加条目:
module.exports = [
{
/*Config for backend code*/
entry: './src/server/server.js',
target: 'node',
output: {
filename: 'server.js'
},
externals: [nodeExternals()],
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.html$/,
use: {
loader: "html-loader",
options: { minimize: true }
}
},
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader,"css-loader"]
}
]
},
plugins: [
new HtmlWebPackPlugin({
template: "./public/index.html",
filename:"./index.html"
}),
new MiniCssExtractPlugin({
filename: "[name].css",
chunkFilename:"[id].css"
})
]
},
{
entry: './client.js',
output: {
filename: 'bundle.js',
},
module: {
rules: [ {
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
},
],
},
]
更改您的 html 呈现代码以包含此内容:
const Html = ({ body, styles, title }) => `
<!DOCTYPE html>
<html>
<head>
<title>${title}</title>
</head>
<body style="margin:0">
<div id="app">${body}</div>
<script async src="/bundle.js"></script>
</body>
</html>
`;
我不是 100% 确信这个确切的代码是否有效,但这是一般的想法。
关于javascript - 服务器端渲染不渲染UI库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54221037/
我在使用 Jade 和 express 时遇到了一些问题。这是 serder 端: router.get('/login', function (req, res) { res.status(2
是否可以使用 JavaScript 服务器端,从而在某些核心计算中不会显示在页面源代码中? 我正在开发一款游戏,代码的一个关键部分只需要在服务器端计算然后传回客户端。 我的整个游戏都是用 Javasc
我正在寻找有关如何使用 ExtJS 4 实现安全页面的信息。我所说的安全页面是指用户将使用 Siteminder (SSO) 登录我们的网站,因此我们将拥有用户的身份。然后我们将通过进行数据库/LDA
我的 Centos 7 服务器正在运行 apache 2.4.6,并且正在使用 mod_wsgi 提供 django webapp。我的问题是我无法从另一台计算机的浏览器访问服务器 url。我没有从
我们收到了客户的请求,要求我们基于 ExtJS 框架构建 Web 应用程序。我查看了互联网,发现 ExtJs 只是一个客户端 javascript 控件,但我认为 Web 应用程序也必须具有服务器端
我有三个组件。 组件一包含组件二和三。 组件二是一个列表。 组件三用于向数据库添加项目。 当我将一个项目保存到数据库时,我想更新组件二中的列表。 我怎么做? 最佳答案 设想 让我们假设:
欢迎, 我正在寻找能使我以尽可能高的格式下载youtube视频的youtube api。 几年前,这项工作更为简单,因为url拥有关于质量的信息,例如“fmt = 22”或“fmt = 6”,我们现在
我想将电子邮件 x@x.com 的用户密码设置为“an”。但代码不起作用。这是我的云代码: Parse.Cloud.define('testSetPasswordForUser', functio
正在阅读http://cocoawithlove.com/2010/03/streaming-mp3aac-audio-again.html这篇文章,想知道如何在服务器端实现它,是否像将文件放在htd
我有一个使用一些 css/javascript 选项卡的 php 脚本,它们在我的本地服务器上运行,但当我上传到我的在线服务器时则不起作用。 只是想知道是否有人知道为什么会出现这种情况?所有路径都
是否可以从 JavaScript 脚本获取服务器端页面的源代码?我希望能够获取服务器上同一文件夹中的页面的源代码。除了javascript之外,是否可以不使用任何其他东西? 最佳答案 如果您想从 ja
.NET 4.0 我正在寻找在我们的服务器上生成Word文档的最简单方法。 局限性: 服务器端 我不想在服务器上安装word 数据源是XML 我试图用XSLT生成快速简单的DOCX,但是我可以找到的用
我正在使用 native jQuery 验证库来验证在联系表单中输入的电子邮件地址。由于这是一个表达式引擎站点,因此我使用其电子邮件验证器作为服务器端备份。 当我输入 test@b.c 时,jQuer
我使用带有服务器端处理的数据表来显示数万行。我需要通过复选框过滤这些数据。我能够制作一个工作正常的复选框,但我不知道如何添加多个复选框以协同工作。我找到了 similar solution在这里,但我
我正在尝试编写一个消息传递应用程序,并且我能够发送消息(显示为服务器客户端正确显示消息)但随后将我的客户端踢出服务器。服务器打印以下错误: java.io.EOFException at java.i
如果设备是移动设备,如何防止侧边栏加载服务器端资源?我了解如何通过 CSS 隐藏,但我更感兴趣的是防止对服务器的调用。 最佳答案 WordPress有一个名为wp_is_mobile()的函数它将检查
我有一个返回 text/event-stream 数据的网址,因此我尝试连接并打印我找到的所有内容: var url = "..." var source = new EventSource(url)
我得到这样的错误列表:{ error: [ "Email is required", "First Name is required"] } 我需要如何修改它,以获取包含字段名称的列表? public
我正在尝试使用 ASIFormDataRequest 将数据发送到 ASP.net 服务器端。我创建了一个aspx页面。目前我可以得到这两个纯文本。但是我不知道如何通过 Request.Form 在
我在 ${host}/api/graphql 有一个可通过 POST 访问的快速 graphql 端点。 在那条路线上,我有身份验证中间件,如果用户未登录,我想重定向到登录页面。 看起来有点像这样。
我是一名优秀的程序员,十分优秀!