Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/ssr-match-id-null-byte.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/router-core': patch
---

Encode dehydrated SSR match IDs with the replacement character instead of a null byte, so the inlined hydration payload no longer contains U+0000 (which is invalid in HTML and rejected by markup validators)
2 changes: 1 addition & 1 deletion packages/router-core/src/ssr/ssr-match-id.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export function dehydrateSsrMatchId(id: string): string {
return id.replaceAll('/', '\0')
return id.replaceAll('/', '\uFFFD')
}

export function hydrateSsrMatchId(id: string): string {
Expand Down
17 changes: 17 additions & 0 deletions packages/router-core/tests/ssr-match-id.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,21 @@ describe('ssr match id codec', () => {
it('decodes browser-normalized replacement chars back to slashes', () => {
expect(hydrateSsrMatchId('\uFFFDposts\uFFFD1')).toBe('/posts/1')
})

it('does not emit control characters that are invalid in SSR HTML', () => {
const dehydratedId = dehydrateSsrMatchId(
'/$orgId/projects/$projectId//acme/projects/dashboard/{}',
)

// U+0000 and the other C0 control characters trigger a
// control-character-in-input-stream parse error when the dehydrated id is
// inlined into the SSR <script> payload, so the codec must never emit them.
const nullChar = String.fromCharCode(0)
expect(dehydratedId).not.toContain(nullChar)

const hasControlChar = dehydratedId
.split('')
.some((char) => char.charCodeAt(0) <= 0x1f)
expect(hasControlChar).toBe(false)
})
})