Skip to content

Commit

Permalink
fix render
Browse files Browse the repository at this point in the history
  • Loading branch information
ynwd committed Apr 2, 2023
1 parent b3e2190 commit 724d446
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 42 deletions.
43 changes: 17 additions & 26 deletions server/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,32 +108,19 @@ export function createSSR(el: JSXHandler | JSX.Element): SSR {
return `<meta name="twitter:image:alt" content="${alt}">`;
}

/**
* @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()],
Expand Down Expand Up @@ -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) : "";
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions server/render_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
21 changes: 11 additions & 10 deletions server/render_test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,30 @@ 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")
.ogDesc("description")
.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();
});

Expand Down
5 changes: 2 additions & 3 deletions server/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function handleStaticFile(
if (cache[extID]) {
extName = cache[extID];
} else {
extName = extname(path ?? "");
extName = extname(path);
cache[extID] = extName;
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
7 changes: 4 additions & 3 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 724d446

Please sign in to comment.