13.2 ssr基本原理,构建步骤

1. ssr的目录结构

服务端渲染 | Vite 官方中文文档

  • index.html
  • server.js # main application server
  • src/
    • main.js # 导出环境无关的(通用的)应用代码
    • entry-client.js # 将应用挂载到一个 DOM 元素上
    • entry-server.js # 使用某框架的 SSR API 渲染该应用

1.index.html 将需要引用 entry-client.js 并包含一个占位标记供给服务端渲染时注入:

js 复制代码
<div id="app"><!--ssr-outlet--></div>
<script type="module" src="/src/entry-client.js"></script>

你可以使用任何你喜欢的占位标记来替代 <!--ssr-outlet-->,只要它能够被正确替换

2.server.js

js 复制代码
import fs from 'node:fs/promises'
import express from 'express'

// Constants
const isProduction = process.env.NODE_ENV === 'production'
const port = process.env.PORT || 5173
const base = process.env.BASE || '/'

// Cached production assets
const templateHtml = isProduction
  ? await fs.readFile('./dist/client/index.html', 'utf-8')
  : ''

// Create http server
const app = express()

// Add Vite or respective production middlewares
/** @type {import('vite').ViteDevServer | undefined} */
let vite
if (!isProduction) {
  const { createServer } = await import('vite')
  vite = await createServer({
    server: { middlewareMode: true },
    appType: 'custom',
    base,
  })
  app.use(vite.middlewares)
} else {
  const compression = (await import('compression')).default
  const sirv = (await import('sirv')).default
  app.use(compression())
  app.use(base, sirv('./dist/client', { extensions: [] }))
}

// Serve HTML
app.use('*all', async (req, res) => {
  try {
    const url = req.originalUrl.replace(base, '')

    /** @type {string} */
    let template
    /** @type {import('./src/entry-server.ts').render} */
    let render
    if (!isProduction) {
      console.log("123")
      // Always read fresh template in development
      template = await fs.readFile('./index.html', 'utf-8')
      template = await vite.transformIndexHtml(url, template)
      render = (await vite.ssrLoadModule('/src/entry-server.ts')).render
    } else {
      console.log("456")
      template = templateHtml
      render = (await import('./dist/server/entry-server.js')).render
    }

    const rendered = await render(url)

    const html = template
      .replace(`<!--app-head-->`, rendered.head ?? '')
      .replace(`<!--app-html-->`, rendered.html ?? '')

    res.status(200).set({ 'Content-Type': 'text/html' }).send(html)
  } catch (e) {
    vite?.ssrFixStacktrace(e)
    console.log(e.stack)
    res.status(500).end(e.stack)
  }
})

// Start http server
app.listen(port, () => {
  console.log(`Server started at http://localhost:${port}`)
})

3.entry-client.js

js 复制代码
import './style.css'
import { createApp } from './main'

const { app } = createApp()

app.mount('#app')

4.entry-server.js

js 复制代码
import { renderToString } from 'vue/server-renderer'
import { createApp } from './main'

export async function render(_url: string) {
  const { app } = createApp()

  // passing SSR context object which will be available via useSSRContext()
  // @vitejs/plugin-vue injects code into a component's setup() that registers
  // itself on ctx.modules. After the render, ctx.modules would contain all the
  // components that have been instantiated during this render call.
  const ctx = {}
  const html = await renderToString(app, ctx)
  console.log('CCCCCCCCCCCCC', app)
  return { html }
}

5.package.json

js 复制代码
  "scripts": {
    "dev": "node server",
    "build": "npm run build:client && npm run build:server",
    "build:client": "vite build --outDir dist/client",
    "build:server": "vite build --ssr src/entry-server.ts --outDir dist/server",
    "preview": "cross-env NODE_ENV=production node server",
    "check": "vue-tsc"
  },
相关推荐
m0_52611940几秒前
pdf文件根据页数解析成图片 js vue3
前端·javascript·pdf
时光少年5 分钟前
Compose AnnotatedString实现Html样式解析
android·前端
用户9047066835717 分钟前
假数据生成器——JSONPlaceholder
前端
光影少年22 分钟前
react16中的hooks的底层实现原理
前端·react.js·掘金·金石计划
sorryhc30 分钟前
手写一个Webpack HMR插件——彻底搞懂热更新原理
前端·javascript·前端工程化
无双_Joney34 分钟前
[更新迭代 - 1] Nestjs 在24年底更新了啥?(bug修复篇)
前端·后端·node.js
xiaoxiao无脸男42 分钟前
three.js
开发语言·前端·javascript
木易 士心1 小时前
Vue 自定义指令详解
javascript·vue.js·ecmascript
90后的晨仔1 小时前
Vue 组件注册详解:全局注册 vs 局部注册
前端·vue.js
前端Hardy1 小时前
HTML&CSS:高颜值交互式日历,贴心记录每一天
前端·javascript·css