React Server API + Vite 简单实现SSR【根据文档提供的案例进行分析】

文档:cn.vite.dev/guide/ssr.h...

版本:react 19。 vite 5.4.19 (vite 7中一些api行为不太一样)

关键步骤:

第一步:server返回html

  1. 一个server.js -- 使用类似模板语法,替换index.html里一些特定"模板"为React组件,组合成html dom
js 复制代码
import {createServer} from 'vite';
import express from 'express';
import fs from 'node:fs'
import { fileURLToPath } from 'node:url'

import path from 'node:path'

const __dirname = path.dirname(fileURLToPath(import.meta.url))


const vite = await createServer({
   server:{middlewareMode:true},
   appType:'custom'
})


const app = express()


app.use(vite.middlewares)

app.use("*all", async (req,res)=>{
   const template= await vite.transformIndexHtml(
       req.url,
       await fs.readFileSync(
       path.resolve(__dirname, 'index.html'),
       'utf-8',
       )    
   )
   
   const  {getMyRender} = await vite.ssrLoadModule(path.resolve(__dirname, 'server-entry.jsx'))  
   //注意这里,vite7 不是这么用的,它需要.render;vite 5 vite.ssrLoadModule返回{ getMyRender: [Getter], [Symbol(Symbol.toStringTag)]: 'Module' }
   

   const appHTML = await getMyRender(req.url)
   //注意这里,vite7返回一个html对象,可以appHTMl.html ;vite 5这里就一个html字符串
   
   const html = template
   .replace(`<!--app-->`,()=>appHTML)//替换index.html里的模板为React渲染好的组件
   //vite7的官方案例里给的可以分别替换head 和html body
   res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
})


app.listen(3000,()=>{
   console.log('server running on 3000')
})
  1. 一个server-entry.js -- 调用React.renderToString(<StrictMode><你的组件></StrictMode>)返回html字符串,server.js里可以使用这个字符串来替换index.html里的"模板"
jsx 复制代码
import { renderToString } from "react-dom/server"
import {App} from "./App"
import React, { StrictMode } from 'react'


export function getMyRender(){
  const htmlstr= renderToString(<StrictMode><App/></StrictMode>)
  return htmlstr
}
  1. 一个index.html - <!--app-->会被server.js替换成React组件,是渲染好的DOM,不是jsx。
html 复制代码
<!doctype html>
<html lang="en">
 <head>
   <meta charset="UTF-8" />
   <meta name="viewport" content="width=device-width, initial-scale=1.0" />
   <title>Vite + React</title>
 </head>
 <body><div id="app"><!--app--></div>
   <script type="module" src="./client-entry.jsx"></script>
 </body>
</html>

===

第二步: hydrate

关注一下index.html的 <script type="module" ...> 它引用了一个client-entry.jsx,这里就是水合发生的地方 1.准备client-entry. index.html里要注意挂载点<div id='app'>要在那

jsx 复制代码
import  { StrictMode } from "react"
import { hydrateRoot } from "react-dom/client"
import {App} from "./App"


hydrateRoot(document.getElementById("app"),<StrictMode><App/></StrictMode>)

第三步 node server.js 运行即可

可能的报错

Hydration failed because the server rendered HTML didn't match the client.

debug:先不加水合(就是把index.html里的script先注释掉)看看渲染结果是不是正常,有可能vite版本会带来ssrLoadModule返回类型的细微差异,导致server拼的html不对,那水合时自然找不到对应的dom元素了,就会报错。server返回的dom与水合时的一定要对上哦

相关推荐
li357418 小时前
将已有 Vue 项目通过 Electron 打包为桌面客户端的完整步骤
前端·vue.js·electron
Icoolkj19 小时前
VuePress 与 VitePress 深度对比:特性、差异与选型指南
前端·javascript·vue.js
excel19 小时前
CNN 分层详解:卷积、池化到全连接的作用与原理
前端
excel19 小时前
CNN 多层设计详解:从边缘到高级特征的逐层学习
前端
西陵20 小时前
Nx带来极致的前端开发体验——任务编排
前端·javascript·架构
大前端helloworld21 小时前
从初中级如何迈入中高级-其实技术只是“入门卷”
前端·面试
东风西巷1 天前
Balabolka:免费高效的文字转语音软件
前端·人工智能·学习·语音识别·软件需求
萌萌哒草头将军1 天前
10个 ES2025 新特性速览!🚀🚀🚀
前端·javascript·vue.js
半夏陌离1 天前
SQL 入门指南:排序与分页查询(ORDER BY 多字段排序、LIMIT 分页实战)
java·前端·数据库