Back to API Reference
⚡
Setup with Cloudflare Workers
Deploy a Cloudflare Worker in front of your origin. It serves rendered HTML when prerendering applies, and passes through everything else.
How it works (SPAs)
Your edge middleware/worker calls
/api/prerender/render?url=... for HTML document requests. If LovableHTML returns 200 you serve rendered HTML. If it returns 304 with a Location header, prerendering didn’t apply — fall back to your origin SPA.Prerequisites
- A LovableHTML account and API key (Settings → API Keys)
- A domain added + verified in your LovableHTML dashboard
- Cloudflare account with Workers enabled
Setup
Replace
<your-api-key> (or set LOVABLEHTML_API_KEY where the snippet expects it). Don’t commit API keys.- If your domain is not on Cloudflare yet, add it first
- Create a new Worker → choose an 'Hello World' Worker → Deploy → Edit Code
- Paste the snippet below and deploy your Worker
- If you did not paste a key into the snippet, set a Worker secret named
LOVABLEHTML_API_KEY. - Go to Worker Settings → Domains & Routes → add route
yourdomain.com/*
lovablehtml-prerender.js
CopyDownload
// lovablehtml-prerender.js (Cloudflare Worker)export default {async fetch(req, env) {// Only handle public GET navigationsif (req.method !== 'GET') return fetch(req);const isHtmlRequest = (req.headers.get('accept') || '').includes('text/html');if (!isHtmlRequest) return fetch(req);const headers = new Headers();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);}const r = await fetch('https://lovablehtml.com/api/prerender/render?url=' + encodeURIComponent(req.url), { headers });// 304 = not pre-rendered, pass through to originif (r.status === 304) {return fetch(req);}if ((r.headers.get('content-type') || '').includes('text/html')) {return new Response(await r.text(), { headers: { 'content-type': 'text/html; charset=utf-8' } });}return fetch(req);},};
How to Test
- 1Call the render API directly and verify x-lovablehtml-render-cache: hit | miss
- 2Hit your site with Accept: text/html and verify you get HTML back
- 3Verify static assets (JS/CSS/images/fonts) are not proxied to LovableHTML
Render API
# 1) Call the LovableHTML render API directlycurl -sS -D - -o /dev/null \-H "x-lovablehtml-api-key: <API_KEY>" \-H "Accept: text/html" \"https://lovablehtml.com/api/prerender/render?url=https%3A%2F%2Fyour-domain.com%2Fyour-page"# Look for:# - HTTP/1.1 200# - x-lovablehtml-render-cache: hit | miss# - x-lovablehtml-snapshot-key: ...
Your site (HTML request)
# 2) Hit your site with an HTML Accept headercurl -sS -D - -o /dev/null \-H "Accept: text/html" \-A "Googlebot" \"https://your-domain.com/your-page"# Look for:# - HTTP/1.1 200# - content-type: text/html
Your site (passthrough)
# 3) Passthrough (no Accept: text/html → middleware/worker should not call LovableHTML)curl -sS -D - -o /dev/null \-A "Mozilla/5.0" \"https://your-domain.com/your-page"
Best Practices
Keep secrets out of git
Store keys in your platform’s secret manager or env vars; rotate/revoke when compromised.
Don’t proxy static assets
Only call LovableHTML for HTML document requests. Always pass through JS/CSS/images/fonts.
Handle 304 passthrough
304 means prerendering doesn’t apply. Fall back to your origin and use the Location header as the target URL when needed.
Invalidate after content changes
Use the cache invalidation endpoints (optionally with prewarm) after deploys or CMS updates.
Need help? Check the full API reference for prerender, cache, and analytics endpoint docs (including session/API key/public token analytics auth), or jump directly to Analytics API or contact us if you run into issues.