From 724d4466b6a0073181fab015d9b6f4fab51ba914 Mon Sep 17 00:00:00 2001 From: Yanu Widodo <10122431+ynwd@users.noreply.github.com> Date: Sun, 2 Apr 2023 10:43:54 +0700 Subject: [PATCH] fix render --- server/render.ts | 43 +++++++++++++++++------------------------- server/render_test.ts | 1 + server/render_test.tsx | 21 +++++++++++---------- server/static.ts | 5 ++--- types.d.ts | 7 ++++--- 5 files changed, 35 insertions(+), 42 deletions(-) diff --git a/server/render.ts b/server/render.ts index f1210723..8ec23829 100644 --- a/server/render.ts +++ b/server/render.ts @@ -108,32 +108,19 @@ export function createSSR(el: JSXHandler | JSX.Element): SSR { return ``; } - /** - * @param bundle - * @param rootComponent - * @param rootTSX - */ function createBundle( - bundle?: string, - rootComponent?: string, - rootTSX?: string, + bundle: string, + rootComponent: string, + rootTSX: string, ) { const cwd = Deno.cwd(); - const b = bundle ? bundle : "bundle"; const hydrateTarget = `${cwd}/${pageDir}/${rootTSX}.hydrate.tsx`; - const bundlePath = `${cwd}${staticPath}/${b}.js`; + const bundlePath = `${cwd}${staticPath}/${bundle}.js`; - try { - if (!rootComponent) rootComponent = "App"; - if (!rootTSX) rootTSX = "app"; - Deno.writeTextFile( - hydrateTarget, - createHydrate(rootComponent, rootTSX), - ); - } catch (err) { - console.error(err); - throw err; - } + Deno.writeTextFile( + hydrateTarget, + createHydrate(rootComponent, rootTSX), + ); esbuild.build({ plugins: [denoPlugin()], @@ -163,11 +150,11 @@ export function createSSR(el: JSXHandler | JSX.Element): SSR { const initRootAttr = rootAttr ? ` ${rootAttr}` : ``; const component = ReactDOMServer.renderToString(element); const title = options.title ?? ""; - const link = options.link ?? ""; - const meta = options.meta ?? ""; - const script = options.script ?? ""; - const style = options.style ?? ""; - const bundle = options.bundle ?? "bundle"; + const link = options.link; + const meta = options.meta; + const script = options.script; + const style = options.style; + const bundle = options.bundle; const description = ogDescription ? createDescription(ogDescription) : ""; const initOgTitle = ogTitle ? createTitle(ogTitle) : ""; const initOgImage = ogImage ? createImage(ogImage) : ""; @@ -239,6 +226,10 @@ export function createSSR(el: JSXHandler | JSX.Element): SSR { twitterCard = c; return instance; }, + twitterImageAlt: (a: string) => { + twitterImageAlt = a; + return instance; + }, metaDesc: (d: string) => { metaDesc = d; return instance; diff --git a/server/render_test.ts b/server/render_test.ts index b04f7952..620e50a1 100644 --- a/server/render_test.ts +++ b/server/render_test.ts @@ -24,6 +24,7 @@ Deno.test({ .ogImage("https://deno.land/images/artwork/v1.png") .ogSiteName("example.com") .twitterCard("card") + .twitterImageAlt("alt") .bodyAttr("style") .ogType("type") .lang("EN") diff --git a/server/render_test.tsx b/server/render_test.tsx index 413acbbb..2ffce7e8 100644 --- a/server/render_test.tsx +++ b/server/render_test.tsx @@ -16,7 +16,7 @@ Deno.test({ .page("/", hello, (_req: HttpRequest, res: HttpResponse) => { const data = "data"; return initSSR(res, hello) - .title(`Hello ${data}`) + // .title(`Hello ${data}`) .ogTitle(`Hello ${data}`) .ogURL("https://fastro.deno.dev") .metaDesc("description") @@ -24,21 +24,22 @@ Deno.test({ .ogImage("https://deno.land/images/artwork/v1.png") .ogSiteName("example.com") .twitterCard("card") + .twitterImageAlt("alt") .bodyAttr("style") .ogType("type") .lang("EN") - .bundle("hello") + // .bundle("hello") .cdn("cdn") .dir("../pages") - .style("h1 {color:red;}") - .style("h2 {color:red;}") - .script("script1") - .script("script2") + // .style("h1 {color:red;}") + // .style("h2 {color:red;}") + // .script("script1") + // .script("script2") .props({ data }) - .meta('name="route-pattern" content="/"') - .link( - 'rel="alternate icon" class="js-site-favicon" type="image/png" href="https://github.githubassets.com/favicons/favicon.png"', - ) + // .meta('name="route-pattern" content="/"') + // .link( + // 'rel="alternate icon" class="js-site-favicon" type="image/png" href="https://github.githubassets.com/favicons/favicon.png"', + // ) .render(); }); diff --git a/server/static.ts b/server/static.ts index d03063a8..952f9c6c 100644 --- a/server/static.ts +++ b/server/static.ts @@ -22,7 +22,7 @@ export function handleStaticFile( if (cache[extID]) { extName = cache[extID]; } else { - extName = extname(path ?? ""); + extName = extname(path); cache[extID] = extName; } @@ -51,7 +51,6 @@ async function handleNonText( } const ct = contentType(extname(path)) || "application/octet-stream"; - console.log("CT", ct); return new Response(file.readable, { headers: { "Content-Type": ct, @@ -129,7 +128,7 @@ function getPathUrl(url: string, staticURL: string) { const p = `${staticURL}/:file*`; const pattern = new URLPattern(p, baseUrl); const r = pattern.test(url); - if (!r) return null; + if (!r) return ""; } return url.substring(baseUrl.length); } diff --git a/types.d.ts b/types.d.ts index a4eeba5b..6533b7fe 100644 --- a/types.d.ts +++ b/types.d.ts @@ -231,15 +231,16 @@ export interface SSR { ogSiteName: (name: string) => SSR; ogDesc: (desc: string) => SSR; twitterCard: (card: string) => SSR; + twitterImageAlt: (alt: string) => SSR; metaDesc: (desc: string) => SSR; props: (props: any) => SSR; request: (req: HttpRequest) => SSR; cache: (cache: Container) => SSR; render: () => Response; _createBundle: ( - bundle?: string, - rootComponent?: string, - rootTSX?: string, + bundle: string, + rootComponent: string, + rootTSX: string, ) => void; _getBundleName: () => string; bundle: (name: string) => SSR;