Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

fix: don't crash the server #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 67 additions & 63 deletions src/node/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,74 +25,78 @@ export async function start(port: number, hostname: string) {

const generateApp = (await import(join(cwd(), 'dist/server/index.js'))).generateApp

const [appHtml, preloadLinks, state, head, teleports, redirect] = await generateApp(url, manifest, req, res)
try {
const [appHtml, preloadLinks, state, head, teleports, redirect] = await generateApp(url, manifest, req, res)

if (redirect !== null) {
res.redirect(redirect)
return
}

const $ = load(template)

const resolvedTags = await head.resolveTags() as HeadTag[]

let tags = ['title', 'meta', 'link', 'base', 'style', 'script', 'noscript']

if ($('title').length === 1) {
tags = tags.filter(t => t !== 'title')
const title = resolvedTags.find(t => t.tag === 'title')

if (title !== undefined) {
// @ts-ignore
$('title').text(title.textContent)
if (redirect !== null) {
res.redirect(redirect)
return
}
}

tags.map(tag => {
resolvedTags.filter(t => t.tag === tag)
.map(t => {
let props = ''

for (const [key, value] of Object.entries(t.props)) {
props = `${props} ${key}="${value}"`
}

if (t.innerHTML !== undefined) {
$('head').append(`<${tag} ${props}>${t.innerHTML}</${tag}>`)
} else {
$('head').append(`<${tag} ${props}>`)
}
})
})

const bodyAttrs = resolvedTags.find(t => t.tag === 'bodyAttrs')

if (bodyAttrs !== undefined) {
for (const [key, value] of Object.entries(bodyAttrs.props)) {
$('body').attr(key, value)

const $ = load(template)

const resolvedTags = await head.resolveTags() as HeadTag[]

let tags = ['title', 'meta', 'link', 'base', 'style', 'script', 'noscript']

if ($('title').length === 1) {
tags = tags.filter(t => t !== 'title')
const title = resolvedTags.find(t => t.tag === 'title')

if (title !== undefined) {
// @ts-ignore
$('title').text(title.textContent)
}
}
}

const htmlAttrs = resolvedTags.find(t => t.tag === 'htmlAttrs')

if (htmlAttrs !== undefined) {
for (const [key, value] of Object.entries(htmlAttrs.props)) {
$('html').attr(key, value)

tags.map(tag => {
resolvedTags.filter(t => t.tag === tag)
.map(t => {
let props = ''

for (const [key, value] of Object.entries(t.props)) {
props = `${props} ${key}="${value}"`
}

if (t.innerHTML !== undefined) {
$('head').append(`<${tag} ${props}>${t.innerHTML}</${tag}>`)
} else {
$('head').append(`<${tag} ${props}>`)
}
})
})

const bodyAttrs = resolvedTags.find(t => t.tag === 'bodyAttrs')

if (bodyAttrs !== undefined) {
for (const [key, value] of Object.entries(bodyAttrs.props)) {
$('body').attr(key, value)
}
}

const htmlAttrs = resolvedTags.find(t => t.tag === 'htmlAttrs')

if (htmlAttrs !== undefined) {
for (const [key, value] of Object.entries(htmlAttrs.props)) {
$('html').attr(key, value)
}
}

$('head').append(preloadLinks)
$('#app').html(appHtml)

if (state !== undefined) {
$('body').append(`<script>window.__INITIAL_STATE__ = ${devalue(state)}</script>`)
}

if (teleports['#teleports'] !== undefined) {
$('body').append(`<div id="teleports">${teleports['#teleports']}</div>`)
}

res.status(200).set({ 'Content-Type': 'text/html' }).end($.html())
} catch (e) {
res.status(500).end(e.message)
}

$('head').append(preloadLinks)
$('#app').html(appHtml)

if (state !== undefined) {
$('body').append(`<script>window.__INITIAL_STATE__ = ${devalue(state)}</script>`)
}

if (teleports['#teleports'] !== undefined) {
$('body').append(`<div id="teleports">${teleports['#teleports']}</div>`)
}

res.status(200).set({ 'Content-Type': 'text/html' }).end($.html())
})

app.listen(port, hostname, () => {
Expand Down