You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Error: No value found for context
at unstable_RouterContextProvider.get (/home/vlady/code/gyarns/node_modules/.vite/deps_ssr/chunk-QZO7VHZY.js:1384:11)
at authMiddleware (/home/vlady/code/gyarns/app/routes/auth/auth-middleware.ts:19:28)
at callRouteMiddleware (/home/vlady/code/gyarns/node_modules/.vite/deps_ssr/chunk-QZO7VHZY.js:4660:24)
at runMiddlewarePipeline (/home/vlady/code/gyarns/node_modules/.vite/deps_ssr/chunk-QZO7VHZY.js:4600:24)
at Object.query (/home/vlady/code/gyarns/node_modules/.vite/deps_ssr/chunk-QZO7VHZY.js:3696:30)
at handleDocumentRequest (/home/vlady/code/gyarns/node_modules/.vite/deps_ssr/chunk-QZO7VHZY.js:10745:40)
at requestHandler (/home/vlady/code/gyarns/node_modules/.vite/deps_ssr/chunk-QZO7VHZY.js:10671:24)
Why is context.get erroring here? Are we expected to initialize all contexts that can ever be used before context.get calls?
My idea was to do a "self-initialization" pattern, where the middleware creates its own dependencies if needed. I am creating an initial context like this, but I was hoping that I don't have to add stuff to that any time I create a new context type.
// app/worker.tsimport{createRequestHandler}from"react-router"import{createAppContext}from"./app-context"consthandler=createRequestHandler(// @ts-expect-error - virtual module provided by React Router at build time()=>import("virtual:react-router/server-build"),import.meta.env.MODE,)exportdefault{asyncfetch(request,env,ctx){try{constappContext=awaitcreateAppContext(request,env,ctx)// @ts-expect-error unstable types,// import {AppContext} from './app-context' instead of AppLoadContextreturnhandler(request,appContext)}catch(error){console.error(error)returnnewResponse("An unexpected error occurred",{status: 500})}},}satisfiesExportedHandler<Env>
// app-context.tsimport{unstable_createContext}from"react-router"import{typeDatabaseClient,createDbClient}from"./db/db-client.server"import{currencyCookie}from"./internationalization/cookies"importtype{Currency}from"./internationalization/currency-context"import{i18next}from"./internationalization/i18n.server"import{typeServerConfig,getServerConfig}from"./lib/config.server"exporttypeAppContext={env: EnvexecutionContext: ExecutionContextdb: DatabaseClientconfig: ServerConfiglanguage: stringcurrency: Currency}/** * Contains data and bindings needed by most requests. */exportconstappContext=unstable_createContext<AppContext>()// https://github.com/remix-run/react-router/issues/13181// https://github.com/alexanderson1993/cloudflare-middleware/pull/1/filesexportasyncfunctioncreateAppContext(request: Request,env: Env,executionContext: ExecutionContext,){letmap=newMap()letdb=createDbClient(env.DB)letconfig=getServerConfig(env,request)letlanguage=awaiti18next.getLocale(request)letcookie=request.headers.get("Cookie")letcurrency=(awaitcurrencyCookie.parse(cookie))??"EUR"letcontextValue: AppContext={
env,
executionContext,
db,
config,
language,
currency,}map.set(appContext,contextValue)returnmap}
The text was updated successfully, but these errors were encountered:
vladinator1000
changed the title
[Docs]: New middleware API - context initialization (Error: No value found for context)
[Docs]: Middleware context initialization (Error: No value found for context)
Mar 18, 2025
Then when you do the context.get(authServerContext) it will return that initial value that you could use to decide if it's the initial one or not.
Maybe a symbol can work here
constinitialValue=Symbol()exportconstauthServerContext=unstable_createContext<AuthServer>(initialValue)// later in the middlewareconstvalue=context.get(authServerContext)if(value===initialValue)context.set(authServerContext,newValue)returnawaitnext()
Describe what's incorrect/missing in the documentation
Hey folks, I'm trying out the new middleware API, followed these docs for v7.3.0 and got an error when I tried check if a context value exists:
Why is
context.get
erroring here? Are we expected to initialize all contexts that can ever be used beforecontext.get
calls?My idea was to do a "self-initialization" pattern, where the middleware creates its own dependencies if needed. I am creating an initial context like this, but I was hoping that I don't have to add stuff to that any time I create a new context type.
The text was updated successfully, but these errors were encountered: