update
This commit is contained in:
15
main.ts
15
main.ts
@@ -15,7 +15,10 @@ const utf8Decoder = new TextDecoder("utf-8");
|
||||
const parser = new DOMParser();
|
||||
|
||||
function appHeaderScript(params: { ssrContext: DJSSRContext, entryPath: string }) {
|
||||
return `<script type="importmap">
|
||||
return `
|
||||
<title>${ toValue(params.ssrContext.head.title) }</title>
|
||||
${ toValue(params.ssrContext.head.metatags).map(_ => `<meta name="${ _.name }" content="${ _.content }">`).join('\n\t') }
|
||||
<script type="importmap">
|
||||
{
|
||||
"imports": {
|
||||
"vue": "/deps/vue/dist/vue.esm-browser.prod.js",
|
||||
@@ -77,13 +80,16 @@ async function getAPIResponse(apiReq: Request): Promise<Response> {
|
||||
for (const filePath of paths) {
|
||||
const content = await Deno.readTextFile(filePath);
|
||||
const dom = parser.parseFromString(content, 'text/html');
|
||||
const metadata = { title: '', tags: [] as string[], slug: '' };
|
||||
const metadata = { title: '', author: 'Ray Peat, übersetzt von Daniel Ledda', titleEn: '', titleDe: '', tags: [] as string[], slug: '' };
|
||||
const metaTags = dom.querySelectorAll('meta') as unknown as NodeListOf<HTMLMetaElement>;
|
||||
for (const metaTag of metaTags) {
|
||||
const name = metaTag.attributes.getNamedItem('name')?.value ?? '';
|
||||
const content = metaTag.attributes.getNamedItem('content')?.value ?? '';
|
||||
if (name === 'title') {
|
||||
if (name === 'title-de') {
|
||||
metadata.titleDe = content;
|
||||
metadata.title = content;
|
||||
} else if (name === 'title-en') {
|
||||
metadata.titleEn = content;
|
||||
} else if (name === 'tags') {
|
||||
metadata.tags = content ? content.split(",") : [];
|
||||
} else if (name === 'slug') {
|
||||
@@ -190,7 +196,7 @@ Deno.serve({
|
||||
app.provide("dom-parse", (innerHTML: string) => {
|
||||
return parser.parseFromString(innerHTML, "text/html").documentElement;
|
||||
});
|
||||
const ssrContext: DJSSRContext = { styles: {}, registry: {}, head: { title: "" } };
|
||||
const ssrContext: DJSSRContext = { styles: {}, registry: {}, head: { title: "", metatags: [] } };
|
||||
if (router) {
|
||||
await router.replace(pathname.split("/generative-energy")[1]);
|
||||
await router.isReady();
|
||||
@@ -198,7 +204,6 @@ Deno.serve({
|
||||
const rendered = await renderToString(app, ssrContext);
|
||||
const content = utf8Decoder.decode(await Deno.readFile(siteTemplate))
|
||||
.replace(`<!-- SSR OUTLET -->`, rendered)
|
||||
.replaceAll("%TITLE%", toValue(ssrContext.head?.title) ?? "Site")
|
||||
.replace(
|
||||
`<!-- SSR HEAD OUTLET -->`,
|
||||
appHeaderScript({ ssrContext, entryPath: clientEntry }),
|
||||
|
||||
Reference in New Issue
Block a user