Next.js has a built-in rewrites feature in next.config.js that transparently forwards requests to an external URL while keeping your domain in the browser's address bar. This makes it the cleanest way to proxy the Admaxxer pixel in a Next.js project — no API routes, no middleware files, no extra dependencies.
/pages/api/event.ts proxy, next.config.js rewrites do not consume a serverless function invocation.rewrites() config key is independent of which routing strategy your project uses.next.config.js or next.config.mjs in your project root.Open next.config.js (or next.config.mjs) and add an async rewrites function:
/** @type {import('next').NextConfig} */
const nextConfig = {
async rewrites() {
return [
{
source: '/js/script.js',
destination: 'https://admaxxer.com/js/script.js',
},
{
source: '/api/event/:path*',
destination: 'https://admaxxer.com/api/event/:path*',
},
];
},
};
module.exports = nextConfig;
If you use ES modules (next.config.mjs):
/** @type {import('next').NextConfig} */
const nextConfig = {
async rewrites() {
return [
{
source: '/js/script.js',
destination: 'https://admaxxer.com/js/script.js',
},
{
source: '/api/event/:path*',
destination: 'https://admaxxer.com/api/event/:path*',
},
];
},
};
export default nextConfig;
Rewrites are read at startup. Stop your dev server and restart it:
npm run dev
For production, redeploy. On Vercel a git push triggers a new deployment automatically.
In Admaxxer go to Connections › Install, enable Custom proxy domain, and enter your domain. The updated snippet will load /js/script.js and post to /api/event on your own domain.
# Script loads
curl -I https://yourdomain.com/js/script.js
# Event endpoint accepts POST
curl -X POST https://yourdomain.com/api/event \
-H 'Content-Type: application/json' \
-d '{"name":"test","url":"https://yourdomain.com/test"}'
The script request should return 200 application/javascript. The event POST should return 200 or 204.
If you need to add custom logic (e.g., injecting a workspace header) you can use Next.js Middleware instead:
// middleware.ts
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export function middleware(request: NextRequest) {
const url = request.nextUrl;
if (
url.pathname === '/js/script.js' ||
url.pathname.startsWith('/api/event')
) {
const target = new URL(
url.pathname + url.search,
'https://admaxxer.com'
);
return NextResponse.rewrite(target);
}
}
export const config = {
matcher: ['/js/script.js', '/api/event/:path*'],
};
The next.config.js approach is simpler and preferred unless you need per-request logic.
next.config.js. Hot reload does not re-read the config file./api/event route. Next.js rewrites are applied after file-system routes. If you have pages/api/event.ts or app/api/event/route.ts, that file takes priority. Rename your file-system route or remove it.next start behind NGINX or Apache, the rewrites happen inside Node.js — no additional NGINX config needed for the pixel paths.Remove the two rewrite entries from next.config.js and restart (or redeploy). Disable the custom proxy domain in Admaxxer to revert the snippet.
Cloudflare · Vercel · Nuxt · Netlify · WordPress · Apache · NGINX
See also: Connections › Install for the base snippet.