Skip to content

feat: support loader/action requestContext on the client #10895

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

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 7 additions & 0 deletions packages/react-router-dom/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ interface DOMRouterOpts {
future?: Partial<Omit<RouterFutureConfig, "v7_prependBasename">>;
hydrationData?: HydrationState;
window?: Window;

/**
* This value is passed to the `requestContext` property in your loader/action functions.
*/
requestContext?: unknown;
}

export function createBrowserRouter(
Expand All @@ -234,6 +239,7 @@ export function createBrowserRouter(
hydrationData: opts?.hydrationData || parseHydrationData(),
routes,
mapRouteProperties,
requestContext: opts?.requestContext,
}).initialize();
}

Expand All @@ -251,6 +257,7 @@ export function createHashRouter(
hydrationData: opts?.hydrationData || parseHydrationData(),
routes,
mapRouteProperties,
requestContext: opts?.requestContext,
}).initialize();
}

Expand Down
12 changes: 9 additions & 3 deletions packages/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ export interface RouterInit {
future?: Partial<FutureConfig>;
hydrationData?: HydrationState;
window?: Window;
requestContext?: unknown;
}

/**
Expand Down Expand Up @@ -866,6 +867,8 @@ export function createRouter(init: RouterInit): Router {
// a POP navigation that was blocked by the user without touching router state
let ignoreNextHistoryUpdate = false;

let requestContext = init.requestContext;

// Initialize the router, all side effects should be kicked off from here.
// Implemented as a Fluent API for ease of:
// let router = createRouter(init).initialize();
Expand Down Expand Up @@ -1387,7 +1390,8 @@ export function createRouter(init: RouterInit): Router {
matches,
manifest,
mapRouteProperties,
basename
basename,
{ requestContext }
);

if (request.signal.aborted) {
Expand Down Expand Up @@ -2193,7 +2197,8 @@ export function createRouter(init: RouterInit): Router {
matches,
manifest,
mapRouteProperties,
basename
basename,
{ requestContext }
)
),
...fetchersToLoad.map((f) => {
Expand All @@ -2205,7 +2210,8 @@ export function createRouter(init: RouterInit): Router {
f.matches,
manifest,
mapRouteProperties,
basename
basename,
{ requestContext }
);
} else {
let error: ErrorResult = {
Expand Down