Make capturing according to serverless

This commit is contained in:
2025-07-06 20:23:54 +03:00
parent 1e099d88ae
commit 963ac7111f
6 changed files with 31 additions and 19 deletions

View File

@@ -1,23 +1,24 @@
import { PostHog } from "posthog-node";
import { waitUntil } from "@vercel/functions";
export default defineNitroPlugin((nitroApp) => {
const runtimeConfig = useRuntimeConfig();
const posthog = new PostHog(runtimeConfig.public.posthogPublicKey, {
host: runtimeConfig.public.posthogHost,
defaults: runtimeConfig.public.posthogDefaults,
flushAt: 1,
flushInterval: 0,
});
nitroApp.hooks.hook("request", (event) => {
event.context.posthog = posthog;
});
nitroApp.hooks.hook("beforeResponse", async () => {
await posthog.shutdown();
nitroApp.hooks.hook("beforeResponse", () => {
waitUntil(posthog.shutdown());
});
nitroApp.hooks.hook("close", async () => {
await posthog.shutdown();
nitroApp.hooks.hook("close", () => {
waitUntil(posthog.shutdown());
});
});