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

Commit

Permalink
fsdfdsfsffs
Browse files Browse the repository at this point in the history
  • Loading branch information
yooouuri committed Jan 25, 2024
1 parent 2d1f70d commit f9a67f5
Showing 1 changed file with 67 additions and 63 deletions.
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

0 comments on commit f9a67f5

Please sign in to comment.