-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Add client middleware to split route modules #13210
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5938d4a
Add client middleware to split route modules
markdalgleish dbbc3e1
Fix race condition in tests
markdalgleish b040fc9
Remove future flag usage
markdalgleish 8090c55
Sort requests array before asserting contents
markdalgleish e52371a
Merge branch 'dev' into markdalgleish/lazy-middleware
markdalgleish 1030f20
Update packages/react-router/lib/router/router.ts
markdalgleish 3720e54
Update changesets
markdalgleish e878b8d
Merge branch 'dev' into markdalgleish/lazy-middleware
markdalgleish File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@react-router/dev": patch | ||
--- | ||
|
||
When both `future.unstable_middleware` and `future.unstable_splitRouteModules` are enabled, split `unstable_clientMiddleware` route exports into separate chunks when possible |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
"react-router": patch | ||
--- | ||
|
||
Add support for `route.unstable_lazyMiddleware` function to allow lazy loading of middleware logic. | ||
|
||
**Breaking change for `unstable_middleware` consumers** | ||
|
||
The `route.unstable_middleware` property is no longer supported in the return value from `route.lazy`. If you want to lazily load middleware, you must use `route.unstable_lazyMiddleware`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@react-router/dev": patch | ||
--- | ||
|
||
Improve performance of `future.unstable_middleware` by ensuring that route modules are only blocking during the middleware phase when the `unstable_clientMiddleware` has been defined |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,6 +113,97 @@ test.describe("Middleware", () => { | |
appFixture.close(); | ||
}); | ||
|
||
test("calls clientMiddleware before/after loaders with split route modules", async ({ | ||
page, | ||
}) => { | ||
let fixture = await createFixture({ | ||
spaMode: true, | ||
files: { | ||
"react-router.config.ts": reactRouterConfig({ | ||
ssr: false, | ||
middleware: true, | ||
splitRouteModules: true, | ||
}), | ||
"vite.config.ts": js` | ||
import { defineConfig } from "vite"; | ||
import { reactRouter } from "@react-router/dev/vite"; | ||
|
||
export default defineConfig({ | ||
build: { manifest: true, minify: false }, | ||
plugins: [reactRouter()], | ||
}); | ||
`, | ||
"app/context.ts": js` | ||
import { unstable_createContext } from 'react-router' | ||
export const orderContext = unstable_createContext([]); | ||
`, | ||
"app/routes/_index.tsx": js` | ||
import { Link } from 'react-router' | ||
import { orderContext } from '../context' | ||
|
||
export const unstable_clientMiddleware = [ | ||
({ context }) => { | ||
context.set(orderContext, [...context.get(orderContext), 'a']); | ||
}, | ||
({ context }) => { | ||
context.set(orderContext, [...context.get(orderContext), 'b']); | ||
}, | ||
]; | ||
|
||
export async function clientLoader({ request, context }) { | ||
return context.get(orderContext).join(','); | ||
} | ||
|
||
export default function Component({ loaderData }) { | ||
return ( | ||
<> | ||
<h2 data-route>Index: {loaderData}</h2> | ||
<Link to="/about">Go to about</Link> | ||
</> | ||
); | ||
} | ||
`, | ||
"app/routes/about.tsx": js` | ||
import { orderContext } from '../context' | ||
|
||
export const unstable_clientMiddleware = [ | ||
({ context }) => { | ||
context.set(orderContext, [...context.get(orderContext), 'c']); | ||
}, | ||
({ context }) => { | ||
context.set(orderContext, [...context.get(orderContext), 'd']); | ||
}, | ||
]; | ||
|
||
export async function clientLoader({ context }) { | ||
return context.get(orderContext).join(','); | ||
} | ||
|
||
export default function Component({ loaderData }) { | ||
return <h2 data-route>About: {loaderData}</h2>; | ||
} | ||
`, | ||
}, | ||
}); | ||
|
||
let appFixture = await createAppFixture(fixture); | ||
|
||
let app = new PlaywrightFixture(appFixture, page); | ||
await app.goto("/"); | ||
await page.waitForSelector('[data-route]:has-text("Index")'); | ||
expect(await page.locator("[data-route]").textContent()).toBe( | ||
"Index: a,b" | ||
); | ||
|
||
(await page.$('a[href="/about"]'))?.click(); | ||
await page.waitForSelector('[data-route]:has-text("About")'); | ||
expect(await page.locator("[data-route]").textContent()).toBe( | ||
"About: c,d" | ||
); | ||
|
||
appFixture.close(); | ||
}); | ||
|
||
test("calls clientMiddleware before/after actions", async ({ page }) => { | ||
let fixture = await createFixture({ | ||
spaMode: true, | ||
|
@@ -596,6 +687,94 @@ test.describe("Middleware", () => { | |
appFixture.close(); | ||
}); | ||
|
||
test("calls clientMiddleware before/after loaders with split route modules", async ({ | ||
page, | ||
}) => { | ||
let fixture = await createFixture({ | ||
files: { | ||
"react-router.config.ts": reactRouterConfig({ | ||
middleware: true, | ||
splitRouteModules: true, | ||
}), | ||
"vite.config.ts": js` | ||
import { defineConfig } from "vite"; | ||
import { reactRouter } from "@react-router/dev/vite"; | ||
|
||
export default defineConfig({ | ||
build: { manifest: true, minify: false }, | ||
plugins: [reactRouter()], | ||
}); | ||
`, | ||
"app/context.ts": js` | ||
import { unstable_createContext } from 'react-router' | ||
export const orderContext = unstable_createContext([]); | ||
`, | ||
"app/routes/_index.tsx": js` | ||
import { Link } from 'react-router' | ||
import { orderContext } from "../context";; | ||
|
||
export const unstable_clientMiddleware = [ | ||
({ context }) => { | ||
context.set(orderContext, [...context.get(orderContext), 'a']); | ||
}, | ||
({ context }) => { | ||
context.set(orderContext, [...context.get(orderContext), 'b']); | ||
}, | ||
]; | ||
|
||
export async function clientLoader({ request, context }) { | ||
return context.get(orderContext).join(','); | ||
} | ||
|
||
export default function Component({ loaderData }) { | ||
return ( | ||
<> | ||
<h2 data-route>Index: {loaderData}</h2> | ||
<Link to="/about">Go to about</Link> | ||
</> | ||
); | ||
} | ||
`, | ||
"app/routes/about.tsx": js` | ||
import { orderContext } from "../context";; | ||
export const unstable_clientMiddleware = [ | ||
({ context }) => { | ||
context.set(orderContext, ['c']); // reset order from hydration | ||
}, | ||
({ context }) => { | ||
context.set(orderContext, [...context.get(orderContext), 'd']); | ||
}, | ||
]; | ||
|
||
export async function clientLoader({ context }) { | ||
return context.get(orderContext).join(','); | ||
} | ||
|
||
export default function Component({ loaderData }) { | ||
return <h2 data-route>About: {loaderData}</h2>; | ||
} | ||
`, | ||
}, | ||
}); | ||
|
||
let appFixture = await createAppFixture(fixture); | ||
|
||
let app = new PlaywrightFixture(appFixture, page); | ||
await app.goto("/"); | ||
await page.waitForSelector('[data-route]:has-text("Index")'); | ||
expect(await page.locator("[data-route]").textContent()).toBe( | ||
"Index: a,b" | ||
); | ||
|
||
(await page.$('a[href="/about"]'))?.click(); | ||
await page.waitForSelector('[data-route]:has-text("About")'); | ||
expect(await page.locator("[data-route]").textContent()).toBe( | ||
"About: c,d" | ||
); | ||
|
||
appFixture.close(); | ||
}); | ||
|
||
test("calls clientMiddleware before/after actions", async ({ page }) => { | ||
let fixture = await createFixture({ | ||
files: { | ||
|
@@ -1074,7 +1253,7 @@ test.describe("Middleware", () => { | |
await page.waitForSelector("[data-child]"); | ||
|
||
// 2 separate server requests made | ||
expect(requests).toEqual([ | ||
expect(requests.sort()).toEqual([ | ||
expect.stringContaining("/parent/child.data?_routes=routes%2Fparent"), | ||
expect.stringContaining( | ||
"/parent/child.data?_routes=routes%2Fparent.child" | ||
|
@@ -1236,15 +1415,15 @@ test.describe("Middleware", () => { | |
await page.waitForSelector("[data-action]"); | ||
|
||
// 2 separate server requests made | ||
expect(requests).toEqual([ | ||
// index gets it's own due to clientLoader | ||
expect.stringMatching( | ||
/\/parent\/child\.data\?_routes=routes%2Fparent\.child\._index$/ | ||
), | ||
expect(requests.sort()).toEqual([ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. |
||
// This is the normal request but only included parent.child because parent opted out | ||
expect.stringMatching( | ||
/\/parent\/child\.data\?_routes=routes%2Fparent\.child$/ | ||
), | ||
// index gets it's own due to clientLoader | ||
expect.stringMatching( | ||
/\/parent\/child\.data\?_routes=routes%2Fparent\.child\._index$/ | ||
), | ||
]); | ||
|
||
// But client middlewares only ran once for the action and once for the revalidation | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test was flaky in Firefox due to the order of requests not being deterministic.