Proxy with Astro
View as MarkdownServe the Reveata script and forward events through two Astro endpoints rendered on demand, with the visitor's IP and your proxy secret.
On this page
Two Astro endpoints stand in for Reveata's addresses, so the browser only talks to your domain. They must run on each request, so your project needs a server adapter (Node, Vercel, Netlify or Cloudflare). The rest of the site can stay prerendered.
A fully static site (no adapter) can't forward events from Astro: the files are built once and no code runs per request. Use your host's proxy instead, for example the Vercel, Netlify or Cloudflare setups in Proxy through your own server, or the managed proxy.
Before you start
- In Reveata, open your website, then Settings → General → Tracking code, and choose Create proxy secret under Proxy secret (admins and the owner can). Copy it: it starts with
dt_proxy_and is shown once. - Declare it as a server secret in
astro.config.mjs(Astro 5), so it is read at runtime and never reaches the browser, then setREVEATA_PROXY_SECRETin your host's environment variables:
// astro.config.mjs
import { defineConfig, envField } from 'astro/config';
import node from '@astrojs/node'; // or your host's adapter
export default defineConfig({
adapter: node({ mode: 'standalone' }),
env: {
schema: {
REVEATA_PROXY_SECRET: envField.string({ context: 'server', access: 'secret', optional: true }),
},
},
});The script: src/pages/dt/script.js.ts
import type { APIRoute } from 'astro';
export const prerender = false;
export const GET: APIRoute = async () => {
const r = await fetch('https://reveata.com/js/script.js');
const headers = new Headers({
'content-type': 'text/javascript; charset=utf-8',
'cache-control': r.headers.get('cache-control') ?? 'public, max-age=86400',
});
const etag = r.headers.get('etag');
if (etag) headers.set('etag', etag);
return new Response(await r.arrayBuffer(), { status: r.status, headers });
};Events: src/pages/dt/event.ts
import type { APIRoute } from 'astro';
import { REVEATA_PROXY_SECRET } from 'astro:env/server';
export const prerender = false;
const PASS = [
'user-agent',
'accept-language',
'origin',
'referer',
'sec-fetch-dest',
'sec-fetch-mode',
'sec-fetch-site',
];
export const POST: APIRoute = async ({ request, clientAddress }) => {
const body = await request.text();
if (body.length > 16 * 1024) return new Response(null, { status: 413 });
const headers = new Headers({
'content-type': 'text/plain',
'x-reveata-proxy-secret': REVEATA_PROXY_SECRET ?? '',
'x-reveata-client-ip': clientAddress,
});
for (const name of PASS) {
const value = request.headers.get(name);
if (value) headers.set(name, value);
}
const upstream = await fetch('https://reveata.com/api/collect', { method: 'POST', headers, body });
const out = new Headers({ 'cache-control': 'no-store' });
const diagnostic = upstream.headers.get('x-reveata-proxy');
if (diagnostic) out.set('x-reveata-proxy', diagnostic);
return new Response(null, { status: upstream.status, headers: out });
};The endpoint forwards the body unchanged, with the visitor's browser headers, and never your cookies. Then use the two paths in your layout:
<script
is:inline
defer
data-website-id="ws_your_tracking_id"
data-api="/dt/event"
src="/dt/script.js"
></script>The visitor's address
clientAddress is the visitor's address as the adapter sees it. On Vercel, Netlify and Cloudflare the adapter reads it from the platform. With the Node adapter behind Nginx, a load balancer or a CDN, it can be that proxy's address instead: check it with the test below, and if it is wrong, read the header your proxy sets (for example X-Real-IP set by your own Nginx) instead of clientAddress.
Check it works
Open /dt/script.js on your site: you should see the script. Visit a page, then open Realtime in Reveata: your visit should appear within seconds, in your own city. If it shows your server's location, read the x-reveata-proxy response header of /dt/event in your browser's network tab (the endpoint passes it on), and see Check that it works for what each value means.