The CORS middleware simplifies the setup of CORS headers. Internally it uses the same CORS utils exported from remix-utils/cors.
remix-utils/cors
To use it, first create a CORS middleware instance:
import { createCorsMiddleware } from "remix-utils/middleware/cors";export const [corsMiddleware] = createCorsMiddleware(); Copy
import { createCorsMiddleware } from "remix-utils/middleware/cors";export const [corsMiddleware] = createCorsMiddleware();
Add the corsMiddleware to the middleware array in the route where you want to configure CORS, use it in your app/root.tsx file to apply it globally:
corsMiddleware
middleware
app/root.tsx
import { corsMiddleware } from "~/middleware/cors.server";export const middleware: Route.MiddlewareFunction[] = [corsMiddleware]; Copy
import { corsMiddleware } from "~/middleware/cors.server";export const middleware: Route.MiddlewareFunction[] = [corsMiddleware];
Now, every request will have the CORS headers set.
You can customize the CORS middleware by passing an options object to the createCorsMiddleware function.
createCorsMiddleware
The options lets you configure the CORS headers, e.g. origin, methods, allowedHeaders, etc.
origin
methods
allowedHeaders
import { createCorsMiddleware } from "remix-utils/middleware/cors";export const [corsMiddleware] = createCorsMiddleware({ origin: "https://example.com", methods: ["GET", "POST"], allowedHeaders: ["Content-Type", "Authorization"], exposedHeaders: ["X-My-Custom-Header"], maxAge: 3600, credentials: true,}); Copy
import { createCorsMiddleware } from "remix-utils/middleware/cors";export const [corsMiddleware] = createCorsMiddleware({ origin: "https://example.com", methods: ["GET", "POST"], allowedHeaders: ["Content-Type", "Authorization"], exposedHeaders: ["X-My-Custom-Header"], maxAge: 3600, credentials: true,});
The accepted options are the same as those accepted by the cors util.
options
cors
Sergio XalambrÃ
The CORS middleware simplifies the setup of CORS headers. Internally it uses the same CORS utils exported from
remix-utils/cors.To use it, first create a CORS middleware instance:
Add the
corsMiddlewareto themiddlewarearray in the route where you want to configure CORS, use it in yourapp/root.tsxfile to apply it globally:Now, every request will have the CORS headers set.
You can customize the CORS middleware by passing an options object to the
createCorsMiddlewarefunction.The options lets you configure the CORS headers, e.g.
origin,methods,allowedHeaders, etc.The accepted
optionsare the same as those accepted by thecorsutil.Author
Sergio XalambrÃ