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

Commit

Permalink
refactor(render): fmt error message
Browse files Browse the repository at this point in the history
  • Loading branch information
boywithkeyboard committed Aug 19, 2023
1 parent b6f347c commit fe1481b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type { Extension } from './extensions.ts'
export { default as jwt } from './jwt.ts'
export { LocationData } from './location_data.ts'
export { otp } from './otp.ts'
export { render } from './render.tsx'
export { sendMail } from './send_mail.ts'
export { Store } from './store.ts'

Expand Down
30 changes: 21 additions & 9 deletions render.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
// Copyright 2023 Samuel Kopp. All rights reserved. Apache-2.0 license.
/** @jsx h */
import { default as renderToString } from 'https://esm.sh/preact-render-to-string@6.1.0?deps=preact@10.16.0'
import { h, VNode } from 'https://esm.sh/preact@10.17.0'
import { extract } from 'https://esm.sh/@twind/core@1.1.3'

import { brightYellow, gray } from 'https://deno.land/std@0.198.0/fmt/colors.ts'
import { extract } from 'https://esm.sh/@twind/core@1.1.3?target=es2022'
import { default as renderToString } from 'https://esm.sh/preact-render-to-string@6.1.0?deps=preact@10.17.0&target=es2022'
import { h, VNode } from 'https://esm.sh/preact@10.17.0?target=es2022'
import { Context } from './mod.ts'

export function render(c: Context, Component: (() => h.JSX.Element) | VNode) {
const html$ = renderToString(
const htmlString = renderToString(
Component instanceof Function ? <Component /> : Component,
)

try {
const { html, css } = extract(html$) // twind throws error when trying to extract if it is not installed
const { html, css } = extract(htmlString) // twind throws error when trying to extract if it is not installed

c.res.body = `<style>${css}</style><body>${html}</body>`
} catch (e) {
console.warn('twind is not installed, styles might not be applied')
c.res.body = html$
if (c.dev) {
console.warn(
gray(
`${
brightYellow('warning')
} - twind is not installed, thus styles might not be applied`,
),
)
}

c.res.body = htmlString
}

c.res.header('content-type', 'text/html; charset=utf-8')
}

export { h } from 'https://esm.sh/preact@10.17.0'
export { h } from 'https://esm.sh/preact@10.17.0?target=es2022'

0 comments on commit fe1481b

Please sign in to comment.