Skip to content

Change middleware return type from void to undefined #13199

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 1 commit into from
Mar 14, 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
5 changes: 5 additions & 0 deletions .changeset/unlucky-pumas-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router": patch
---

[UNSTABLE] Update `Route.unstable_MiddlewareFunction` to have a return value of `Response | undefined` instead of `Response | void` becaue you should not return anything if you aren't returning the `Response`
3 changes: 1 addition & 2 deletions packages/react-router/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import type {
DataStrategyResult,
ImmutableRouteKey,
MapRoutePropertiesFunction,
MaybePromise,
MutationFormMethod,
RedirectResult,
RouteData,
Expand Down Expand Up @@ -61,8 +62,6 @@ import {
//#region Types and Constants
////////////////////////////////////////////////////////////////////////////////

type MaybePromise<T> = T | Promise<T>;

/**
* A Router instance manages all navigation and data loading/mutations
*/
Expand Down
10 changes: 6 additions & 4 deletions packages/react-router/lib/router/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { Equal, Expect } from "../types/utils";
import type { Location, Path, To } from "./history";
import { invariant, parsePath, warning } from "./history";

export type MaybePromise<T> = T | Promise<T>;

/**
* Map of routeId -> data returned from a loader/action/error
*/
Expand Down Expand Up @@ -206,7 +208,7 @@ interface DataFunctionArgs<Context> {
* middlewares from the bottom-up
*/
export interface unstable_MiddlewareNextFunction<Result = unknown> {
(): Result | Promise<Result>;
(): MaybePromise<Result>;
}

/**
Expand All @@ -218,7 +220,7 @@ export interface unstable_MiddlewareNextFunction<Result = unknown> {
export type unstable_MiddlewareFunction<Result = unknown> = (
args: DataFunctionArgs<unstable_RouterContextProvider>,
next: unstable_MiddlewareNextFunction<Result>
) => Result | Promise<Result>;
) => MaybePromise<Result | undefined>;

/**
* Arguments passed to loader functions
Expand All @@ -237,7 +239,7 @@ export interface ActionFunctionArgs<Context = any>
*/
type DataFunctionValue = unknown;

type DataFunctionReturnValue = Promise<DataFunctionValue> | DataFunctionValue;
type DataFunctionReturnValue = MaybePromise<DataFunctionValue>;

/**
* Route loader function signature
Expand Down Expand Up @@ -373,7 +375,7 @@ export type AgnosticPatchRoutesOnNavigationFunction<
M extends AgnosticRouteMatch = AgnosticRouteMatch
> = (
opts: AgnosticPatchRoutesOnNavigationFunctionArgs<O, M>
) => void | Promise<void>;
) => MaybePromise<void>;

/**
* Function provided by the framework-aware layers to set any framework-specific
Expand Down
2 changes: 1 addition & 1 deletion packages/react-router/lib/types/route-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ type ServerDataFunctionArgs<T extends RouteInfo> = {
export type CreateServerMiddlewareFunction<T extends RouteInfo> = (
args: ServerDataFunctionArgs<T>,
next: unstable_MiddlewareNextFunction<Response>
) => MaybePromise<Response | void>;
) => MaybePromise<Response | undefined>;

export type CreateClientMiddlewareFunction<T extends RouteInfo> = (
args: ClientDataFunctionArgs<T>,
Expand Down