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

Add lazy route property API, use for loading middleware #13294

Merged
merged 16 commits into from
Apr 1, 2025
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
35 changes: 35 additions & 0 deletions .changeset/spicy-lamps-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
"react-router": minor
---

Add granular object-based API for `route.lazy` to support lazy loading of individual route properties, for example:

```ts
createBrowserRouter([
{
path: "/show/:showId",
lazy: {
loader: async () => (await import("./show.loader.js")).loader,
action: async () => (await import("./show.action.js")).action,
Component: async () => (await import("./show.component.js")).Component,
},
},
]);
```

**Breaking change for `route.unstable_lazyMiddleware` consumers**

The `route.unstable_lazyMiddleware` property is no longer supported. If you want to lazily load middleware, you must use the new object-based `route.lazy` API with `route.lazy.unstable_middleware`, for example:

```ts
createBrowserRouter([
{
path: "/show/:showId",
lazy: {
unstable_middleware: async () =>
(await import("./show.middleware.js")).middleware,
// etc.
},
},
]);
```
14 changes: 7 additions & 7 deletions docs/start/data/custom.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ Routes can take most of their definition lazily with the `lazy` property.
createBrowserRouter([
{
path: "/show/:showId",
lazy: () => {
let [loader, action, Component] = await Promise.all([
import("./show.action.js"),
import("./show.loader.js"),
import("./show.component.js"),
]);
return { loader, action, Component };
lazy: {
loader: async () =>
(await import("./show.loader.js")).loader,
action: async () =>
(await import("./show.action.js")).action,
Component: async () =>
(await import("./show.component.js")).Component,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh - I didn't realize our docs showed this type of granular file splitting. I feel like the more common use case is lazy: () => import("./route") so I would actually vote to document that version with a note on how to get more granular if/when needed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel this is the better API to document because it handles all properties, whereas the lazy function can't handle middleware. I also feel like this version is a bit clearer in terms of what's going on.

},
},
]);
Expand Down
230 changes: 91 additions & 139 deletions packages/react-router/__tests__/router/context-middleware-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,26 +562,30 @@ describe("context/middleware", () => {
{
id: "parent",
path: "/parent",
unstable_lazyMiddleware: async () => [
async ({ context }, next) => {
await next();
// Grab a snapshot at the end of the upwards middleware chain
snapshot = context.get(orderContext);
},
getOrderMiddleware(orderContext, "a"),
getOrderMiddleware(orderContext, "b"),
],
lazy: {
unstable_middleware: async () => [
async ({ context }, next) => {
await next();
// Grab a snapshot at the end of the upwards middleware chain
snapshot = context.get(orderContext);
},
getOrderMiddleware(orderContext, "a"),
getOrderMiddleware(orderContext, "b"),
],
},
loader({ context }) {
context.get(orderContext).push("parent loader");
},
children: [
{
id: "child",
path: "child",
unstable_lazyMiddleware: async () => [
getOrderMiddleware(orderContext, "c"),
getOrderMiddleware(orderContext, "d"),
],
lazy: {
unstable_middleware: async () => [
getOrderMiddleware(orderContext, "c"),
getOrderMiddleware(orderContext, "d"),
],
},
loader({ context }) {
context.get(orderContext).push("child loader");
},
Expand Down Expand Up @@ -634,10 +638,12 @@ describe("context/middleware", () => {
{
id: "child",
path: "child",
unstable_lazyMiddleware: async () => [
getOrderMiddleware(orderContext, "c"),
getOrderMiddleware(orderContext, "d"),
],
lazy: {
unstable_middleware: async () => [
getOrderMiddleware(orderContext, "c"),
getOrderMiddleware(orderContext, "d"),
],
},
loader({ context }) {
context.get(orderContext).push("child loader");
},
Expand All @@ -663,7 +669,7 @@ describe("context/middleware", () => {
]);
});

it("ignores middleware returned from route.lazy", async () => {
it("ignores middleware returned from route.lazy function", async () => {
let snapshot;

let consoleWarn = jest
Expand All @@ -679,15 +685,17 @@ describe("context/middleware", () => {
{
id: "parent",
path: "/parent",
unstable_lazyMiddleware: async () => [
async ({ context }, next) => {
await next();
// Grab a snapshot at the end of the upwards middleware chain
snapshot = context.get(orderContext);
},
getOrderMiddleware(orderContext, "a"),
getOrderMiddleware(orderContext, "b"),
],
lazy: {
unstable_middleware: async () => [
async ({ context }, next) => {
await next();
// Grab a snapshot at the end of the upwards middleware chain
snapshot = context.get(orderContext);
},
getOrderMiddleware(orderContext, "a"),
getOrderMiddleware(orderContext, "b"),
],
},
loader({ context }) {
context.get(orderContext).push("parent loader");
},
Expand Down Expand Up @@ -726,70 +734,6 @@ describe("context/middleware", () => {
"Route property unstable_middleware is not a supported property to be returned from a lazy route function. This property will be ignored."
);
});

it("ignores lazy middleware returned from route.lazy", async () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test was removed because route.unstable_lazyMiddleware was removed.

let snapshot;

let consoleWarn = jest
.spyOn(console, "warn")
.mockImplementation(() => {});

router = createRouter({
history: createMemoryHistory(),
routes: [
{
path: "/",
},
{
id: "parent",
path: "/parent",
unstable_lazyMiddleware: async () => [
async ({ context }, next) => {
await next();
// Grab a snapshot at the end of the upwards middleware chain
snapshot = context.get(orderContext);
},
getOrderMiddleware(orderContext, "a"),
getOrderMiddleware(orderContext, "b"),
],
loader({ context }) {
context.get(orderContext).push("parent loader");
},
children: [
{
id: "child",
path: "child",
// @ts-expect-error
lazy: async () => ({
unstable_lazyMiddleware: async () => [
getOrderMiddleware(orderContext, "c"),
getOrderMiddleware(orderContext, "d"),
],
}),
loader({ context }) {
context.get(orderContext).push("child loader");
},
},
],
},
],
});

await router.navigate("/parent/child");

expect(snapshot).toEqual([
"a middleware - before next()",
"b middleware - before next()",
"parent loader",
"child loader",
"b middleware - after next()",
"a middleware - after next()",
]);

expect(consoleWarn).toHaveBeenCalledWith(
"Route property unstable_lazyMiddleware is not a supported property to be returned from a lazy route function. This property will be ignored."
);
});
});

describe("throwing", () => {
Expand Down Expand Up @@ -1581,37 +1525,41 @@ describe("context/middleware", () => {
{
id: "parent",
path: "/parent",
unstable_lazyMiddleware: async () => [
async (_, next) => {
let res = (await next()) as Response;
res.headers.set("parent1", "yes");
return res;
},
async (_, next) => {
let res = (await next()) as Response;
res.headers.set("parent2", "yes");
return res;
},
],
lazy: {
unstable_middleware: async () => [
async (_, next) => {
let res = (await next()) as Response;
res.headers.set("parent1", "yes");
return res;
},
async (_, next) => {
let res = (await next()) as Response;
res.headers.set("parent2", "yes");
return res;
},
],
},
loader() {
return "PARENT";
},
children: [
{
id: "child",
path: "child",
unstable_lazyMiddleware: async () => [
async (_, next) => {
let res = (await next()) as Response;
res.headers.set("child1", "yes");
return res;
},
async (_, next) => {
let res = (await next()) as Response;
res.headers.set("child2", "yes");
return res;
},
],
lazy: {
unstable_middleware: async () => [
async (_, next) => {
let res = (await next()) as Response;
res.headers.set("child1", "yes");
return res;
},
async (_, next) => {
let res = (await next()) as Response;
res.headers.set("child2", "yes");
return res;
},
],
},
loader() {
return "CHILD";
},
Expand Down Expand Up @@ -2507,37 +2455,41 @@ describe("context/middleware", () => {
{
id: "parent",
path: "/parent",
unstable_lazyMiddleware: async () => [
async ({ context }, next) => {
let res = (await next()) as Response;
res.headers.set("parent1", "yes");
return res;
},
async ({ context }, next) => {
let res = (await next()) as Response;
res.headers.set("parent2", "yes");
return res;
},
],
lazy: {
unstable_middleware: async () => [
async ({ context }, next) => {
let res = (await next()) as Response;
res.headers.set("parent1", "yes");
return res;
},
async ({ context }, next) => {
let res = (await next()) as Response;
res.headers.set("parent2", "yes");
return res;
},
],
},
loader() {
return new Response("PARENT");
},
children: [
{
id: "child",
path: "child",
unstable_lazyMiddleware: async () => [
async ({ context }, next) => {
let res = (await next()) as Response;
res.headers.set("child1", "yes");
return res;
},
async ({ context }, next) => {
let res = (await next()) as Response;
res.headers.set("child2", "yes");
return res;
},
],
lazy: {
unstable_middleware: async () => [
async ({ context }, next) => {
let res = (await next()) as Response;
res.headers.set("child1", "yes");
return res;
},
async ({ context }, next) => {
let res = (await next()) as Response;
res.headers.set("child2", "yes");
return res;
},
],
},
loader({ context }) {
return new Response("CHILD");
},
Expand Down
Loading