Skip to content

Conversation

brophdawg11
Copy link
Contributor

Currently, we return/bubble nothing in client middlewares because there's no Response, but this means you can't do post processing which eliminates one of our primary example use cases of middlewares which is looking up CMS redirects on 404s.

Middleware is implemented as part of dataStrategy and therefore the expected return value internally is the same thing returned by dataStrategy (Record<string, DataStrategyResult>) so it's already public API, so we can just stop purposefully hiding that from userland and bubble it back up (they don't need to use it though). This also has the added benefit of removing another key difference (both in explanation and in implementation) between server and client middleware and makes them almost identical the the only difference being type of the next return value.

That example CMS use case in the client would now look like this:

async function cmsFallbackMiddleware({ request }, next) {
  const results = await next();

  // Check if we got a 404 from any of our routes
  let is404 =
    isRouteErrorResponse(r.result) &&
    r.result.status === 404;
  if (Object.values(results).some((r) => is404(r))) {
    // Check CMS for a redirect
    const cmsRedirect = await checkCMSRedirects(
      request.url,
    );
    if (cmsRedirect) {
      throw redirect(cmsRedirect, 302);
    }
  }

  return results;

Copy link

changeset-bot bot commented Aug 11, 2025

🦋 Changeset detected

Latest commit: f2eac66

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 11 packages
Name Type
react-router Patch
@react-router/architect Patch
@react-router/cloudflare Patch
@react-router/dev Patch
react-router-dom Patch
@react-router/express Patch
@react-router/node Patch
@react-router/serve Patch
@react-router/fs-routes Patch
@react-router/remix-routes-option-adapter Patch
create-react-router Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@brophdawg11 brophdawg11 changed the title Update client middleware to propagate a the data strategy results up the chain Update client middleware to propagate data strategy results up the chain Aug 11, 2025
@MichaelDeBoey MichaelDeBoey changed the title Update client middleware to propagate data strategy results up the chain feat(react-router): Update client middleware to propagate data strategy results up the chain Aug 13, 2025
const results = await next();

// Check if we got a 404 from any of our routes
let is404 =
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can this be simplified to not need to return the results?

@brophdawg11 brophdawg11 merged commit bd341ff into dev Aug 15, 2025
5 checks passed
@brophdawg11 brophdawg11 deleted the brophdawg11/client-middleware-return branch August 15, 2025 21:03
joseph0926 pushed a commit to joseph0926/react-router that referenced this pull request Aug 17, 2025
Copy link
Contributor

🤖 Hello there,

We just published version 7.8.2-pre.0 which includes this pull request. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

Copy link
Contributor

🤖 Hello there,

We just published version 7.8.2 which includes this pull request. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants