-
-
Save jlarmstrongiv/4ca9047c825da944b042d976e6953d7f to your computer and use it in GitHub Desktop.
React versions of HonoX Components
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import type { FC } from 'react' | |
| import type { Manifest } from 'vite' | |
| type Options = { manifest?: Manifest; prod?: boolean } & JSX.IntrinsicElements['link'] | |
| export const Link: FC<Options> = (options) => { | |
| let { href, prod, manifest, ...rest } = options | |
| if (href) { | |
| if (prod ?? import.meta.env.PROD) { | |
| if (!manifest) { | |
| const MANIFEST = import.meta.glob<{ default: Manifest }>('/dist/.vite/manifest.json', { | |
| eager: true, | |
| }) | |
| for (const [, manifestFile] of Object.entries(MANIFEST)) { | |
| if (manifestFile['default']) { | |
| manifest = manifestFile['default'] | |
| break | |
| } | |
| } | |
| } | |
| if (manifest) { | |
| const assetInManifest = manifest[href.replace(/^\//, '')] | |
| if (assetInManifest) { | |
| if (href.startsWith('/')) { | |
| return <link href={`/${assetInManifest.file}`} {...rest}></link> | |
| } | |
| return <link href={assetInManifest.file} {...rest}></link> | |
| } | |
| } | |
| return <></> | |
| } else { | |
| return <link href={href} {...rest}></link> | |
| } | |
| } | |
| return <link {...rest} /> | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { HasIslands } from 'honox/server' | |
| import type { Manifest } from 'vite' | |
| type Options = { | |
| src: string | |
| async?: boolean | |
| prod?: boolean | |
| manifest?: Manifest | |
| nonce?: string | |
| } | |
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | |
| export const Script = (options: Options): any => { | |
| const src = options.src | |
| if (options.prod ?? import.meta.env.PROD) { | |
| let manifest: Manifest | undefined = options.manifest | |
| if (!manifest) { | |
| const MANIFEST = import.meta.glob<{ default: Manifest }>('/dist/.vite/manifest.json', { | |
| eager: true, | |
| }) | |
| for (const [, manifestFile] of Object.entries(MANIFEST)) { | |
| if (manifestFile['default']) { | |
| manifest = manifestFile['default'] | |
| break | |
| } | |
| } | |
| } | |
| if (manifest) { | |
| const scriptInManifest = manifest[src.replace(/^\//, '')] | |
| if (scriptInManifest) { | |
| return ( | |
| <HasIslands> | |
| <script | |
| type='module' | |
| async={!!options.async} | |
| src={`/${scriptInManifest.file}`} | |
| nonce={options.nonce} | |
| ></script> | |
| </HasIslands> | |
| ) | |
| } | |
| } | |
| return <></> | |
| } else { | |
| return <script type='module' async={!!options.async} src={src} nonce={options.nonce}></script> | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { reactRenderer } from '@hono/react-renderer' | |
| import { Script } from '../components/Script' | |
| import { Link } from '../components/Link' | |
| export default reactRenderer(({ children, title }) => { | |
| return ( | |
| <html lang='en'> | |
| <head> | |
| <meta charSet='UTF-8' /> | |
| <meta name='viewport' content='width=device-width, initial-scale=1.0' /> | |
| {title ? <title>{title}</title> : ''} | |
| <Script src="/app/client.ts" async /> | |
| <Link href="/app/style.css" rel="stylesheet" /> | |
| </head> | |
| <body>{children}</body> | |
| </html> | |
| ) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment