Skip to content

Commit

Permalink
Deferred API updates (#9070)
Browse files Browse the repository at this point in the history
* chore: remove Deferrable/ResolvedDeferrable types for raw Promises

* feat: support deferred arrays

* chore:" remove typings from Deferred and useDeferredData

To be added back in once we figur eout typing for useLoaderData in 6.5

* Handle deferred render errors at the boundary

* internal renames

* do not handle fallback render errors in deferred error boundary

* Remove Suspense from inside Deferred

* Rename test files to align casing

* Update changeset for Suspense removal

* Support deferred(promise)

* Change deferred typings from any -> unknown

* Add changeset:

* Bump bundle

* add tests for data memory router arrays/single deferreds
  • Loading branch information
brophdawg11 authored Jul 20, 2022
1 parent 6ac05b4 commit 92aa5bb
Show file tree
Hide file tree
Showing 15 changed files with 811 additions and 450 deletions.
21 changes: 11 additions & 10 deletions .changeset/light-months-argue.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@ function loader() {
};

// In your route element, grab the values from useLoaderData and render them
// with <Deferred>
// with <Deferred> inside a <Suspense> boundary
function DeferredPage() {
let data = useLoaderData();
return (
<>
<p>Critical Data: {data.critical}</p>
<Deferred
value={data.lazy}
fallbackElement={<p>Loading...</p>}
errorElement={<RenderDeferredError />}>
<RenderDeferredData />
</Deferred>
<Suspense fallback={<p>Loading...</p>}>
<Deferred value={data.lazy} errorElement={<RenderDeferredError />}>
<RenderDeferredData />
</Deferred>
</Suspense>
</>
);
}
Expand All @@ -57,9 +56,11 @@ function DeferredPage() {
return (
<>
<p>Critical Data: {data.critical}</p>
<Deferred value={data.lazy} fallbackElement={<p>Loading...</p>}>
{(data) => <p>{data}</p>}
</Deferred>
<Suspense fallback={<p>Loading...</p>}>
<Deferred value={data.lazy} errorElement={<RenderDeferredError />}>
{(data) => <p>{data}</p>}
</Deferred>
</Suspense>
</>
);
}
Expand Down
14 changes: 14 additions & 0 deletions .changeset/new-kiwis-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
"react-router": patch
"@remix-run/router": patch
---

Deferred API Updates

- Removes `<Suspense>` from inside `<Deferred>`, requires users to render their own suspense boundaries
- Updates `Deferred` to use a true error boundary to catch render errors as well as data errors
- Support array and single promise usages
- `return deferred([ await critical(), lazy() ])`
- `return deferred(lazy())`
- Remove `Deferrable`/`ResolvedDeferrable` in favor of raw `Promise`'s and `Awaited`
- Remove generics from `useDeferredData` until `useLoaderData` generic is decided in 6.5
70 changes: 39 additions & 31 deletions examples/data-router/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from "react";
import {
type ActionFunction,
type Deferrable,
type LoaderFunction,
DataBrowserRouter,
Deferred,
Expand Down Expand Up @@ -238,11 +237,11 @@ function Todo() {
interface DeferredRouteLoaderData {
critical1: string;
critical2: string;
lazyResolved: Deferrable<string>;
lazy1: Deferrable<string>;
lazy2: Deferrable<string>;
lazy3: Deferrable<string>;
lazyError: Deferrable<string>;
lazyResolved: Promise<string>;
lazy1: Promise<string>;
lazy2: Promise<string>;
lazy3: Promise<string>;
lazyError: Promise<string>;
}

const rand = () => Math.round(Math.random() * 100);
Expand Down Expand Up @@ -270,28 +269,35 @@ function DeferredPage() {
<div>
<p>{data.critical1}</p>
<p>{data.critical2}</p>
<Deferred
value={data.lazyResolved}
fallbackElement={<p>should not see me!</p>}
>
<RenderDeferredData />
</Deferred>
<Deferred value={data.lazy1} fallbackElement={<p>loading 1...</p>}>
<RenderDeferredData />
</Deferred>
<Deferred value={data.lazy2} fallbackElement={<p>loading 2...</p>}>
<RenderDeferredData />
</Deferred>
<Deferred value={data.lazy3} fallbackElement={<p>loading 3...</p>}>
{(data) => <p>{data}</p>}
</Deferred>
<Deferred
value={data.lazyError}
fallbackElement={<p>loading (error)...</p>}
errorElement={<RenderDeferredError />}
>
<RenderDeferredData />
</Deferred>

<React.Suspense fallback={<p>should not see me!</p>}>
<Deferred value={data.lazyResolved}>
<RenderDeferredData />
</Deferred>
</React.Suspense>

<React.Suspense fallback={<p>loading 1...</p>}>
<Deferred value={data.lazy1}>
<RenderDeferredData />
</Deferred>
</React.Suspense>

<React.Suspense fallback={<p>loading 2...</p>}>
<Deferred value={data.lazy2}>
<RenderDeferredData />
</Deferred>
</React.Suspense>

<React.Suspense fallback={<p>loading 3...</p>}>
<Deferred value={data.lazy3}>{(data) => <p>{data}</p>}</Deferred>
</React.Suspense>

<React.Suspense fallback={<p>loading (error)...</p>}>
<Deferred value={data.lazyError} errorElement={<RenderDeferredError />}>
<RenderDeferredData />
</Deferred>
</React.Suspense>

<Outlet />
</div>
);
Expand All @@ -314,9 +320,11 @@ function DeferredChild() {
return (
<div>
<p>{data.critical}</p>
<Deferred value={data.lazy} fallbackElement={<p>loading child...</p>}>
<RenderDeferredData />
</Deferred>
<React.Suspense fallback={<p>loading child...</p>}>
<Deferred value={data.lazy}>
<RenderDeferredData />
</Deferred>
</React.Suspense>
<Form method="post">
<button type="submit" name="key" value="value">
Submit
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
},
"filesize": {
"packages/router/dist/router.js": {
"none": "95 kB"
"none": "96 kB"
},
"packages/react-router/dist/react-router.production.min.js": {
"none": "11 kB"
Expand Down
1 change: 0 additions & 1 deletion packages/react-router-dom/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ export type {
ActionFunctionArgs,
DataMemoryRouterProps,
DataRouteMatch,
Deferrable,
DeferredProps,
Fetcher,
Hash,
Expand Down
1 change: 0 additions & 1 deletion packages/react-router-native/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export type {
ActionFunctionArgs,
DataMemoryRouterProps,
DataRouteMatch,
Deferrable,
DeferredProps,
Fetcher,
Hash,
Expand Down
19 changes: 10 additions & 9 deletions packages/react-router/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@
return (
<>
<p>Critical Data: {data.critical}</p>
<Deferred
value={data.lazy}
fallbackElement={<p>Loading...</p>}
errorElement={<RenderDeferredError />}>
<RenderDeferredData />
</Deferred>
<Suspense fallback={<p>Loading...</p>}>
<Deferred value={data.lazy} errorElement={<RenderDeferredError />}>
<RenderDeferredData />
</Deferred>
</Suspense>
</>
);
}
Expand All @@ -58,9 +57,11 @@
return (
<>
<p>Critical Data: {data.critical}</p>
<Deferred value={data.lazy} fallbackElement={<p>Loading...</p>}>
{(data) => <p>{data}</p>}
</Deferred>
<Suspense fallback={<p>Loading...</p>}>
<Deferred value={data.lazy} errorElement={<RenderDeferredError />}>
{(data) => <p>{data}</p>}
</Deferred>
</Suspense>
</>
);
}
Expand Down
Loading

0 comments on commit 92aa5bb

Please sign in to comment.