Skip to content
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 packages/router/.size-limit.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"gzip": true,
"path": "dist/index.production.js",
"import": "*",
"limit": "4.15 kB"
"limit": "4.2 kB"
},
{
"name": "All publics (Brotli)",
Expand Down
44 changes: 43 additions & 1 deletion packages/router/src/head.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,23 @@ import {
dir,
link,
meta,
script
script,
toHtmlAttribute
} from './head.js'

describe('router', () => {
describe('head', () => {
describe('toHtmlAttribute', () => {
it('should map httpEquiv to http-equiv', () => {
expect(toHtmlAttribute('httpEquiv')).toBe('http-equiv')
})

it('should lowercase other prop names', () => {
expect(toHtmlAttribute('crossOrigin')).toBe('crossorigin')
expect(toHtmlAttribute('content')).toBe('content')
})
})

describe('syncHead', () => {
describe('title', () => {
beforeEach(() => {
Expand Down Expand Up @@ -589,6 +601,36 @@ describe('router', () => {
stop()
})

it('should render httpEquiv as the http-equiv attribute', () => {
const stop = syncHead(() => ({
default: null,
Head$: () => [meta({
httpEquiv: 'refresh',
content: '0; url=/next'
})]
}))

expect(document.head.innerHTML).toBe('<meta http-equiv="refresh" content="0; url=/next">')

stop()
})

it('should hydrate existing http-equiv meta tag', () => {
document.head.innerHTML = '<meta http-equiv="refresh" content="0; url=/next">'

const stop = syncHead(() => ({
default: null,
Head$: () => [meta({
httpEquiv: 'refresh',
content: '0; url=/next'
})]
}))

expect(document.head.innerHTML).toBe('<meta http-equiv="refresh" content="0; url=/next">')

stop()
})

it('should hydrate existing meta tag', () => {
document.head.innerHTML = '<meta name="description" content="Hello world">'

Expand Down
14 changes: 12 additions & 2 deletions packages/router/src/head.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ export function dir($value: DirValue): DirPropertyDescriptor {
}
}

/**
* Converts a head descriptor prop name to its HTML attribute name.
* @param prop - Prop name, such as `httpEquiv` or `crossOrigin`.
* @returns The attribute name, such as `http-equiv` or `crossorigin`.
*/
/* @__NO_SIDE_EFFECTS__ */
export function toHtmlAttribute(prop: string) {
return prop === 'httpEquiv' ? 'http-equiv' : prop.toLowerCase()
}

function buildPredicate(tag: string, attributes: [string, AnySignalish][]) {
let selector = tag
let count = 0
Expand All @@ -126,7 +136,7 @@ function buildPredicate(tag: string, attributes: [string, AnySignalish][]) {
code = String(resolvedValue)
} else {
count++
selector += `[${key}="${String(resolvedValue).replace(/"/g, '\\"')}"]`
selector += `[${toHtmlAttribute(key)}="${String(resolvedValue).replace(/"/g, '\\"')}"]`
}
}
}
Expand Down Expand Up @@ -188,7 +198,7 @@ function startElement(
if (key === 'code') {
element.textContent = stringValue
} else {
element.setAttribute(key, stringValue)
element.setAttribute(toHtmlAttribute(key), stringValue)
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions packages/ssr/src/renderer/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ describe('ssr', () => {
}))).toBe('<meta name="description" />')
})

it('should render httpEquiv as the http-equiv attribute', () => {
expect(headDescriptorToHtml(meta({
httpEquiv: 'refresh',
content: '0; url=/next'
}))).toBe('<meta http-equiv="refresh" content="0; url=/next" />')
})

it('should insert script code as is', () => {
expect(headDescriptorToHtml(script({
type: 'application/ld+json',
Expand Down
5 changes: 3 additions & 2 deletions packages/ssr/src/renderer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import {
type HeadDescriptor,
type Location,
type PageRef,
PermanentReplaceHistoryAction
PermanentReplaceHistoryAction,
toHtmlAttribute
} from '@nano_kit/router'
import {
type EmptyValue,
Expand Down Expand Up @@ -84,7 +85,7 @@ export function headDescriptorToHtml(descriptor: HeadDescriptor): string {
if (key === 'code') {
code = String(resolvedValue)
} else {
html += ` ${key.toLowerCase()}="${escapeHtml(String(resolvedValue))}"`
html += ` ${toHtmlAttribute(key)}="${escapeHtml(String(resolvedValue))}"`
}
}
})
Expand Down