Skip to content

Commit

Permalink
feat: <Script /> don't emits script if no island (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
yusukebe authored Feb 4, 2024
1 parent ef9fd5b commit 8a6acda
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 17 deletions.
7 changes: 6 additions & 1 deletion src/server/components/script.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { FC } from 'hono/jsx'
import type { Manifest } from 'vite'
import { HasIslands } from './has-islands.js'

type Options = {
src: string
Expand All @@ -19,7 +20,11 @@ export const Script: FC<Options> = async (options) => {
if (manifest) {
const scriptInManifest = manifest[src.replace(/^\//, '')]
if (scriptInManifest) {
return <script type='module' src={`/${scriptInManifest.file}`}></script>
return (
<HasIslands>
<script type='module' src={`/${scriptInManifest.file}`}></script>
</HasIslands>
)
}
}
return <></>
Expand Down
3 changes: 3 additions & 0 deletions test/hono-jsx/app-script/islands/Component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Component() {
return <p>Component</p>
}
26 changes: 26 additions & 0 deletions test/hono-jsx/app-script/routes/_renderer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { jsxRenderer } from 'hono/jsx-renderer'
import { Script } from '../../../../src/server'

export default jsxRenderer(
({ children }) => {
return (
<html>
<head>
<Script
src='/app/client.ts'
prod={true}
manifest={{
'app/client.ts': {
file: 'static/client-abc.js',
},
}}
/>
</head>
<body>{children}</body>
</html>
)
},
{
docType: false,
}
)
18 changes: 4 additions & 14 deletions test/hono-jsx/app-script/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
import { Script } from '../../../../src/server'
import Component from '../islands/Component'

export default function Hello() {
return (
<html>
<head>
<Script
src='/app/client.ts'
prod={true}
manifest={{
'app/client.ts': {
file: 'static/client-abc.js',
},
}}
/>
</head>
</html>
<main>
<Component />
</main>
)
}
10 changes: 8 additions & 2 deletions test/hono-jsx/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ describe('With preserved', () => {
})

describe('API', () => {
const ROUES = import.meta.glob('./app/routes//**/[a-z[-][a-z-_[]*.(tsx|ts)', {
const ROUES = import.meta.glob('./app/routes/**/[a-z[-][a-z-_[]*.(tsx|ts)', {
eager: true,
})

Expand Down Expand Up @@ -284,16 +284,22 @@ describe('<Script /> component', () => {
const ROUTES = import.meta.glob('./app-script/routes/index.tsx', {
eager: true,
})

const RENDERER = import.meta.glob('./app-script/routes/**/_renderer.tsx', {
eager: true,
})

const app = createApp({
root: './app-script/routes',
ROUTES: ROUTES as any,
RENDERER: RENDERER as any,
})

it('Should convert the script path correctly', async () => {
const res = await app.request('/')
expect(res.status).toBe(200)
expect(await res.text()).toBe(
'<html><head><script type="module" src="/static/client-abc.js"></script></head></html>'
'<html><head><script type="module" src="/static/client-abc.js"></script></head><body><main><honox-island component-name="Component.tsx" data-serialized-props="{}"><p>Component</p></honox-island></main></body></html>'
)
})
})

0 comments on commit 8a6acda

Please sign in to comment.