Skip to content

Commit

Permalink
perf(jsx): skip the special behavior when the element is in the head. (
Browse files Browse the repository at this point in the history
  • Loading branch information
usualoma authored Sep 1, 2024
1 parent 0712530 commit e4cc5aa
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/jsx/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ export const jsxFn = (
props,
children
)
} else if (tag === 'svg') {
} else if (tag === 'svg' || tag === 'head') {
nameSpaceContext ||= createContext('')
return new JSXNode(tag, props, [
new JSXFunctionNode(
Expand Down
15 changes: 15 additions & 0 deletions src/jsx/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,21 @@ describe('render to string', () => {
expect(template.toString()).toBe('<span data-text="&lt;html-escaped-string&gt;">Hello</span>')
})
})

describe('head', () => {
it('Simple head elements should be rendered as is', () => {
const template = (
<head>
<title>Hono!</title>
<meta name='description' content='A description' />
<script src='script.js'></script>
</head>
)
expect(template.toString()).toBe(
'<head><title>Hono!</title><meta name="description" content="A description"/><script src="script.js"></script></head>'
)
})
})
})

describe('className', () => {
Expand Down
21 changes: 18 additions & 3 deletions src/jsx/intrinsic-element/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,15 @@ const documentMetadataTag = (tag: string, children: Child, props: Props, sort: b

export const title: FC<PropsWithChildren> = ({ children, ...props }) => {
const nameSpaceContext = getNameSpaceContext()
if (nameSpaceContext && useContext(nameSpaceContext) === 'svg') {
new JSXNode('title', props, toArray(children ?? []) as Child[])
if (nameSpaceContext) {
const context = useContext(nameSpaceContext)
if (context === 'svg' || context === 'head') {
return new JSXNode(
'title',
props,
toArray(children ?? []) as Child[]
) as unknown as HtmlEscapedString
}
}

return documentMetadataTag('title', children, props, false)
Expand All @@ -116,7 +123,11 @@ export const script: FC<PropsWithChildren<IntrinsicElements['script']>> = ({
children,
...props
}) => {
if (['src', 'async'].some((k) => !props[k])) {
const nameSpaceContext = getNameSpaceContext()
if (
['src', 'async'].some((k) => !props[k]) ||
(nameSpaceContext && useContext(nameSpaceContext) === 'head')
) {
return returnWithoutSpecialBehavior('script', children, props)
}

Expand Down Expand Up @@ -144,6 +155,10 @@ export const link: FC<PropsWithChildren<IntrinsicElements['link']>> = ({ childre
return documentMetadataTag('link', children, props, 'precedence' in props)
}
export const meta: FC<PropsWithChildren> = ({ children, ...props }) => {
const nameSpaceContext = getNameSpaceContext()
if (nameSpaceContext && useContext(nameSpaceContext) === 'head') {
return returnWithoutSpecialBehavior('meta', children, props)
}
return documentMetadataTag('meta', children, props, false)
}

Expand Down

0 comments on commit e4cc5aa

Please sign in to comment.