在每一毫秒都至关重要的世界里,服务器端渲染已成为前端应用程序的一项基本功能。
本指南将引导您了解使用 React 构建可用于生产的 SSR 的基本模式。您将了解具有内置 SSR(例如 Next.js)的基于 React 的框架背后的原理,并学习如何创建自己的自定义解决方案。
提供的代码是生产就绪的,具有客户端和服务器部分的完整构建过程,包括 Dockerfile。在此实现中,Vite 用于构建客户端和 SSR 代码,但您可以使用您选择的任何其他工具。 Vite还为客户端提供了开发模式下的热重载。
如果您对此设置的无 Vite 版本感兴趣,请随时与我们联系。
目录
- 什么是SSR
-
创建应用程序
- 初始化 Vite
- 更新 React 组件
- 创建服务器
- 配置构建
- 路由
- 码头工人
- 结论
什么是SSR
服务器端渲染 (SSR) 是 Web 开发中的一种技术,服务器在将网页发送到浏览器之前生成网页的 HTML 内容。与传统的客户端渲染 (CSR) 不同,JavaScript 在加载空 HTML shell 后在用户设备上构建内容,SSR 直接从服务器提供完全渲染的 HTML。
SSR 的主要优点:
- 改进的 SEO:由于搜索引擎爬虫接收完全渲染的内容,SSR 可确保更好的索引和 排名。
- 更快的首次绘制:用户几乎立即看到有意义的内容,因为服务器处理了繁重的工作 渲染。
- 增强性能:通过减少浏览器上的渲染工作量,SSR 为 使用较旧或功能较弱的设备的用户。
- 无缝服务器到客户端数据传输:SSR 允许您将动态服务器端数据传递到客户端,而无需 重建客户端包。
创建应用程序
使用 SSR 的应用程序流程遵循以下步骤:
- 阅读模板 HTML 文件。
- 初始化 React 并生成应用内容的 HTML 字符串。
- 将生成的 HTML 字符串插入模板中。
- 将完整的 HTML 发送到浏览器。
- 在客户端,匹配 HTML 标签并水合应用程序,使其具有交互性。
初始化Vite
我更喜欢使用pnpm和react-swc-ts Vite模板,但你可以选择任何其他设置。
pnpm create vite react-ssr-app --template react-swc-ts
安装依赖项:
pnpm create vite react-ssr-app --template react-swc-ts
更新 React 组件
在典型的 React 应用程序中,index.html 有一个 main.tsx 入口点。使用 SSR,您需要两个入口点:一个用于服务器,一个用于客户端。
服务器入口点
Node.js 服务器将运行您的应用程序并通过将 React 组件渲染为字符串 (renderToString) 来生成 HTML。
pnpm install
客户端入口点
浏览器将水合服务器生成的 HTML,将其与 JavaScript 连接以使页面具有交互性。
Hydration 是将事件侦听器和其他动态行为附加到服务器呈现的静态 HTML 的过程。
// ./src/entry-server.tsx import { renderToString } from 'react-dom/server' import App from './App' export function render() { return renderToString(<app></app>) }
更新index.html
更新项目根目录中的index.html 文件。 ;占位符是服务器将注入生成的 HTML 的位置。
// ./src/entry-client.tsx import { hydrateRoot } from 'react-dom/client' import { StrictMode } from 'react' import App from './App' import './index.css' hydrateRoot( document.getElementById('root')!, <strictmode> <app></app> </strictmode>, )
服务器所需的所有依赖项都应作为开发依赖项(devDependency)安装,以确保它们不包含在客户端捆绑包中。
接下来,在项目的根目录中创建一个名为 ./server 的文件夹并添加以下文件。
重新导出主服务器文件
重新导出主服务器文件。这使得运行命令更加方便。
<meta charset="UTF-8"> <link rel="icon" type="image/svg+xml" href="/vite.svg"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Vite + React + TS</title> <div> <h3> Create Server </h3> <p>First, install the dependencies:<br> </p> <pre class="brush:php;toolbar:false">pnpm install -D express compression sirv tsup vite-node nodemon @types/express @types/compression
定义常量
HTML_KEY常量必须与index.html中的占位符注释匹配。其他常量管理环境设置。
// ./server/index.ts export * from './app'
创建 Express 服务器
为开发和生产环境设置不同配置的 Express 服务器。
// ./server/constants.ts export const NODE_ENV = process.env.NODE_ENV || 'development' export const APP_PORT = process.env.APP_PORT || 3000 export const PROD = NODE_ENV === 'production' export const HTML_KEY = `<!--app-html-->`
开发模式配置
开发中,使用Vite的中间件处理请求,并通过热重载动态转换index.html文件。服务器将加载 React 应用程序并将其渲染为每个请求的 HTML。
// ./server/app.ts import express from 'express' import { PROD, APP_PORT } from './constants' import { setupProd } from './prod' import { setupDev } from './dev' export async function createServer() { const app = express() if (PROD) { await setupProd(app) } else { await setupDev(app) } app.listen(APP_PORT, () => { console.log(`http://localhost:${APP_PORT}`) }) } createServer()
生产模式配置
在生产中,使用压缩来优化性能,使用 Sirv 来提供静态文件,并使用预构建的服务器包来渲染应用程序。
// ./server/dev.ts import { Application } from 'express' import fs from 'fs' import path from 'path' import { HTML_KEY } from './constants' const HTML_PATH = path.resolve(process.cwd(), 'index.html') const ENTRY_SERVER_PATH = path.resolve(process.cwd(), 'src/entry-server.tsx') export async function setupDev(app: Application) { // Create a Vite development server in middleware mode const vite = await ( await import('vite') ).createServer({ root: process.cwd(), server: { middlewareMode: true }, appType: 'custom', }) // Use Vite middleware for serving files app.use(vite.middlewares) app.get('*', async (req, res, next) => { try { // Read and transform the HTML file let html = fs.readFileSync(HTML_PATH, 'utf-8') html = await vite.transformIndexHtml(req.originalUrl, html) // Load the entry-server.tsx module and render the app const { render } = await vite.ssrLoadModule(ENTRY_SERVER_PATH) const appHtml = await render() // Replace the placeholder with the rendered HTML html = html.replace(HTML_KEY, appHtml) res.status(200).set({ 'Content-Type': 'text/html' }).end(html) } catch (e) { // Fix stack traces for Vite and handle errors vite.ssrFixStacktrace(e as Error) console.error((e as Error).stack) next(e) } }) }
配置构建
要遵循构建应用程序的最佳实践,您应该排除所有不必要的包并仅包含应用程序实际使用的包。
更新Vite配置
更新您的 Vite 配置以优化构建过程并处理 SSR 依赖项:
// ./server/prod.ts import { Application } from 'express' import fs from 'fs' import path from 'path' import compression from 'compression' import sirv from 'sirv' import { HTML_KEY } from './constants' const CLIENT_PATH = path.resolve(process.cwd(), 'dist/client') const HTML_PATH = path.resolve(process.cwd(), 'dist/client/index.html') const ENTRY_SERVER_PATH = path.resolve(process.cwd(), 'dist/ssr/entry-server.js') export async function setupProd(app: Application) { // Use compression for responses app.use(compression()) // Serve static files from the client build folder app.use(sirv(CLIENT_PATH, { extensions: [] })) app.get('*', async (_, res, next) => { try { // Read the pre-built HTML file let html = fs.readFileSync(HTML_PATH, 'utf-8') // Import the server-side render function and generate HTML const { render } = await import(ENTRY_SERVER_PATH) const appHtml = await render() // Replace the placeholder with the rendered HTML html = html.replace(HTML_KEY, appHtml) res.status(200).set({ 'Content-Type': 'text/html' }).end(html) } catch (e) { // Log errors and pass them to the error handler console.error((e as Error).stack) next(e) } }) }
更新 tsconfig.json
更新 tsconfig.json 以包含服务器文件并适当配置 TypeScript:
pnpm create vite react-ssr-app --template react-swc-ts
创建 tsup 配置
使用 TypeScript 捆绑器 tsup 来构建服务器代码。 noExternal 选项指定要与服务器捆绑的包。 请务必包含您的服务器使用的任何其他软件包。
pnpm install
添加构建脚本
// ./src/entry-server.tsx import { renderToString } from 'react-dom/server' import App from './App' export function render() { return renderToString(<app></app>) }
运行应用程序
开发:使用以下命令以热重载启动应用程序:
// ./src/entry-client.tsx import { hydrateRoot } from 'react-dom/client' import { StrictMode } from 'react' import App from './App' import './index.css' hydrateRoot( document.getElementById('root')!, <strictmode> <app></app> </strictmode>, )
生产:构建应用程序并启动生产服务器:
<meta charset="UTF-8"> <link rel="icon" type="image/svg+xml" href="/vite.svg"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Vite + React + TS</title> <div> <h3> Create Server </h3> <p>First, install the dependencies:<br> </p> <pre class="brush:php;toolbar:false">pnpm install -D express compression sirv tsup vite-node nodemon @types/express @types/compression
要验证 SSR 是否正常工作,请检查对服务器的第一个网络请求。响应应包含应用程序的完全呈现的 HTML。
路由
要向您的应用添加不同的页面,您需要正确配置路由并在客户端和服务器入口点处理它。
// ./server/index.ts export * from './app'
添加客户端路由
在客户端入口点使用 BrowserRouter 包装您的应用程序以启用客户端路由。
// ./server/constants.ts export const NODE_ENV = process.env.NODE_ENV || 'development' export const APP_PORT = process.env.APP_PORT || 3000 export const PROD = NODE_ENV === 'production' export const HTML_KEY = `<!--app-html-->`
添加服务器端路由
在服务器入口点使用 StaticRouter 来处理服务器端路由。将 url 作为 prop 传递,以根据请求呈现正确的路线。
// ./server/app.ts import express from 'express' import { PROD, APP_PORT } from './constants' import { setupProd } from './prod' import { setupDev } from './dev' export async function createServer() { const app = express() if (PROD) { await setupProd(app) } else { await setupDev(app) } app.listen(APP_PORT, () => { console.log(`http://localhost:${APP_PORT}`) }) } createServer()
更新服务器配置
更新您的开发和生产服务器设置,以将请求 URL 传递给渲染函数:
// ./server/dev.ts import { Application } from 'express' import fs from 'fs' import path from 'path' import { HTML_KEY } from './constants' const HTML_PATH = path.resolve(process.cwd(), 'index.html') const ENTRY_SERVER_PATH = path.resolve(process.cwd(), 'src/entry-server.tsx') export async function setupDev(app: Application) { // Create a Vite development server in middleware mode const vite = await ( await import('vite') ).createServer({ root: process.cwd(), server: { middlewareMode: true }, appType: 'custom', }) // Use Vite middleware for serving files app.use(vite.middlewares) app.get('*', async (req, res, next) => { try { // Read and transform the HTML file let html = fs.readFileSync(HTML_PATH, 'utf-8') html = await vite.transformIndexHtml(req.originalUrl, html) // Load the entry-server.tsx module and render the app const { render } = await vite.ssrLoadModule(ENTRY_SERVER_PATH) const appHtml = await render() // Replace the placeholder with the rendered HTML html = html.replace(HTML_KEY, appHtml) res.status(200).set({ 'Content-Type': 'text/html' }).end(html) } catch (e) { // Fix stack traces for Vite and handle errors vite.ssrFixStacktrace(e as Error) console.error((e as Error).stack) next(e) } }) }
通过这些更改,您现在可以在 React 应用程序中创建与 SSR 完全兼容的路由。然而,这种基本方法不处理延迟加载的组件(React.lazy)。有关管理延迟加载的模块,请参阅我的另一篇文章,使用流和动态数据的高级 React SSR 技术,链接在底部。
码头工人
这是一个用于容器化您的应用程序的 Dockerfile:
// ./server/prod.ts import { Application } from 'express' import fs from 'fs' import path from 'path' import compression from 'compression' import sirv from 'sirv' import { HTML_KEY } from './constants' const CLIENT_PATH = path.resolve(process.cwd(), 'dist/client') const HTML_PATH = path.resolve(process.cwd(), 'dist/client/index.html') const ENTRY_SERVER_PATH = path.resolve(process.cwd(), 'dist/ssr/entry-server.js') export async function setupProd(app: Application) { // Use compression for responses app.use(compression()) // Serve static files from the client build folder app.use(sirv(CLIENT_PATH, { extensions: [] })) app.get('*', async (_, res, next) => { try { // Read the pre-built HTML file let html = fs.readFileSync(HTML_PATH, 'utf-8') // Import the server-side render function and generate HTML const { render } = await import(ENTRY_SERVER_PATH) const appHtml = await render() // Replace the placeholder with the rendered HTML html = html.replace(HTML_KEY, appHtml) res.status(200).set({ 'Content-Type': 'text/html' }).end(html) } catch (e) { // Log errors and pass them to the error handler console.error((e as Error).stack) next(e) } }) }
构建并运行 Docker 镜像
// ./vite.config.ts import { defineConfig } from 'vite' import react from '@vitejs/plugin-react-swc' import { dependencies } from './package.json' export default defineConfig(({ mode }) => ({ plugins: [react()], ssr: { noExternal: mode === 'production' ? Object.keys(dependencies) : undefined, }, }))
{ "include": [ "src", "server", "vite.config.ts" ] }
结论
在本指南中,我们为使用 React 创建生产就绪的 SSR 应用程序奠定了坚实的基础。您已经学习了如何设置项目、配置路由和创建 Dockerfile。此设置非常适合高效构建登陆页面或小型应用程序。
探索代码
- 示例:react-ssr-basics-example
- 模板:react-ssr-template
- Vite 额外模板: template-ssr-react-ts
相关文章
这是我的 React SSR 系列的一部分。更多文章敬请期待!
- 构建生产就绪的 SSR React 应用程序(您在这里)
- 使用流和动态数据的高级 React SSR 技术(即将推出)
- 在 SSR React 应用程序中设置主题(即将推出)
保持联系
我始终乐于接受反馈、合作或讨论技术想法 - 请随时与我们联系!
- 投资组合:maxh1t.xyz
- 电子邮件:m4xh17@gmail.com
以上是构建生产就绪的 SSR React 应用程序的详细内容。更多信息请关注PHP中文网其他相关文章!

选择Python还是JavaScript应基于职业发展、学习曲线和生态系统:1)职业发展:Python适合数据科学和后端开发,JavaScript适合前端和全栈开发。2)学习曲线:Python语法简洁,适合初学者;JavaScript语法灵活。3)生态系统:Python有丰富的科学计算库,JavaScript有强大的前端框架。

JavaScript框架的强大之处在于简化开发、提升用户体验和应用性能。选择框架时应考虑:1.项目规模和复杂度,2.团队经验,3.生态系统和社区支持。

引言我知道你可能会觉得奇怪,JavaScript、C 和浏览器之间到底有什么关系?它们之间看似毫无关联,但实际上,它们在现代网络开发中扮演着非常重要的角色。今天我们就来深入探讨一下这三者之间的紧密联系。通过这篇文章,你将了解到JavaScript如何在浏览器中运行,C 在浏览器引擎中的作用,以及它们如何共同推动网页的渲染和交互。JavaScript与浏览器的关系我们都知道,JavaScript是前端开发的核心语言,它直接在浏览器中运行,让网页变得生动有趣。你是否曾经想过,为什么JavaScr

Node.js擅长于高效I/O,这在很大程度上要归功于流。 流媒体汇总处理数据,避免内存过载 - 大型文件,网络任务和实时应用程序的理想。将流与打字稿的类型安全结合起来创建POWE

Python和JavaScript在性能和效率方面的差异主要体现在:1)Python作为解释型语言,运行速度较慢,但开发效率高,适合快速原型开发;2)JavaScript在浏览器中受限于单线程,但在Node.js中可利用多线程和异步I/O提升性能,两者在实际项目中各有优势。

JavaScript起源于1995年,由布兰登·艾克创造,实现语言为C语言。1.C语言为JavaScript提供了高性能和系统级编程能力。2.JavaScript的内存管理和性能优化依赖于C语言。3.C语言的跨平台特性帮助JavaScript在不同操作系统上高效运行。

JavaScript在浏览器和Node.js环境中运行,依赖JavaScript引擎解析和执行代码。1)解析阶段生成抽象语法树(AST);2)编译阶段将AST转换为字节码或机器码;3)执行阶段执行编译后的代码。

Python和JavaScript的未来趋势包括:1.Python将巩固在科学计算和AI领域的地位,2.JavaScript将推动Web技术发展,3.跨平台开发将成为热门,4.性能优化将是重点。两者都将继续在各自领域扩展应用场景,并在性能上有更多突破。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3 Linux新版
SublimeText3 Linux最新版

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),