feat: improve nextjs proxying mode

This commit is contained in:
Carl-Gerhard Lindesvärd
2025-11-25 11:43:54 +01:00
parent 86903b1937
commit 6da8267509
9 changed files with 184 additions and 94 deletions

View File

@@ -1,7 +1,5 @@
import {
createNextRouteHandler,
createScriptHandler,
} from '@openpanel/nextjs/server';
import { createRouteHandler } from '@openpanel/nextjs/server';
export const POST = createNextRouteHandler();
export const GET = createScriptHandler();
const routeHandler = createRouteHandler();
export const GET = routeHandler;
export const POST = routeHandler;

View File

@@ -0,0 +1,23 @@
'use client';
import { Button } from '@/components/ui/button';
import { useOpenPanel } from '@openpanel/nextjs';
export default function TestPage() {
const op = useOpenPanel();
return (
<div>
<h1>Test Page</h1>
<Button
onClick={async () => {
const deviceId = await op.fetchDeviceId();
alert(`Device ID: ${deviceId}`);
}}
>
Fetch device id
</Button>
<Button onClick={() => op.track('hello')}>Hello</Button>
<Button onClick={() => op.revenue(100)}>Revenue</Button>
</div>
);
}

View File

@@ -273,13 +273,16 @@ export function GET() {
### Proxy events
With `createNextRouteHandler` you can proxy your events through your server, this will ensure all events are tracked since there is a lot of adblockers that block requests to third party domains. You'll also need to either host our tracking script or you can use `createScriptHandler` function which proxies this as well.
With `createRouteHandler` you can proxy your events through your server, this will ensure all events are tracked since there is a lot of adblockers that block requests to third party domains. The handler automatically routes requests based on the path, supporting both API endpoints (like `/track` and `/track/device-id`) and the tracking script (`/op1.js`).
```typescript title="/app/api/[...op]/route.ts"
import { createNextRouteHandler, createScriptHandler } from '@openpanel/nextjs/server';
import { createRouteHandler } from '@openpanel/nextjs/server';
export const POST = createNextRouteHandler();
export const GET = createScriptHandler()
const routeHandler = createRouteHandler();
// Export the same handler for all HTTP methods - it routes internally based on pathname
export const GET = routeHandler;
export const POST = routeHandler;
```
Remember to change the `apiUrl` and `cdnUrl` in the `OpenPanelComponent` to your own server.

View File

@@ -14,7 +14,7 @@
"@hyperdx/node-opentelemetry": "^0.8.1",
"@number-flow/react": "0.3.5",
"@openpanel/common": "workspace:*",
"@openpanel/nextjs": "^1.0.17",
"@openpanel/nextjs": "^1.1.0",
"@openpanel/payments": "workspace:^",
"@openpanel/sdk-info": "workspace:^",
"@openstatus/react": "0.0.3",