Serve Lucida from your own domain (reverse proxy)
Proxy the browser SDK through a path on your domain with Next.js rewrites, Vercel, Cloudflare Workers or nginx, so ad blockers and strict networks do not break capture.
Ad blockers and strict networks often block third-party analytics hosts. Sending the SDK's calls through a path on your own domain, such as https://example.com/lc, keeps capture working. You change one SDK option and add one rule in front of https://capture.lucidace.com.
What the proxy must do
The SDK calls only /v1/* on the capture host: /v1/capture, /v1/batch, /v1/replay, /v1/flags, /v1/flags/definitions, /v1/flags/stream, /v1/config and /v1/guides. Your rule must:
Strip your prefix:
/lc/v1/capturebecomes/v1/captureoncapture.lucidace.com.Pass the body through untouched, including
Content-Encoding(gzip replay chunks and batches).Not cache anything, and not forward your site's cookies.
Not buffer
/v1/flags/stream(server-sent events). Where the platform buffers (Vercel, Netlify), leave flag streaming off in the SDK; it then polls/v1/flags/definitions.
Visitor IP. The hosted capture host does not take the visitor address from a proxy's X-Forwarded-For on its own, so geo and IP filters may see your proxy's address instead of the visitor's until your proxy proves who it is: send X-Lucida-Proxy-Secret (your proxy record's secret) with the visitor address in X-Lucida-Client-IP. Running your own gateway instead? Trusted-proxy settings are on the self-hosting page.
Script and module assets. With the npm package nothing else needs proxying: modules are bundled. If you load the classic lucida.js loader, it fetches modules from <script origin>/static/v<version>/<name>.js, next to itself. Serve those files from your own origin, or set assetHost (SDK pinning). Do not point assetHost at the /lc API path.
Next.js rewrites
@lucida-analytics/next ships the rule for you. It forwards only /v1/*, from /ingest by default (lucidaRewrites({ path, host }) changes either); then init the browser SDK with apiHost: "/ingest", which is what LucidaNextProvider uses by default. The full Next.js setup is on Install on Next.js.
// next.config.js
const { lucidaRewrites } = require("@lucida-analytics/next");
module.exports = { skipTrailingSlashRedirect: true, async rewrites() { return lucidaRewrites(); } };Or write the rule by hand:
// next.config.js
module.exports = {
async rewrites() {
return [{ source: "/lc/:path*", destination: "https://capture.lucidace.com/:path*" }];
},
skipTrailingSlashRedirect: true,
};To drop cookies and set the visitor address too, use middleware:
// middleware.ts
import { NextResponse, type NextRequest } from "next/server";
export function middleware(req: NextRequest) {
const url = new URL(req.nextUrl.pathname.replace(/^\/lc/, "") + req.nextUrl.search, "https://capture.lucidace.com");
const headers = new Headers(req.headers);
headers.delete("cookie");
return NextResponse.rewrite(url, { request: { headers } });
}
export const config = { matcher: "/lc/:path*" };Next.js route handler (app-level relay)
When you want to set the visitor address yourself, relay from a route handler. Lucida believes a forwarded visitor address only together with X-Lucida-Proxy-Secret: <proxy record secret>. Send the address as X-Lucida-Client-IP: <visitor ip> (and X-Forwarded-For): the hosted edge rewrites X-Forwarded-For to its own hop, so X-Lucida-Client-IP is the one that arrives. Get the secret from the proxy_secret in the answer of POST /v1/projects/{p}/proxy-records (shown once; reset with POST .../proxy-records/{id}/secret) and keep it in server env as LUCIDA_PROXY_SECRET. Without the secret the hosted gateway geolocates the relay, not the visitor.
// app/api/lc/[...path]/route.ts
const ALLOWED = new Set(["v1/batch", "v1/config", "v1/flags"]);
const MAX_BODY = 5 * 1024 * 1024;
async function relay(req: Request, { params }: { params: Promise<{ path: string[] }> }) {
const path = (await params).path.join("/");
if (!ALLOWED.has(path)) return new Response("not found", { status: 404 });
const body = req.method === "POST" ? await req.arrayBuffer() : undefined;
if (body && body.byteLength > MAX_BODY) return new Response("too large", { status: 413 });
const headers = new Headers({
"content-type": req.headers.get("content-type") ?? "application/json",
"x-forwarded-for": req.headers.get("x-forwarded-for")?.split(",")[0].trim() ?? "",
"x-lucida-client-ip": req.headers.get("x-forwarded-for")?.split(",")[0].trim() ?? "",
"x-lucida-proxy-secret": process.env.LUCIDA_PROXY_SECRET ?? "",
});
const enc = req.headers.get("content-encoding");
if (enc) headers.set("content-encoding", enc); // no cookies are forwarded
const url = new URL(`https://capture.lucidace.com/${path}`);
url.search = new URL(req.url).search;
return fetch(url, { method: req.method, headers, body, signal: AbortSignal.timeout(10_000) });
}
export { relay as GET, relay as POST };Init the SDK with apiHost: "/api/lc". If next.config also has a rewrite such as /api/:path* or lucidaRewrites(), exclude /api/lc from it, or the rewrite wins over the handler. Replay chunks (/v1/replay) are not relayed here; use a rewrite or the managed domain for those.
Vercel (vercel.json)
{
"rewrites": [
{ "source": "/lc/:path*", "destination": "https://capture.lucidace.com/:path*" }
]
}Vercel buffers rewritten responses, so leave flag streaming off.
Cloudflare Workers
Route example.com/lc/* to a Worker:
export default {
async fetch(request) {
const url = new URL(request.url);
url.hostname = "capture.lucidace.com";
url.protocol = "https:";
url.pathname = url.pathname.replace(/^\/lc/, "");
const headers = new Headers(request.headers);
headers.set("X-Forwarded-For", request.headers.get("CF-Connecting-IP") ?? "");
headers.delete("cookie");
return fetch(new Request(url, new Request(request, { headers })));
},
};nginx
location /lc/ {
proxy_pass https://capture.lucidace.com/; # trailing slash strips /lc/
proxy_http_version 1.1;
proxy_set_header Host capture.lucidace.com;
proxy_ssl_server_name on;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Cookie "";
proxy_set_header Connection "";
proxy_buffering off; # /v1/flags/stream is SSE
proxy_request_buffering off;
client_max_body_size 20m; # replay chunks up to 10 MB, batches up to 20 MB
proxy_read_timeout 1h;
}Check it
Open the browser network tab: requests go to /lc/v1/batch (or /lc/v1/capture) on your domain and answer 2xx. Or run curl -i -X POST https://example.com/lc/v1/batch with a valid body as in Send events over HTTP.