You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit fcc44a0
Browse filesBrowse the repository at this point in the historyBrowse files
refactor(testing): renderRoute hydrate models the root marker; shared e2e body (#992)
renderRoute({ hydrate: true }) now sets data-hydrated on <html> after
hydration, as production's root does from its effect
(browser/rsc-router.tsx), and removes it on unmount. A <Suspense> boundary
hydrates in a later pass than the tree above it, so a reader inside one is late
by structure and no test sets the attribute. The #992 regression test through
the primitive is that structural case, StrictMode on and off; on main's hook it
reports "Hydration failed because the server rendered text didn't match the
client".
The hydrate plumbing is plain locals with one return per mode, and the RTL
capability check and getConfig read live in the hydrate branch.
withoutBrowserGlobals is written for its two globals. The option's JSDoc states
the fidelity limit: a hook that reads a global that stays defined in the server
pass (history, localStorage, navigator) outside a server snapshot renders the
same in both passes and can still mismatch in real SSR.
E2E: the late-Suspense body moves to @shared/e2e
(expectLateSuspenseReaderHydratesClean) and uses guardHydrationErrors. Both
apps' fixtures hold the boundary's server content under a :gate until the test
releases it (GET <page>/:gate/release), after it has seen the root hydrate with
the fallback still on screen, so "the boundary hydrates after the root" is
asserted instead of left to a 500 ms timer. The held render polls a release
count with a 15 s safety timeout.
Also carries the CHANGELOG entries for #1017 and the comment update in
location-state-hydration.test.tsx.
Copy file name to clipboardExpand all lines: packages/rangojs-router/CHANGELOG.md
+58Lines changed: 58 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,64 @@
2
2
3
3
## Unreleased
4
4
5
+
### Fixed: `useLocationState` no longer causes a hydration mismatch when its reader hydrates inside `<Suspense>` ([#1017](https://github.com/rangojs/rango/pull/1017))
6
+
7
+
After a reload or a back/forward navigation, `history.state` still holds the
8
+
location state. The server cannot see it and renders the reader with
9
+
`undefined`. A reader at the top of the tree hydrated with `undefined` too and
10
+
picked the stored value up afterwards. A reader inside a `<Suspense>` boundary
11
+
that hydrated after the root did not: its hydrating render returned the stored
12
+
value, React reported a mismatch (`Minified React error #418` in production)
13
+
and re-rendered the boundary on the client (#992).
14
+
15
+
Every hydrating render of `useLocationState` now returns `undefined`, wherever
16
+
the reader sits, and the stored value appears on the next render.
17
+
18
+
| Reader | Hydrating render before | Hydrating render now |
| Hydrates with the root |`undefined`| unchanged |
21
+
| Inside a `<Suspense>` that hydrates after the root | the stored value |`undefined`|
22
+
| Mounted on the client (a navigation, not a reload) | the stored value | unchanged |
23
+
24
+
Nothing to change in app code. A late-hydrating reader takes one extra render,
25
+
as readers that hydrate with the root always did. Flash state behaves the same
26
+
way and is still cleared after it is read once.
27
+
28
+
### Added: `renderRoute({ hydrate: true })` hydrates the tree instead of mounting it ([#1017](https://github.com/rangojs/rango/pull/1017))
29
+
30
+
`renderRoute` from `@rangojs/router/testing/dom` mounted with `createRoot`, so
31
+
a test could not see what a hook renders on the server or while hydrating. With
32
+
`hydrate: true` it renders the same element to HTML first, puts that HTML in
33
+
the container, and hydrates it. The result gains `serverHtml` and
34
+
`recoverableErrors`, the hydration errors React reported:
- The server pass runs with `window` and `document` removed, so a
47
+
`typeof window` branch takes its server side.
48
+
- Hydrate mode sets `data-hydrated` on `<html>` in an effect, as the production
49
+
root does, and removes it on unmount. A `<Suspense>` boundary that hydrates
50
+
in a later pass is therefore "late" the way it is in a real document.
51
+
- A global that stays defined in the server pass (`history`, `localStorage`,
52
+
`navigator`) reads the same in both passes. A hook that reads one outside a
53
+
server snapshot can pass here and still mismatch in real SSR.
54
+
- Content that suspends in the server pass is emitted as its `<Suspense>`
55
+
fallback and client-rendered; React reports that in `recoverableErrors`.
56
+
- An attribute-only mismatch is not a recoverable error. React keeps the
57
+
server attribute and only logs it.
58
+
- It needs `@testing-library/react` 16.2.0 or later and throws on older
59
+
versions, which do not report hydration errors.
60
+
61
+
Without `hydrate` nothing changes.
62
+
5
63
### Fixed: a partial request ignores a `Referer` from another origin as its navigation context ([#1019](https://github.com/rangojs/rango/pull/1019))
6
64
7
65
A partial (navigation) request tells the server which page it is leaving. The
expect(serverHtml).toBe('<p data-testid="count">0</p>'); // no history.state on the server
195
+
expect(serverHtml).toContain('<p data-testid="count">0</p>'); // no history.state on the server
192
196
expect(recoverableErrors).toEqual([]); // the first client render matched it
193
197
expect(getByTestId("count").textContent).toBe("3"); // then the stored value
194
198
});
@@ -204,8 +208,8 @@ it("hydrates as the server rendered it, then shows the stored count", async () =
204
208
|`<Suspense>` whose content renders synchronously | In `serverHtml`. It hydrates in a later pass than the tree above it, after that tree's effects, before `renderRoute` resolves. |
205
209
| Content that suspends in the server pass (`lazy`, a pending or plain `use()` promise) | NOT hydrated: `serverHtml` has the fallback, the client renders the content, and `recoverableErrors` reports the boundary. With no `<Suspense>` above it `renderRoute` rejects. A settled promise (Caveats, below) stays in the server HTML. |
206
210
| Render counts and module state | Every component renders once in the server pass before its hydration render. Both passes share one module realm, so module state written while rendering is still there at hydration. |
207
-
|`localStorage`, `navigator`, `history` as bare globals | Still defined in the server pass. Only`window` and `document` are removed. |
208
-
|`data-hydrated` on `<html>`|Not set by `renderRoute` (production's root sets it from an effect). Set and remove it in the test, as above. |
211
+
|`history`, `localStorage`, `navigator` read outside a server snapshot| FIDELITY LIMIT: still defined in the server pass (only`window` and `document` are removed), so the read renders the same in both passes here and can still mismatch in real SSR.|
212
+
|`data-hydrated` on `<html>`|Set after hydration, as the production root sets it from its effect, and removed on unmount. A `<Suspense>` boundary therefore hydrates with it already set.|
209
213
210
214
- Needs `@testing-library/react` 16.2.0 or newer. 16.0 and 16.1 never pass `onRecoverableError` to `hydrateRoot`, so a mismatch would be invisible; `renderRoute` throws there instead.
211
215
-`recoverableErrors` is live: React appends to it for as long as the root is mounted.
0 commit comments