mirror of
https://github.com/umami-software/umami.git
synced 2025-12-06 01:18:00 +01:00
68 lines
1.4 KiB
TypeScript
68 lines
1.4 KiB
TypeScript
export const dynamic = 'force-dynamic';
|
|
|
|
import { NextResponse } from 'next/server';
|
|
import { POST } from '@/app/api/send/route';
|
|
import type { Pixel } from '@/generated/prisma/client';
|
|
import redis from '@/lib/redis';
|
|
import { notFound } from '@/lib/response';
|
|
import { findPixel } from '@/queries/prisma';
|
|
|
|
const image = Buffer.from('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw', 'base64');
|
|
|
|
export async function GET(request: Request, { params }: { params: Promise<{ slug: string }> }) {
|
|
const { slug } = await params;
|
|
|
|
let pixel: Pixel;
|
|
|
|
if (redis.enabled) {
|
|
pixel = await redis.client.fetch(
|
|
`pixel:${slug}`,
|
|
async () => {
|
|
return findPixel({
|
|
where: {
|
|
slug,
|
|
},
|
|
});
|
|
},
|
|
86400,
|
|
);
|
|
|
|
if (!pixel) {
|
|
return notFound();
|
|
}
|
|
} else {
|
|
pixel = await findPixel({
|
|
where: {
|
|
slug,
|
|
},
|
|
});
|
|
|
|
if (!pixel) {
|
|
return notFound();
|
|
}
|
|
}
|
|
|
|
const payload = {
|
|
type: 'event',
|
|
payload: {
|
|
pixel: pixel.id,
|
|
url: request.url,
|
|
referrer: request.headers.get('referer'),
|
|
},
|
|
};
|
|
|
|
const req = new Request(request.url, {
|
|
method: 'POST',
|
|
headers: request.headers,
|
|
body: JSON.stringify(payload),
|
|
});
|
|
|
|
await POST(req);
|
|
|
|
return new NextResponse(image, {
|
|
headers: {
|
|
'Content-Type': 'image/gif',
|
|
'Content-Length': image.length.toString(),
|
|
},
|
|
});
|
|
}
|