-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
74 lines (66 loc) · 2.02 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
73
74
import { pipeToWebWritable, renderToString } from 'vue/server-renderer'
import { createApp } from './main'
import { renderHeadToString } from '@vueuse/head'
function renderPreloadLinks(modules, manifest) {
let links = ''
const seen = new Set()
for (const [key, value] of Object.entries(manifest)) {
if (key.startsWith('node_modules')) {
if (!links.includes(value)) {
console.log(value[0])
links += renderPreloadLink(value[0])
}
}
}
modules.forEach(id => {
const files = manifest[id]
if (files) {
files.forEach(file => {
seen.add(file)
const filename = file.split(/[\\/]/).pop()
if (manifest[filename]) {
for (const depFile of manifest[filename]) {
links += renderPreloadLink(depFile)
seen.add(depFile)
}
}
links += renderPreloadLink(file)
})
}
})
return links
}
function renderPreloadLink(file) {
if (file.endsWith('.js')) {
return `<${file}>; rel=preload; as=script; crossorigin, `
} else if (file.endsWith('.css')) {
return `<${file}>; rel=preload; as=style`
} else {
// TODO
return ''
}
}
export async function render(url, manifest, template) {
const { app, router, head } = createApp()
// set the router to the desired URL before rendering
router.push(url)
await router.isReady()
const ctx = {}
await renderToString(app) // Workaround to get head <meta name="head:count" content="0">
const { headTags } = renderHeadToString(head)
const tmpl = template.split('<!--app-->')
const prepend = tmpl[0].replace(`<!--head-->`, headTags)
const append = tmpl[1]
const encoder = new TextEncoder()
const { readable, writable } = new TransformStream({
start(controller) {
controller.enqueue(encoder.encode(prepend))
},
flush(controller) {
controller.enqueue(encoder.encode(append))
}
})
pipeToWebWritable(app, ctx, writable)
const link = renderPreloadLinks(ctx?.modules, manifest)
return [readable, link]
}