Repository navigation
fix: preserve metadata ownership when hydrating element roots #108
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
Open
BleedingDev
wants to merge
1
commit into
TanStack:main
Choose a base branch
from
BleedingDev:fix/element-root-metadata-ownership
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+139
−10
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@tanstack/redact': patch | ||
| --- | ||
|
|
||
| Adopt server-hoisted title and metadata from the owning document when hydrating an Element root, preserving nested Suspense content and its interactivity. Release claims after abandoned root hydration, and restrict inline metadata searches to the active Suspense range. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { afterEach, expect, it, vi } from 'vitest' | ||
| import { Suspense, lazy, useEffect, useState } from 'react' | ||
| import { hydrateRoot } from 'react-dom/client' | ||
| import { flushSync } from 'react-dom' | ||
|
|
||
| const cleanups: Array<() => void> = [] | ||
| afterEach(() => { for (const cleanup of cleanups.splice(0)) cleanup() }) | ||
|
|
||
| it.each([false, true])('adopts hoisted metadata with an Element root and lazy=%s', async pending => { | ||
| const doc = new DOMParser().parseFromString('<!doctype html><html><head><title>Page</title><meta name="description" content="Page"></head><body><main><!--$--><section><h1>Page</h1><!--$--><aside><button>0</button></aside><!--/$--></section><!--/$--></main></body></html>', 'text/html') | ||
| const container = doc.querySelector('main')! | ||
| const section = doc.querySelector('section'), aside = doc.querySelector('aside') | ||
| const title = doc.querySelector('title'), meta = doc.querySelector('meta') | ||
| const error = vi.fn(), mounted = vi.fn() | ||
| function Counter() { | ||
| useEffect(mounted, []) | ||
| const [count, setCount] = useState(0) | ||
| return <aside><button onClick={() => setCount(value => value + 1)}>{count}</button></aside> | ||
| } | ||
| let resolve!: (value: { default: typeof Counter }) => void | ||
| const load = vi.fn(() => new Promise<{ default: typeof Counter }>(yes => { resolve = yes })) | ||
| const Lazy = lazy(load) | ||
| function Page({ updated = false }: { updated?: boolean }) { | ||
| return <Suspense fallback={null}><section><title>{updated ? 'Updated' : 'Page'}</title><meta name="description" content={updated ? 'Updated' : 'Page'} /><h1>Page</h1><Suspense fallback={<i>Loading</i>}>{pending ? <Lazy /> : <Counter />}</Suspense></section></Suspense> | ||
| } | ||
| const root = hydrateRoot(container, <Page />, { onRecoverableError: error, onUncaughtError: error }) | ||
| cleanups.push(() => root.unmount()) | ||
| expect(doc.querySelector('section')).toBe(section) | ||
| expect(doc.querySelector('aside')).toBe(aside) | ||
| expect(doc.querySelector('title')).toBe(title) | ||
| expect(doc.querySelector('meta')).toBe(meta) | ||
| expect(container.querySelector('title,meta')).toBeNull() | ||
| expect(error).not.toHaveBeenCalled() | ||
| if (pending) { | ||
| expect(load).toHaveBeenCalledTimes(1) | ||
| resolve({ default: Counter }) | ||
| await vi.waitFor(() => expect(mounted).toHaveBeenCalledTimes(1)) | ||
| } | ||
| flushSync(() => doc.querySelector('button')!.click()) | ||
| expect(doc.querySelector('button')?.textContent).toBe('1') | ||
| expect(doc.querySelector('aside')).toBe(aside) | ||
| flushSync(() => root.render(<Page updated />)) | ||
| expect(doc.querySelector('title')).toBe(title) | ||
| expect(doc.querySelector('meta')).toBe(meta) | ||
| expect(title?.textContent).toBe('Updated') | ||
| expect(meta?.getAttribute('content')).toBe('Updated') | ||
| expect(error).not.toHaveBeenCalled() | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { afterEach, expect, it, vi } from 'vitest' | ||
| import { Suspense, lazy, useEffect, useLayoutEffect } from 'react' | ||
| import { hydrateRoot } from 'react-dom/client' | ||
| import { flushSync } from 'react-dom' | ||
| import { renderToString } from 'react-dom/server' | ||
|
|
||
| const cleanup: Array<() => void> = [] | ||
| afterEach(() => { for (const dispose of cleanup.splice(0)) dispose() }) | ||
|
|
||
| it.each(['resolve', 'replace'])('releases head claims from abandoned root hydration before %s', async action => { | ||
| const doc = new DOMParser().parseFromString('<html><head><title>Page</title><meta name="description" content="Page"></head><body><main><button>ready</button></main></body></html>', 'text/html') | ||
| const container = doc.querySelector('main')!, title = doc.querySelector('title')!, meta = doc.querySelector('meta')! | ||
| let resolve!: (value: { default: typeof Child }) => void | ||
| const mounted = vi.fn(), errors = vi.fn() | ||
| function Child() { useEffect(mounted, []); return <button>ready</button> } | ||
| const Lazy = lazy(() => new Promise<{ default: typeof Child }>(yes => { resolve = yes })) | ||
| const tree = <><title>Page</title><meta name="description" content="Page" /><Lazy /></> | ||
| const root = hydrateRoot(container, tree, { onRecoverableError: errors }) | ||
| cleanup.push(() => root.unmount()) | ||
| expect(mounted).not.toHaveBeenCalled() | ||
| if (action === 'resolve') resolve({ default: Child }) | ||
| else flushSync(() => root.render(<><title>Page</title><meta name="description" content="Page" /><Child /></>)) | ||
| await vi.waitFor(() => expect(mounted).toHaveBeenCalledTimes(1)) | ||
| expect(doc.querySelectorAll('title')).toHaveLength(1) | ||
| expect(doc.querySelectorAll('meta')).toHaveLength(1) | ||
| expect(doc.querySelector('title')).toBe(title) | ||
| expect(doc.querySelector('meta')).toBe(meta) | ||
| flushSync(() => root.render(<><title>Updated</title><meta name="description" content="Updated" /><Child /></>)) | ||
| expect(doc.title).toBe('Updated') | ||
| expect(title.textContent).toBe('Updated') | ||
| expect(meta.getAttribute('content')).toBe('Updated') | ||
| expect(errors).not.toHaveBeenCalled() | ||
| if (action === 'replace') { resolve({ default: Child }); await Promise.resolve(); expect(doc.title).toBe('Updated') } | ||
| }) | ||
|
|
||
| it('keeps inline metadata adoption inside the active Suspense cursor', async () => { | ||
| function First() { return <><title>first</title><meta name="description" content="first" /><button>first</button></> } | ||
| function Second() { return <><title>second</title><meta name="description" content="second" /><button>second</button></> } | ||
| const tree = <main><Suspense fallback={null}><First /></Suspense><Suspense fallback={null}><Second /></Suspense></main> | ||
| const doc = new DOMParser().parseFromString('<html><head></head><body><div></div></body></html>', 'text/html') | ||
| const container = doc.querySelector('div')! | ||
| container.innerHTML = renderToString(tree) | ||
| const titles = Array.from(container.querySelectorAll('title')), metas = Array.from(container.querySelectorAll('meta')), buttons = Array.from(container.querySelectorAll('button')) | ||
| let resolve!: (value: { default: typeof First }) => void | ||
| const Lazy = lazy(() => new Promise<{ default: typeof First }>(yes => { resolve = yes })) | ||
| const errors = vi.fn(), mounted = vi.fn() | ||
| function ReadySecond() { useLayoutEffect(mounted, []); return <Second /> } | ||
| const root = hydrateRoot(container, <main><Suspense fallback={null}><Lazy /></Suspense><Suspense fallback={null}><ReadySecond /></Suspense></main>, { onRecoverableError: errors }) | ||
| cleanup.push(() => root.unmount()) | ||
| expect(mounted).toHaveBeenCalledTimes(1) | ||
| expect(doc.head.querySelector('title')).toBe(titles[1]) | ||
| expect(container.querySelector('title')).toBe(titles[0]) | ||
| expect(doc.head.querySelector('meta')).toBe(metas[1]) | ||
| expect(errors).not.toHaveBeenCalled() | ||
| resolve({ default: First }) | ||
| await vi.waitFor(() => expect(doc.head.querySelectorAll('title')).toHaveLength(2)) | ||
| for (const title of titles) expect(title.parentNode).toBe(doc.head) | ||
| for (const meta of metas) expect(meta.parentNode).toBe(doc.head) | ||
| Array.from(container.querySelectorAll('button')).forEach((button, index) => expect(button).toBe(buttons[index])) | ||
| expect(errors).not.toHaveBeenCalled() | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: TanStack/redact
Length of output: 8674
Script executed:
Repository: TanStack/redact
Length of output: 39366
Script executed:
Repository: TanStack/redact
Length of output: 36834
Script executed:
Repository: TanStack/redact
Length of output: 20953
Script executed:
Repository: TanStack/redact
Length of output: 3564
Script executed:
Repository: TanStack/redact
Length of output: 15218
Prefer the inline candidate for Element roots.
For an Element root,
documentMetadataParentmakesfindMetadatasearch the owning document head first. Since titles have no key attributes,<title>Site</title>can be claimed for an inline<title>Widget</title>. Text hydration then reports a recoverable mismatch, and the inline title remains unclaimed.Use the active inline cursor before the document-head fallback for Element roots. Keep the existing head-first behavior for Document roots.