Strapi preview sounds simple until the browser gets involved.
You have Strapi running on one origin, a frontend preview app on another, and an editor clicking “Preview” expecting instant results. Then the browser blocks your request because of CORS, cookies don’t stick, or your preview token leaks across environments.
I’ve seen teams lose hours here because they treat CORS as a checkbox instead of a design choice. For Strapi preview, your CORS setup affects security, editor experience, and how much pain you’ll have when moving from local dev to staging and production.
Here’s the practical comparison guide.
What “Strapi preview” usually means
A typical preview flow looks like this:
- Strapi admin lives at something like
https://cms.example.com - Frontend preview lives at
https://preview.example.com - Editors save draft content in Strapi
- Frontend fetches draft content from Strapi using a preview mode, token, or authenticated session
- Browser enforces CORS because frontend and CMS are on different origins
That last part is where things break.
If your frontend JavaScript calls Strapi directly from the browser, CORS is in play. If your preview request goes through your own backend first, CORS often becomes much simpler.
Quick refresher: what the browser actually checks
For a cross-origin fetch, the browser looks for response headers like:
Access-Control-Allow-Origin: https://preview.example.com
Access-Control-Allow-Credentials: true
Access-Control-Expose-Headers: X-Preview-Status, ETag
Real APIs do this differently depending on their needs. For example, api.github.com sends:
access-control-allow-origin: *
access-control-expose-headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset, Warning
That works for a public API pattern. Strapi preview usually isn’t public. Draft content should be treated as sensitive, so copying the “just use *” approach is usually a bad idea.
The 4 common CORS patterns for Strapi preview
1. Access-Control-Allow-Origin: *
This is the quick-and-dirty setup.
Example
In Strapi, you might configure middleware like this:
// ./config/middlewares.js
module.exports = [
// ...
{
name: 'strapi::cors',
config: {
origin: '*',
headers: ['Content-Type', 'Authorization', 'Origin', 'Accept'],
methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'],
},
},
];
Pros
- Fastest setup
- Good for fully public read-only APIs
- Easy for local development
- Fewer “why is my browser blocking this?” moments
Cons
- Bad fit for authenticated preview
- Doesn’t work with credentials in the way teams expect
- Makes it easier to expose draft endpoints to any site
- Encourages sloppy security boundaries
My take
I only like * when the content is genuinely public and there is no cookie-based auth or sensitive preview state. For draft preview, I would avoid it.
If your preview needs credentials, wildcard origins are not the solution.
2. Explicit allowlist of preview origins
This is the most common sane option.
You define exactly which origins may call Strapi from the browser.
Example
// ./config/middlewares.js
module.exports = [
// ...
{
name: 'strapi::cors',
config: {
origin: [
'http://localhost:3000',
'https://preview.example.com',
'https://staging-preview.example.com',
],
credentials: true,
headers: ['Content-Type', 'Authorization', 'Origin', 'Accept'],
methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'],
keepHeaderOnError: true,
},
},
];
Pros
- Strong default for real preview environments
- Works with authenticated browser requests
- Limits exposure to known frontend apps
- Easy to reason about during incident response
Cons
- You must maintain the allowlist
- Teams forget staging, branch deploys, or localhost ports
- Can get messy with preview deployments on random subdomains
My take
This is the best default for most Strapi preview setups. If you know the frontend origins, allow only those origins. Boring is good here.
The main drawback is operational overhead. If your hosting platform creates preview deployments like https://feature-123.preview.example.app, you either need automation or a different architecture.
3. Dynamic origin reflection
This pattern checks the incoming Origin header and reflects it back only if it matches your rules.
Example
Depending on your deployment, you may implement logic around allowed origins via environment variables or middleware customization:
const allowedOrigins = [
'http://localhost:3000',
'https://preview.example.com',
];
function isAllowedOrigin(origin) {
if (!origin) return false;
if (allowedOrigins.includes(origin)) return true;
return /^https:\/\/[a-z0-9-]+\.preview\.example\.app$/.test(origin);
}
module.exports = [
{
name: 'strapi::cors',
config: {
origin: (ctx) => {
const requestOrigin = ctx.request.header.origin;
return isAllowedOrigin(requestOrigin) ? requestOrigin : false;
},
credentials: true,
},
},
];
Pros
- Flexible for branch previews and ephemeral environments
- Safer than wildcard if implemented carefully
- Good compromise for modern frontend hosting setups
Cons
- Easy to get wrong with loose regex rules
- One bad pattern can allow attacker-controlled origins
- Harder to audit than a fixed list
My take
Dynamic reflection is useful, but I don’t trust it unless the matching rules are painfully strict. I’ve seen regex patterns that accidentally allow preview.example.com.attacker.com, which is exactly the kind of bug that survives code review because it “looks right.”
If you go this route, test the origin matching like production code, not config fluff.
4. Skip browser-to-Strapi calls and proxy preview through your frontend backend
This is the cleanest architecture in many cases.
Instead of the browser calling Strapi directly, your preview frontend calls its own backend endpoint. That backend fetches draft content from Strapi server-to-server. Browser CORS either disappears or becomes trivial.
Example with a Next.js-style API route
// app/api/preview-post/route.js
export async function GET(request) {
const slug = new URL(request.url).searchParams.get('slug');
const strapiRes = await fetch(
`${process.env.STRAPI_URL}/api/posts?filters[slug][$eq]=${encodeURIComponent(slug)}&publicationState=preview`,
{
headers: {
Authorization: `Bearer ${process.env.STRAPI_PREVIEW_TOKEN}`,
},
cache: 'no-store',
}
);
const data = await strapiRes.json();
return Response.json(data);
}
Pros
- Best security boundary for draft content
- No need to expose Strapi preview tokens to the browser
- Simpler CORS story
- Easier to add auth checks, rate limits, and logging
Cons
- More backend code
- Slightly more moving parts
- Can feel slower to prototype
My take
If the preview contains sensitive draft content, this is my favorite pattern. You stop fighting browser policy and move trust decisions to the server, where they belong.
A lot of “CORS problems” are really architecture problems.
Which option should you choose?
Here’s the blunt version:
Use wildcard * if:
- The content is public
- No credentials are involved
- You do not care which sites can read the API from a browser
That usually does not describe Strapi preview.
Use a fixed allowlist if:
- You have a stable set of preview origins
- Editors access known environments
- You want the simplest secure option
This is the best default for most teams.
Use dynamic origin reflection if:
- You have ephemeral preview deployments
- You can strictly validate origin patterns
- You’re willing to test and monitor the config carefully
Useful, but easier to mess up.
Use a backend proxy if:
- Preview content is sensitive
- You need stronger auth and auditing
- You want less browser-side complexity
This is the architecture I’d pick for most serious teams.
Credentials change the game
If your Strapi preview relies on cookies or authenticated sessions, remember this: credentialed cross-origin requests are stricter.
Frontend request:
fetch('https://cms.example.com/api/posts?publicationState=preview', {
credentials: 'include',
});
Server must respond with a specific origin, not *:
Access-Control-Allow-Origin: https://preview.example.com
Access-Control-Allow-Credentials: true
Vary: Origin
That Vary: Origin matters for caches and CDNs. Without it, you can get weird behavior where one origin’s CORS response is reused for another.
Don’t forget exposed headers
If your frontend preview wants access to non-simple response headers, expose them.
For example, if you want the browser to read ETag for draft caching or a custom preview status header:
Access-Control-Expose-Headers: ETag, X-Preview-Status
That’s exactly why public APIs like GitHub expose a long list of headers. The browser won’t let JavaScript read them otherwise.
Security pitfalls I keep seeing
1. Allowing every origin in non-production “just for testing”
Temporary CORS settings have a way of becoming permanent.
2. Exposing draft content with a public token
If the browser can see the token, assume users can too.
3. Using weak regex for subdomain matching
Be strict. Match full origins, including scheme.
4. Forgetting preflight requests
If you send Authorization headers or use non-simple methods, the browser sends an OPTIONS preflight first. Your server has to handle it correctly.
5. Treating CORS as auth
CORS is not authentication. It’s a browser enforcement layer. Non-browser clients can still hit your endpoints directly.
My recommended setup
For most Strapi preview implementations:
- Proxy preview through your frontend/backend if possible
- If the browser must call Strapi directly, use an explicit allowlist
- Enable credentials only when you actually need them
- Expose only the headers your frontend reads
- Keep preview tokens off the client
If you’re tightening the rest of your response headers too, CORS is only one piece. For broader header hardening, CSP becomes relevant, and https://csp-guide.com is a useful reference.
For Strapi-specific behavior, check the official docs:
CORS for Strapi preview isn’t hard once you stop trying to make one config fit every environment. Pick the model that matches your trust boundary, not the one that makes the error disappear fastest.