entry-server.js 2.2 KB
Newer Older
1
import { createApp } from './main'
fxy060608's avatar
fxy060608 已提交
2
import { renderToString } from '@vue/server-renderer'
fxy060608's avatar
fxy060608 已提交
3 4 5
import {
  UNI_SSR,
  UNI_SSR_DATA,
fxy060608's avatar
fxy060608 已提交
6
  UNI_SSR_STORE,
fxy060608's avatar
fxy060608 已提交
7
  UNI_SSR_TITLE,
fxy060608's avatar
fxy060608 已提交
8 9
  UNI_SSR_GLOBAL_DATA,
} from '@dcloudio/uni-shared'
10
import { plugin } from '@dcloudio/uni-h5'
fxy060608's avatar
fxy060608 已提交
11
import { getSsrGlobalData } from '@dcloudio/uni-app'
fxy060608's avatar
fxy060608 已提交
12

fxy060608's avatar
fxy060608 已提交
13
export async function render(url, manifest = {}) {
14 15
  const { app, store } = createApp()
  app.use(plugin)
fxy060608's avatar
fxy060608 已提交
16 17 18
  const router = app.router

  // set the router to the desired URL before rendering
fxy060608's avatar
fxy060608 已提交
19
  await router.push(url)
fxy060608's avatar
fxy060608 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32
  await router.isReady()

  // 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)

  // the SSR manifest generated by Vite contains module -> chunk/asset mapping
  // which we can then use to determine what files need to be preloaded for this
  // request.
  const preloadLinks = renderPreloadLinks(ctx.modules, manifest)
fxy060608's avatar
fxy060608 已提交
33 34
  // the SSR context
  const __uniSSR = ctx[UNI_SSR] || (ctx[UNI_SSR] = {})
fxy060608's avatar
fxy060608 已提交
35
  if (!__uniSSR[UNI_SSR_DATA]) {
fxy060608's avatar
fxy060608 已提交
36 37
    __uniSSR[UNI_SSR_DATA] = {}
  }
fxy060608's avatar
fxy060608 已提交
38
  __uniSSR[UNI_SSR_GLOBAL_DATA] = getSsrGlobalData()
fxy060608's avatar
fxy060608 已提交
39 40 41
  if (store) {
    __uniSSR[UNI_SSR_STORE] = store.state
  }
fxy060608's avatar
fxy060608 已提交
42
  const appContext = renderAppContext(ctx)
fxy060608's avatar
fxy060608 已提交
43
  return [html, preloadLinks, appContext, ctx[UNI_SSR_TITLE] || '']
fxy060608's avatar
fxy060608 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
}

function renderPreloadLinks(modules, manifest) {
  let links = ''
  const seen = new Set()
  modules.forEach((id) => {
    const files = manifest[id]
    if (files) {
      files.forEach((file) => {
        if (!seen.has(file)) {
          seen.add(file)
          links += renderPreloadLink(file)
        }
      })
    }
  })
  return links
}

function renderPreloadLink(file) {
  if (file.endsWith('.js')) {
    return '<link rel="modulepreload" crossorigin href="' + file + '">'
  } else if (file.endsWith('.css')) {
    return '<link rel="stylesheet" href="' + file + '">'
  } else {
    // TODO
    return ''
  }
}
fxy060608's avatar
fxy060608 已提交
73

fxy060608's avatar
fxy060608 已提交
74
function renderAppContext(ctx) {
fxy060608's avatar
fxy060608 已提交
75 76
  return `<script>window.__uniSSR = ${JSON.stringify(ctx[UNI_SSR])}</script>`
}