Pre-rendering for Shopify through Cloudflare
How Shopify pre-rendering works#
Shopify serves your store from its own edge, so you cannot point your domain anywhere else without breaking checkout. Instead, your domain stays on Cloudflare pointing at Shopify, and a Worker route intercepts crawler requests before they reach the store.
Crawler GET requests for HTML receive the rendered snapshot from Encited. Browser visits, assets, and any failed render calls continue through Cloudflare to Shopify unchanged.
Prerequisites#
- An Encited account and API key (Settings → API Keys).
- Your store's custom domain with DNS managed in a Cloudflare account.
- The domain connected to Shopify and set as the primary domain (Settings → Domains).
Set up pre-rendering for Shopify#
Shopify keeps serving your store. Only the DNS record on the apex changes, and nothing changes in Shopify admin.
1. Point your DNS at Shopify with a proxied CNAME#
In your Cloudflare zone, every hostname your store runs on (the apex and www) must be a CNAME to shops.myshopify.com, set to Proxied (orange cloud). Cloudflare then routes traffic through your zone first, so your Worker runs before Shopify serves the page.
If your apex has Shopify's A record
Shopify asks for an A record on the apex (23.227.38.65). Cloudflare sends an A record's traffic to Shopify before your Worker runs, even when the record is proxied. Replace it with a CNAME:
- Note the current A record, so you can restore it.
- Delete the A record on the apex.
- Add a CNAME with name
@, targetshops.myshopify.com, and proxy status Proxied.
Cloudflare allows a CNAME on the apex on every plan. Your store stays on the apex, and nothing changes in Shopify. The apex can be unreachable for a few seconds between steps 2 and 3. If the apex shows a Cloudflare error page after the change, restore the A record and contact Shopify support.
2. Create a new Worker#
Open Cloudflare dashboard → choose a "Hello World" Worker → Deploy → Edit Code
3. Paste the snippet below into the Worker editor#
encited
// encited.js (Cloudflare Worker)
export default {
async fetch(req, env) {
// Only handle public GET navigations
if (req.method !== 'GET') return fetch(req);
// Treat missing/empty Accept and bare '*/*' as HTML so crawler tests
// (curl without -H, default fetch) still route through prerender.
// Asset requests from browsers send specific Accept (e.g. 'text/css,*/*;q=0.1')
// so they won't match.
const accept = (req.headers.get('accept') || '').trim();
const isHtmlRequest = !accept || accept === '*/*' || accept.includes('text/html');
if (!isHtmlRequest) return fetch(req);
const headers = new Headers();
// Add LOVABLEHTML_API_KEY as a secret: Worker -> Settings -> Variables and Secrets.
headers.set('x-lovablehtml-api-key', env.LOVABLEHTML_API_KEY);
headers.set('accept', 'text/html');
const forward = [
'accept-language',
'sec-fetch-mode',
'sec-fetch-site',
'sec-fetch-dest',
'sec-fetch-user',
'upgrade-insecure-requests',
'referer',
'user-agent',
];
for (const name of forward) {
const v = req.headers.get(name);
if (v) headers.set(name, v);
}
// Bot requests only: Encited checks the IP against the crawler's published
// ranges. Browsers send Sec-Fetch-Mode, so visitor IPs are never sent.
const clientIp = req.headers.get('cf-connecting-ip');
if (!req.headers.get('sec-fetch-mode') && clientIp) {
headers.set('x-encited-client-ip', clientIp);
}
try {
const r = await fetch(
'https://encited.com/api/prerender/render?url=' + encodeURIComponent(req.url),
{ headers, redirect: 'manual' },
);
// 301 = configured redirect rule matched - forward to client
if (r.status === 301) {
const loc = r.headers.get('location');
if (loc) {
return new Response(null, {
status: 301,
headers: { location: loc, 'cache-control': 'no-store' },
});
}
}
// 304 = not pre-rendered, pass through to origin
if (r.status === 304) {
return fetch(req);
}
if (r.status === 200 && (r.headers.get('content-type') || '').includes('text/html')) {
const responseHeaders = new Headers(r.headers);
for (const name of ['content-encoding', 'content-length', 'transfer-encoding', 'connection', 'keep-alive']) {
responseHeaders.delete(name);
}
responseHeaders.set('content-type', 'text/html; charset=utf-8');
return new Response(r.body, { status: 200, headers: responseHeaders });
}
} catch {
// Prerender unreachable → fall through so visitors still get the site
}
return fetch(req);
},
};
4. Add a route to your Worker#
Go to your Worker → Settings → Domains & Routes → Add Route → enter *yourdomain.com/*. It covers the apex and www.
5. Deploy the Worker#
It can take a couple of minutes to start working.
Common mistakes
- DNS record on the gray cloud. Routes only fire on proxied traffic. In DNS → Records, the CNAME record for each store hostname must show the orange cloud (Proxied), not gray (DNS only).
- An A record on the apex. Proxied or not, an A record sends apex traffic to Shopify before your Worker runs. Use the apex CNAME from step 1.
- Always Use HTTPS turned on. It can block the
/.well-known/acme-challenge/*path that Shopify needs to renew your certificate. Leave it off and use a redirect rule that skips that path. - Failure mode left on the default. Set the route's Failure mode to Fail open (proceed) so a Worker error sends requests straight to your origin instead of failing.
- Bot Fight Mode blocking crawlers. Bot Fight Mode and AI crawler blocking (Security → Bots) run before your Worker, so a blocked crawler never reaches it. Allow the crawlers you want pre-rendered.
Test the integration#
1. Send a crawler request
curl -sS -D - -o /dev/null \
-A "Googlebot" \
-H "Accept: text/html" \
"https://your-domain.com/"
Look for a successful HTML response and the x-lovablehtml-render-cache header.
2. Confirm browser passthrough
curl -sS -D - -o /dev/null \
-A "Mozilla/5.0" \
-H "Accept: text/html" \
-H "Accept-Language: en-US" \
-H "Sec-Fetch-Mode: navigate" \
-H "Sec-Fetch-Dest: document" \
-H "Upgrade-Insecure-Requests: 1" \
"https://your-domain.com/"
This request should come back from Shopify without an Encited render-cache header.
Common errors#
The Worker never runs on store traffic
The DNS records for your store must be CNAMEs to shops.myshopify.com, set to Proxied (orange cloud). A gray-cloud record or an A record sends traffic to Shopify before your Worker runs.
The header appears on www but not on the apex
The apex is still an A record. Replace it with a proxied CNAME to shops.myshopify.com, as in step 1.
Your domain redirects to the myshopify.com address
In Shopify admin open Settings → Domains and make sure your custom domain is set as the primary domain. Shopify redirects non-primary domains to the primary one.
Crawlers are blocked before they reach the Worker
Bot Fight Mode and AI crawler blocking (Security → Bots) run before Worker routes. Allow the crawlers you want pre-rendered.
Crawler requests return errors instead of rendered HTML
Confirm the Worker has a secret named LOVABLEHTML_API_KEY with a valid API key from your Encited dashboard, then redeploy the Worker.
Request and security behavior#
- Only crawler GET requests for public HTML pages are answered with pre-rendered snapshots.
- Along with the public page URL, only the API key and crawler-classification headers are sent to Encited.
- For bot requests (no Sec-Fetch-Mode header), the visitor IP is also sent, so Encited can check the crawler against its published IP ranges. Browser visitors' IPs are never sent.
- Cookie and Authorization headers are never forwarded to Encited.
- If Encited is unavailable or does not return rendered HTML, the request continues to Shopify unchanged.
If your zone runs bot protection or rate limiting, those rules block us before the Worker route helps. Set a render token and add the Cloudflare rule so renders and audit crawls get through.
Common questions#
Does this touch checkout or the Shopify admin?#
No. Only crawler requests for HTML pages are answered with pre-rendered snapshots. Checkout, admin, apps, and all browser traffic continue to Shopify unchanged.
Can I use the no-code DNS setup with Shopify instead?#
Only with extra setup inside Shopify. The myshopify.com address permanently redirects to your primary domain, so it can't serve as the pre-render origin. You'd first need to connect a spare subdomain in Shopify (Settings → Domains) and change its type to Alias so it serves the store without redirecting, then use that subdomain as the project URL. The Worker route on this page needs no changes in Shopify admin, which is why we recommend it.
Do I need to change my Shopify DNS records?#
Only the apex. Replace Shopify's apex A record with a proxied CNAME to shops.myshopify.com (step 1). Keep the www CNAME, set both to Proxied, and attach the Worker route. Nothing changes in Shopify admin.
