Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(jsx): skip the special behavior when the element is in the head. #3352

Merged
merged 1 commit into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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