mirror of
https://github.com/cloudflare/vinext.git
synced 2026-09-14 19:04:59 +08:00
0dbfeff1af
* Extract app page route wiring helpers
* Add slot client primitives
* Fix app page error boundary serialization
* Fix client error boundary pathname reset
* Document Next.js error boundary verification
* Implement flat App Router payload for layout persistence
* fix: address review findings in flat payload implementation
- Fix stale closure on readBrowserRouterState by using a useRef updated
synchronously during render instead of a closure captured in
useLayoutEffect. External callers (navigate, server actions, HMR) now
always read the current router state.
- Restore GlobalErrorBoundary wrapping that was dropped when switching
from buildPageElement to buildAppPageElements. Apps with
app/global-error.tsx now get their global error boundary back.
- Add exhaustive default case to routerReducer so new action types
produce a compile error and a runtime throw instead of silent undefined.
- Remove dead code: createRouteNodeSnapshot, AppRouteNodeSnapshot,
AppRouteNodeValue were defined but never imported.
- Remove deprecated buildAppPageRouteElement and its test — no
production callers remain after the flat payload cutover.
- Short-circuit normalizeAppElements when no slot keys need rewriting
to avoid unnecessary allocation on every payload.
- Align test data in error boundary RSC payload test (matchedParams
slug: "post" -> "missing" to match requestUrl /posts/missing).
* fix: normalize flat payload after use(), not before
createFromReadableStream() returns a React thenable whose .then()
returns undefined (not a Promise). Chaining .then(normalizeAppElements)
broke SSR by assigning undefined to flightRoot.
Fix: call use() on the raw thenable, then normalize synchronously
after resolution. Also widen renderAppPageLifecycle element type to
accept flat map payloads.
* fix: produce flat RSC payload on all rendering paths
The SSR entry always expects a flat Record<string, ReactNode> with
__route and __rootLayout metadata from the RSC stream. Three paths
were still producing bare ReactNode payloads:
1. renderAppPageBoundaryElementResponse only created the flat map for
isRscRequest=true, but HTML requests also flow through RSC→SSR
2. buildPageElements "no default export" early return
3. Server action "Page not found" fallback
All three now produce the flat keyed element map, fixing 17 test
failures across 404/not-found, forbidden/unauthorized, error boundary,
production build, rewrite, and encoded-slash paths.
* test: update unit tests for flat RSC payload on all paths
- Update renderElementToStream mock to extract the route element from
the flat map before rendering to HTML (mirrors real SSR entry flow)
- Update entry template snapshots for the buildPageElements changes
* fix: wrap Flight thenable in Promise.resolve() before chaining .then()
createFromReadableStream() returns a React Flight thenable whose
.then() returns undefined instead of a new Promise. The browser
entry's normalizeAppElementsPromise chained .then() on this raw
thenable, producing undefined — which crashed use() during hydration
with "An unsupported type was passed to use(): undefined".
Wrapping in Promise.resolve() first converts the Flight thenable
into a real Promise, making .then() chains work correctly.
The same fix was already applied to the SSR entry in 5395efc but
was missed in the browser entry.
* fix: eliminate Promise from ElementsContext to fix React 19 hydration
React 19.2.4's use(Promise) during hydration triggers "async Client
Component" because native Promises lack React's internal .status
property (set only by Flight thenables). When use() encounters a
Promise without .status, it suspends — which React interprets as the
component being async, causing a fatal error.
Fix: store resolved AppElements directly in ElementsContext and
router state instead of Promise<AppElements>. The navigation async
flow (createPendingNavigationCommit) awaits the Promise before
dispatching, so React state never holds a Promise.
- ElementsContext: Promise<AppElements> → AppElements
- AppRouterState.elements: Promise<AppElements> → AppElements
- mergeElementsPromise → mergeElements (sync object spread)
- Slot: useContext only, no use(Promise)
- SSR entry: pass resolved elements to context
- dispatchBrowserTree: simplified, no async error handler
Also fix flaky instrumentation E2E test that read the last error
entry instead of finding by path.
* test: update slot and browser state tests for resolved ElementsContext
- Remove Promise wrappers from ElementsContext test values
- mergeElementsPromise → mergeElements (sync)
- Replace Suspense streaming test with direct render test
- Remove unused createDeferred helper and Suspense import
- Update browser state test assertions (no longer async)
* ci: retrigger
* fix: address code review findings (P1-P3)
P1a: mergeElements preserves previous slot content when the new payload
marks a parallel slot as unmatched. On soft navigation, unmatched slots
keep their previous subtree instead of triggering notFound().
P1b: renderNavigationPayload now receives navId and checks for
superseded navigations after its await. Stale payloads are discarded
instead of being dispatched into the React tree.
P2: The catch block in renderNavigationPayload only calls
commitClientNavigationState() when activateNavigationSnapshot() was
actually reached, preventing counter underflow.
P3: The no-default-export fallback in buildPageElements now derives
the root layout tree path from route.layoutTreePositions and
route.routeSegments instead of hardcoding "/".
* fix: avoid serializing app render dependency wrappers
* Fix flat payload dependency barriers
* Fix template-only route wrappers
* chore: trigger CI review
* fix: skip Slot wrapping for layout entries without a default export
When a layout entry exists but has no default export, the element is
never written to the flat map. The unconditional Slot wrapping would
return null (id not in elements), silently dropping the entire route
subtree below that layout level.
Guard the Slot wrapping with a check for the layout component. When
absent, pass layoutChildren through directly — preserving the
LayoutSegmentProvider and error/not-found boundaries while skipping
the Slot indirection.
* fix: restore merged app router entry behavior
* fix: address app router review regressions
* Fix app-page-request intercept tests
* Address PR 2c review follow-ups
* Refactor same-url payload commits
* Clarify PR 2c browser invariants
* Update App Router entry snapshots
* Tighten App Router review follow-ups
* Align App Router entry helper usage
* Tighten browser commit invariants
* fix(app-router): preserve scoped parallel slot identity
* test(app-router): align slot identity assertions
* fix(app-router): keep boundary root layout metadata unknown
* docs(app-router): refresh stale review comments
84 lines
2.3 KiB
TypeScript
84 lines
2.3 KiB
TypeScript
import { createElement } from "react";
|
|
import { renderToReadableStream } from "react-dom/server.edge";
|
|
import { describe, expect, it } from "vite-plus/test";
|
|
import {
|
|
createAppRenderDependency,
|
|
renderAfterAppDependencies,
|
|
renderWithAppDependencyBarrier,
|
|
} from "../packages/vinext/src/server/app-render-dependency.js";
|
|
|
|
async function readStream(stream: ReadableStream<Uint8Array>): Promise<string> {
|
|
const reader = stream.getReader();
|
|
const decoder = new TextDecoder();
|
|
let text = "";
|
|
|
|
for (;;) {
|
|
const { done, value } = await reader.read();
|
|
if (done) {
|
|
break;
|
|
}
|
|
text += decoder.decode(value, { stream: true });
|
|
}
|
|
|
|
return text + decoder.decode();
|
|
}
|
|
|
|
async function renderHtml(element: React.ReactNode): Promise<string> {
|
|
const stream = await renderToReadableStream(element, {
|
|
onError(error: unknown) {
|
|
throw error instanceof Error ? error : new Error(String(error));
|
|
},
|
|
});
|
|
await stream.allReady;
|
|
return readStream(stream);
|
|
}
|
|
|
|
describe("app render dependency helpers", () => {
|
|
it("documents that React can render a sync sibling before an async sibling completes", async () => {
|
|
let activeLocale = "en";
|
|
|
|
async function LocaleLayout() {
|
|
await Promise.resolve();
|
|
activeLocale = "de";
|
|
return createElement("div", null, "layout");
|
|
}
|
|
|
|
function LocalePage() {
|
|
return createElement("p", null, `page:${activeLocale}`);
|
|
}
|
|
|
|
const body = await renderHtml(
|
|
createElement("div", null, createElement(LocaleLayout), createElement(LocalePage)),
|
|
);
|
|
|
|
expect(body).toContain("page:en");
|
|
});
|
|
|
|
it("waits to serialize dependent entries until the barrier entry has rendered", async () => {
|
|
let activeLocale = "en";
|
|
const layoutDependency = createAppRenderDependency();
|
|
|
|
async function LocaleLayout() {
|
|
await Promise.resolve();
|
|
activeLocale = "de";
|
|
return createElement("div", null, renderWithAppDependencyBarrier("layout", layoutDependency));
|
|
}
|
|
|
|
function LocalePage() {
|
|
return createElement("p", null, `page:${activeLocale}`);
|
|
}
|
|
|
|
const body = await renderHtml(
|
|
createElement(
|
|
"div",
|
|
null,
|
|
createElement(LocaleLayout),
|
|
renderAfterAppDependencies(createElement(LocalePage), [layoutDependency]),
|
|
),
|
|
);
|
|
|
|
expect(body).toContain("page:de");
|
|
expect(body).not.toContain("page:en");
|
|
});
|
|
});
|