Skip to content

Instantly share code, notes, and snippets.

@jlarmstrongiv
Last active October 24, 2024 05:23
Show Gist options
  • Select an option

  • Save jlarmstrongiv/4ca9047c825da944b042d976e6953d7f to your computer and use it in GitHub Desktop.

Select an option

Save jlarmstrongiv/4ca9047c825da944b042d976e6953d7f to your computer and use it in GitHub Desktop.
React versions of HonoX Components
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>
}
}
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