Skip to content
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

Deferred API updates #9070

Merged
merged 15 commits into from
Jul 20, 2022
Merged
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
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