The singleton middleware let's you create a singleton object that will be shared between loaders of a single requests.
This is specially useful to share objects that needs to be created only once per request, like a cache, but not shared between requests.
import { unstable_createSingletonMiddleware } from "remix-utils/middleware/singleton";export const [singletonMiddleware, getSingleton] = unstable_createSingletonMiddleware({ instantiator: () => new MySingletonClass(), }); Copy
import { unstable_createSingletonMiddleware } from "remix-utils/middleware/singleton";export const [singletonMiddleware, getSingleton] = unstable_createSingletonMiddleware({ instantiator: () => new MySingletonClass(), });
To use it, you need to add it to the unstable_middleware array in the route where you want to use it.
unstable_middleware
import { singletonMiddleware } from "~/middleware/singleton.server";export const unstable_middleware = [singletonMiddleware]; Copy
import { singletonMiddleware } from "~/middleware/singleton.server";export const unstable_middleware = [singletonMiddleware];
And you can use the getSingleton function in your loaders to get the singleton object.
getSingleton
import { getSingleton } from "~/middleware/singleton.server";export async function loader({ context }: LoaderFunctionArgs) { let singleton = getSingleton(context); let result = await singleton.method(); // ...} Copy
import { getSingleton } from "~/middleware/singleton.server";export async function loader({ context }: LoaderFunctionArgs) { let singleton = getSingleton(context); let result = await singleton.method(); // ...}
The singleton middleware can be created with different classes and arguments, so you can have multiple singletons in the same request.
import { unstable_createSingletonMiddleware } from "remix-utils/middleware/singleton";export const [singletonMiddleware, getSingleton] = unstable_createSingletonMiddleware({ instantiator: () => new MySingletonClass("arg1", "arg2"), });export const [anotherSingletonMiddleware, getAnotherSingleton] = unstable_createSingletonMiddleware({ instantiator: () => new AnotherSingletonClass("arg1", "arg2"), }); Copy
import { unstable_createSingletonMiddleware } from "remix-utils/middleware/singleton";export const [singletonMiddleware, getSingleton] = unstable_createSingletonMiddleware({ instantiator: () => new MySingletonClass("arg1", "arg2"), });export const [anotherSingletonMiddleware, getAnotherSingleton] = unstable_createSingletonMiddleware({ instantiator: () => new AnotherSingletonClass("arg1", "arg2"), });
And use it in a route like this.
import { singletonMiddleware, anotherSingletonMiddleware,} from "~/middleware/singleton.server";export const unstable_middleware = [ singletonMiddleware, anotherSingletonMiddleware,]; Copy
import { singletonMiddleware, anotherSingletonMiddleware,} from "~/middleware/singleton.server";export const unstable_middleware = [ singletonMiddleware, anotherSingletonMiddleware,];
You can also access the request and context objects in the instantiator function, so you can create the singleton based on the request or context.
request
context
instantiator
import { unstable_createSingletonMiddleware } from "remix-utils/middleware/singleton";import { MySingletonClass } from "~/singleton";export const [singletonMiddleware, getSingleton] = unstable_createSingletonMiddleware({ instantiator: (request, context) => { return new MySingletonClass(request, context); }, }); Copy
import { unstable_createSingletonMiddleware } from "remix-utils/middleware/singleton";import { MySingletonClass } from "~/singleton";export const [singletonMiddleware, getSingleton] = unstable_createSingletonMiddleware({ instantiator: (request, context) => { return new MySingletonClass(request, context); }, });
This can allows you to create a class that depends on the request, maybe to read the URL or body, or depends on the context, maybe to read the session or some other data.
Sergio XalambrÃ
The singleton middleware let's you create a singleton object that will be shared between loaders of a single requests.
This is specially useful to share objects that needs to be created only once per request, like a cache, but not shared between requests.
To use it, you need to add it to the
unstable_middleware
array in the route where you want to use it.And you can use the
getSingleton
function in your loaders to get the singleton object.The singleton middleware can be created with different classes and arguments, so you can have multiple singletons in the same request.
And use it in a route like this.
You can also access the
request
andcontext
objects in theinstantiator
function, so you can create the singleton based on the request or context.This can allows you to create a class that depends on the request, maybe to read the URL or body, or depends on the context, maybe to read the session or some other data.
Author
Sergio XalambrÃ