How to Set CORs Origins for Cloudflare Pages

7 replies
Last updated: May 3, 2022
Hey y’all - I’m wondering if there is a way to set CORs origins for cloudflare pages deploy previews with a URL pattern like this
https://{hash}.domain.pages.dev
where the hash is unique for each PR. Here’s an example from the project we’re working on:
https://444238b5.stylitics-demo-beta.pages.dev
AI Update

Good news! Sanity supports wildcard patterns in CORS origins, which is perfect for handling Cloudflare Pages deploy previews with dynamic hash subdomains.

In your CORS origins configuration (found in manage.sanity.io under Settings > API > CORS Origins), you can add a pattern like:

https://*.stylitics-demo-beta.pages.dev

This wildcard pattern will match all deploy preview URLs like https://444238b5.stylitics-demo-beta.pages.dev, https://abc123ef.stylitics-demo-beta.pages.dev, etc.

Important considerations:

  1. Credentials and wildcards: If you need to use authentication tokens with your Sanity client (which requires the "Allow credentials" option), be aware that browsers have security restrictions around wildcards with credentials. The CORS spec doesn't allow Access-Control-Allow-Origin: * when credentials are involved, but subdomain wildcards like *.yourdomain.pages.dev do work with credentials enabled.

  2. Add both production and preview patterns: You'll likely want to add multiple origins:

    • https://*.stylitics-demo-beta.pages.dev (for all deploy previews)
    • Your production domain (e.g., https://stylitics-demo-beta.pages.dev)
    • http://localhost:3000 (for local development)
  3. Protocol matters: Make sure to include https:// in your pattern - the protocol must match exactly.

  4. Asset CDN limitation: Note that CORS settings configured in the Management Dashboard apply to the Data API but not automatically to the Asset CDN. If you're fetching assets directly from the browser, you may need additional configuration.

To add these, just go to your project in the Sanity Management Dashboard, navigate to Settings > API > CORS Origins, and add the wildcard pattern. Enable "Allow credentials" if you're using authentication tokens in your Sanity client.

If I’m understanding right you should be able to do it like this:
https://*.<http://domain.pages.dev|domain.pages.dev>
I’ve tried that and get this err
That’s really weird, just tested and it worked for me
Very weird indeed!
It’s a browser thing (this will work in Chrome but not Firefox). Jacob: Please try in Chrome or using the CLI (
sanity cors add https://*.<http://stylitics-demo-beta.pages.dev|stylitics-demo-beta.pages.dev> --no-credentials
). Apologies for the inconvenience.
Oh damn! Maybe this will be the nail in the coffin for me leaving FF haha
Worked like a charm - thanks
user A
!

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?