Skip to content

Commit 1643f70

Browse files
author
wizard-ci[bot]
committed
wizard-ci: next-js/15-app-router-todo
1 parent ca0defc commit 1643f70

File tree

6 files changed

+470
-2
lines changed

6 files changed

+470
-2
lines changed

apps/next-js/15-app-router-todo/components/todos/todo-list.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { useState, useEffect } from 'react';
44
import Link from 'next/link';
5+
import posthog from 'posthog-js';
56
import { Todo } from '@/lib/data';
67
import { TodoForm } from './todo-form';
78
import { TodoItem } from './todo-item';
@@ -24,6 +25,9 @@ export function TodoList() {
2425
}
2526
} catch (error) {
2627
console.error('Failed to fetch todos:', error);
28+
posthog.capture('todo_fetch_error', {
29+
error: error instanceof Error ? error.message : 'Unknown error',
30+
});
2731
} finally {
2832
setLoading(false);
2933
}
@@ -42,9 +46,16 @@ export function TodoList() {
4246
if (response.ok) {
4347
const newTodo = await response.json();
4448
setTodos([...todos, newTodo]);
49+
posthog.capture('todo_created', {
50+
todo_id: newTodo.id,
51+
has_description: !!description,
52+
});
4553
}
4654
} catch (error) {
4755
console.error('Failed to add todo:', error);
56+
posthog.capture('todo_create_error', {
57+
error: error instanceof Error ? error.message : 'Unknown error',
58+
});
4859
}
4960
};
5061

@@ -61,9 +72,22 @@ export function TodoList() {
6172
if (response.ok) {
6273
const updatedTodo = await response.json();
6374
setTodos(todos.map((todo) => (todo.id === id ? updatedTodo : todo)));
75+
if (completed) {
76+
posthog.capture('todo_completed', {
77+
todo_id: id,
78+
});
79+
} else {
80+
posthog.capture('todo_uncompleted', {
81+
todo_id: id,
82+
});
83+
}
6484
}
6585
} catch (error) {
6686
console.error('Failed to update todo:', error);
87+
posthog.capture('todo_update_error', {
88+
todo_id: id,
89+
error: error instanceof Error ? error.message : 'Unknown error',
90+
});
6791
}
6892
};
6993

@@ -75,9 +99,16 @@ export function TodoList() {
7599

76100
if (response.ok) {
77101
setTodos(todos.filter((todo) => todo.id !== id));
102+
posthog.capture('todo_deleted', {
103+
todo_id: id,
104+
});
78105
}
79106
} catch (error) {
80107
console.error('Failed to delete todo:', error);
108+
posthog.capture('todo_delete_error', {
109+
todo_id: id,
110+
error: error instanceof Error ? error.message : 'Unknown error',
111+
});
81112
}
82113
};
83114

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import posthog from "posthog-js"
2+
3+
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
4+
api_host: "/ingest",
5+
ui_host: "https://us.posthog.com",
6+
// Include the defaults option as required by PostHog
7+
defaults: '2025-05-24',
8+
// Enables capturing unhandled exceptions via Error Tracking
9+
capture_exceptions: true,
10+
// Turn on debug in development mode
11+
debug: process.env.NODE_ENV === "development",
12+
});
13+
14+
//IMPORTANT: Never combine this approach with other client-side PostHog initialization approaches, especially components like a PostHogProvider. instrumentation-client.ts is the correct solution for initializating client-side PostHog in Next.js 15.3+ apps.

apps/next-js/15-app-router-todo/next.config.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@ import type { NextConfig } from 'next';
22

33
const nextConfig: NextConfig = {
44
// Configuration for stable Next.js 15
5+
async rewrites() {
6+
return [
7+
{
8+
source: "/ingest/static/:path*",
9+
destination: "https://us-assets.i.posthog.com/static/:path*",
10+
},
11+
{
12+
source: "/ingest/:path*",
13+
destination: "https://us.i.posthog.com/:path*",
14+
},
15+
];
16+
},
17+
// This is required to support PostHog trailing slash API requests
18+
skipTrailingSlashRedirect: true,
519
};
620

721
export default nextConfig;

apps/next-js/15-app-router-todo/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
"lucide-react": "^0.511.0",
1717
"next": "15.5.7",
1818
"postcss": "^8.5.3",
19+
"posthog-js": "^1.321.2",
20+
"posthog-node": "^5.21.0",
1921
"radix-ui": "^1.4.2",
2022
"react": "19.1.2",
2123
"react-dom": "19.1.2",

0 commit comments

Comments
 (0)