From f9a67f569da7e7a9aad57ee67ebc4b8a7831f896 Mon Sep 17 00:00:00 2001 From: Youri van Mill Date: Thu, 25 Jan 2024 10:48:09 +0100 Subject: [PATCH] fsdfdsfsffs --- src/node/start.ts | 130 ++++++++++++++++++++++++---------------------- 1 file changed, 67 insertions(+), 63 deletions(-) diff --git a/src/node/start.ts b/src/node/start.ts index 950cdee..96de0f9 100644 --- a/src/node/start.ts +++ b/src/node/start.ts @@ -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}`) - } 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}`) + } 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(``) + } + + if (teleports['#teleports'] !== undefined) { + $('body').append(`
${teleports['#teleports']}
`) + } + + 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(``) - } - - if (teleports['#teleports'] !== undefined) { - $('body').append(`
${teleports['#teleports']}
`) - } - - res.status(200).set({ 'Content-Type': 'text/html' }).end($.html()) }) app.listen(port, hostname, () => {