Environment
- Framework: React Router v7 (with middleware enabled)
- Platform: Vercel
- Build Status:
Successful - Runtime:
Function invocation fails with 500 error
Problem
Deploying a React Router v7 application with middleware to Vercel results in successful builds but runtime failures. The application works locally but fails when deployed.
Error Message
Error: Invalid `context` value provided to `handleRequest`.
When middleware is enabled you must return an instance of
`RouterContextProvider` from your `getLoadContext` function.
Full Stack Trace
[error] Error: Invalid `context` value provided to `handleRequest`. When middleware is enabled you must return an instance of `RouterContextProvider` from your `getLoadContext` function.
at Object.requestHandler [as handlerWeb] (/var/task/node_modules/react-router/dist/development/index.js:1153:21)
at Object.handler (/opt/rust/nodejs.js:2:15439)
at Server.<anonymous> (/opt/rust/nodejs.js:2:1980)
at /opt/rust/nodejs.js:16:7828
at AsyncLocalStorage.run (node:internal/async_local_storage/async_hooks:91:14)
at /opt/rust/nodejs.js:16:7816
at AsyncLocalStorage.run (node:internal/async_local_storage/async_hooks:91:14)
at Server.<anonymous> (/opt/rust/nodejs.js:16:7804)
at Server.s (/opt/rust/nodejs.js:16:1716)
at Server.emit (node:events:518:28)
Repository
Steps to Reproduce
- Create a React Router v7 from repo (main)
- Deploy to Vercel
- Build completes successfully
- Access any route - function invocation fails with 500 error and there is “Unexpected Server Error” message at the page: https://react-router-i18next-example-vercel-521qac7hs.vercel.app/
Expected Behavior
The application should run successfully on Vercel, just as it does in local development.
Additional Context
- Local development works without issues
- The error suggests a mismatch between React Router v7’s middleware expectations and Vercel’s serverless function adapter
- This appears to be related to how Vercel’s build output API handles the
getLoadContextfunction with React Router v7 middleware
Possible Solution
The issue might be related to how Vercel’s adapter wraps the React Router handler. React Router v7 with middleware requires getLoadContext to return a RouterContextProvider instance, but Vercel’s runtime might not be handling this correctly.