And because cors mutates the response, you can also call it and later return.
import { cors } from"remix-utils/cors";
exportasyncfunctionloader({ request }: Route.LoaderArgs) { letdata = awaitgetData(request); letresponse = json<LoaderData>(data); awaitcors(request, response); // this mutates the Response object returnresponse; // so you can return it here }
If you want to setup it globally once, you can do it like this in entry.server
Additionally, the cors function accepts a options object as a third optional argument. These are the options.
origin: Configures the Access-Control-Allow-Origin CORS header.
Possible values are:
true: Enable CORS for any origin (same as "*")
false: Don't setup CORS
string: Set to a specific origin, if set to "*" it will allow any origin
RegExp: Set to a RegExp to match against the origin
Array<string | RegExp>: Set to an array of origins to match against the
string or RegExp
Function: Set to a function that will be called with the request origin
and should return a boolean indicating if the origin is allowed or not.
The default value is true.
methods: Configures the Access-Control-Allow-Methods CORS header.
The default value is ["GET", "HEAD", "PUT", "PATCH", "POST", "DELETE"].
allowedHeaders: Configures the Access-Control-Allow-Headers CORS header.
exposedHeaders: Configures the Access-Control-Expose-Headers CORS header.
credentials: Configures the Access-Control-Allow-Credentials CORS header.
maxAge: Configures the Access-Control-Max-Age CORS header.
Install using
bunx shadcn@latest add @remix-utils/cors.The CORS function let you implement CORS headers on your loaders and actions so you can use them as an API for other client-side applications.
There are two main ways to use the
corsfunction.If you want to use it on every loader/action, you can do it like this:
You could also do the
jsonandcorscall in one line.And because
corsmutates the response, you can also call it and later return.If you want to setup it globally once, you can do it like this in
entry.serverAdditionally, the
corsfunction accepts aoptionsobject as a third optional argument. These are the options.origin: Configures the Access-Control-Allow-Origin CORS header. Possible values are:true: Enable CORS for any origin (same as "*")false: Don't setup CORSstring: Set to a specific origin, if set to "*" it will allow any originRegExp: Set to a RegExp to match against the originArray<string | RegExp>: Set to an array of origins to match against the string or RegExpFunction: Set to a function that will be called with the request origin and should return a boolean indicating if the origin is allowed or not. The default value istrue.methods: Configures the Access-Control-Allow-Methods CORS header. The default value is["GET", "HEAD", "PUT", "PATCH", "POST", "DELETE"].allowedHeaders: Configures the Access-Control-Allow-Headers CORS header.exposedHeaders: Configures the Access-Control-Expose-Headers CORS header.credentials: Configures the Access-Control-Allow-Credentials CORS header.maxAge: Configures the Access-Control-Max-Age CORS header.Author
Sergio XalambrÃ