AgentSkillsCN

posthog-local-dev-loop

配置PostHog本地开发,支持热重载和测试。 在设置开发环境、配置测试工作流或建立与PostHog的快速迭代周期时使用。 可通过“posthog dev setup”、“posthog local development”、“posthog dev环境”、“develop with posthog”等短语触发。

SKILL.md
--- frontmatter
name: posthog-local-dev-loop
description: |
  Configure PostHog local development with hot reload and testing.
  Use when setting up a development environment, configuring test workflows,
  or establishing a fast iteration cycle with PostHog.
  Trigger with phrases like "posthog dev setup", "posthog local development",
  "posthog dev environment", "develop with posthog".
allowed-tools: Read, Write, Edit, Bash(npm:*), Bash(pnpm:*), Grep
version: 1.0.0
license: MIT
author: Jeremy Longshore <jeremy@intentsolutions.io>

PostHog Local Dev Loop

Overview

Set up a fast, reproducible local development workflow for PostHog.

Prerequisites

  • Completed posthog-install-auth setup
  • Node.js 18+ with npm/pnpm
  • Code editor with TypeScript support
  • Git for version control

Instructions

Step 1: Create Project Structure

code
my-posthog-project/
├── src/
│   ├── posthog/
│   │   ├── client.ts       # PostHog client wrapper
│   │   ├── config.ts       # Configuration management
│   │   └── utils.ts        # Helper functions
│   └── index.ts
├── tests/
│   └── posthog.test.ts
├── .env.local              # Local secrets (git-ignored)
├── .env.example            # Template for team
└── package.json

Step 2: Configure Environment

bash
# Copy environment template
cp .env.example .env.local

# Install dependencies
npm install

# Start development server
npm run dev

Step 3: Setup Hot Reload

json
{
  "scripts": {
    "dev": "tsx watch src/index.ts",
    "test": "vitest",
    "test:watch": "vitest --watch"
  }
}

Step 4: Configure Testing

typescript
import { describe, it, expect, vi } from 'vitest';
import { PostHogClient } from '../src/posthog/client';

describe('PostHog Client', () => {
  it('should initialize with API key', () => {
    const client = new PostHogClient({ apiKey: 'test-key' });
    expect(client).toBeDefined();
  });
});

Output

  • Working development environment with hot reload
  • Configured test suite with mocking
  • Environment variable management
  • Fast iteration cycle for PostHog development

Error Handling

ErrorCauseSolution
Module not foundMissing dependencyRun npm install
Port in useAnother processKill process or change port
Env not loadedMissing .env.localCopy from .env.example
Test timeoutSlow networkIncrease test timeout

Examples

Mock PostHog Responses

typescript
vi.mock('@posthog/sdk', () => ({
  PostHogClient: vi.fn().mockImplementation(() => ({
    // Mock methods here
  })),
}));

Debug Mode

bash
# Enable verbose logging
DEBUG=POSTHOG=* npm run dev

Resources

Next Steps

See posthog-sdk-patterns for production-ready code patterns.