Skip to content

Recipes ​

Allow One Origin ​

An array with one origin reflects that origin and sends no Access-Control-Allow-Origin to any other. A plain string would be sent to every origin, leaving the rejection to the browser (origin).

ts
import { App, defineCoreHandler } from 'routup';
import { cors } from '@routup/cors';

export const app = new App();

// an array of one: other origins get no Access-Control-Allow-Origin
app.use(cors({ origin: ['https://app.example.com'] }));

app.get('/todos', defineCoreHandler(() => [{ id: 1, title: 'Write docs' }]));
bash
curl -i -H 'origin: https://app.example.com' http://localhost:3000/todos
# access-control-allow-origin: https://app.example.com
# access-control-expose-headers: *
# vary: origin
curl -i -H 'origin: https://other.example' http://localhost:3000/todos
# access-control-expose-headers: *
# vary: origin

An Allow-List ​

A function decides per request; it receives the request's Origin and returns true to allow it. Here a fixed list plus every numbered preview deployment.

ts
import { App, defineCoreHandler } from 'routup';
import { cors } from '@routup/cors';

const ORIGINS = new Set([
    'https://app.example.com',
    'https://admin.example.com',
]);

export const app = new App();

app.use(cors({
    // the listed origins and every preview deployment
    origin: (origin) => ORIGINS.has(origin) ||
        /^https:\/\/pr-\d+\.preview\.example\.com$/.test(origin),
}));

app.get('/todos', defineCoreHandler(() => [{ id: 1, title: 'Write docs' }]));
bash
curl -i -H 'origin: https://pr-42.preview.example.com' http://localhost:3000/todos
# access-control-allow-origin: https://pr-42.preview.example.com
# access-control-expose-headers: *
# vary: origin

Anchor a pattern at both ends: without the $, https://pr-42.preview.example.com.evil.example would match.

Credentials ​

For cookies or an Authorization header sent by the browser, credentials: true needs a concrete origin and a list of exposed headers (credentials).

ts
import { App, defineCoreHandler } from 'routup';
import { cors } from '@routup/cors';

export const app = new App();

app.use(cors({
    // a listed origin is reflected; browsers reject '*' on a credentialed request
    origin: ['https://app.example.com'],
    credentials: true,
    // list the headers the script may read; '*' does not apply with credentials
    exposeHeaders: ['x-request-id'],
}));

app.get('/me', defineCoreHandler((event) => {
    event.response.headers.set('x-request-id', crypto.randomUUID());

    return { name: 'Ada' };
}));

The browser script calls fetch(url, { credentials: 'include' }). The preflight lists the methods, since '*' is expanded under credentials:

bash
curl -i -X OPTIONS http://localhost:3000/me \
    -H 'origin: https://app.example.com' \
    -H 'access-control-request-method: GET' \
    -H 'access-control-request-headers: authorization'
# HTTP/1.1 204 No Content
# access-control-allow-credentials: true
# access-control-allow-headers: authorization
# access-control-allow-methods: GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS
# access-control-allow-origin: https://app.example.com
# content-length: 0
# vary: origin, access-control-request-headers

Preflight Caching ​

A request with a JSON body or an Authorization header is preceded by a preflight. maxAge lets the browser reuse the answer, so it sends one preflight per ten minutes instead of one per request.

ts
import { App, defineCoreHandler } from 'routup';
import { cors } from '@routup/cors';

export const app = new App();

app.use(cors({
    origin: ['https://app.example.com'],
    methods: ['GET', 'POST', 'DELETE'],
    allowHeaders: ['content-type', 'authorization'],
    // the browser reuses the preflight answer for 10 minutes
    maxAge: 600,
}));

app.post('/todos', defineCoreHandler(() => ({ id: 2 })));
bash
curl -i -X OPTIONS http://localhost:3000/todos \
    -H 'origin: https://app.example.com' \
    -H 'access-control-request-method: POST' \
    -H 'access-control-request-headers: content-type'
# HTTP/1.1 204 No Content
# access-control-allow-headers: content-type,authorization
# access-control-allow-methods: GET,POST,DELETE
# access-control-allow-origin: https://app.example.com
# access-control-max-age: 600
# content-length: 0
# vary: origin, access-control-request-headers

Browsers cap the value: Chromium at two hours, Firefox at one day (MDN).

Your Own Preflight Handler ​

With preflight.continue, the plugin sets the preflight headers and passes the request on, so an OPTIONS route decides the response.

ts
import { App, defineCoreHandler } from 'routup';
import { cors } from '@routup/cors';

export const app = new App();

app.use(cors({ preflight: { continue: true } }));

app.options('/upload', defineCoreHandler((event) => {
    event.response.status = 204;
    event.response.headers.set('x-upload-limit', '10mb');

    return null;
}));
bash
curl -i -X OPTIONS http://localhost:3000/upload \
    -H 'origin: https://app.example.com' \
    -H 'access-control-request-method: PUT'
# HTTP/1.1 204 No Content
# access-control-allow-methods: *
# access-control-allow-origin: *
# x-upload-limit: 10mb