Skip to content

Conversation

@mrdavidlaing-mo
Copy link
Collaborator

Research better architectural patterns and approaches for environment variable debugging in Next.js applications.

Objective:
Find architectural approaches that avoid the issues we've been having with server-side code being processed in Edge Runtime.

Research Areas:

  • Using API routes vs instrumentation vs middleware for server-side debugging
  • Client-side debugging patterns that don't conflict with server-side code
  • Development-only debugging architectures
  • Best practices for environment variable inspection in Next.js
  • Separation of server/client debugging concerns
  • Using React dev tools or browser extensions instead of custom components
  • Alternative Next.js lifecycle hooks for environment debugging
  • Development vs production debugging strategies

Deliverables:

  1. Create specs/env-debugging-architecture-patterns.md with findings
  2. Document different architectural approaches with clear diagrams
  3. Include implementation examples for each pattern that avoid Edge Runtime issues
  4. Compare approaches and recommend best practices based on our learnings
  5. Provide migration path from our current broken implementation
  6. Test recommended approaches to ensure they work
  7. Commit findings to repository

Success Criteria:

  • Clear architectural recommendations that solve our current issues
  • Tested implementation examples that work without Edge Runtime errors
  • Best practices based on both research and our painful experience
  • Actionable next steps for fixing our current implementation

mrdavidlaing-mo and others added 5 commits July 21, 2025 10:52
For debugging we want the webapp to log all the environment variables it has access to on the client side
… (vibe-kanban 27bead3b)

Update log file naming to use YYYY-MM-DDTHH format instead of the current pattern. Check the @package.json for how the filenames are being generated
…n f0ca7c49)

Create server-side environment logging functionality that prints Next.js environment variables to the console when the webapp dev server starts up. This will help with debugging environment variable issues during development.

Requirements:
1. Create a server-side environment logger utility in apps/webapp/lib/debug/ServerEnvironmentLogger.ts
2. Log environment variables at webapp startup (only in development mode)
3. Group and categorize environment variables:
  - NEXT_PUBLIC_* variables (with sensitive key masking)
  - Firebase-related variables
  - Other relevant variables (NODE_ENV, PORT, etc.)
4. Include timestamp, working directory, and variable counts
5. Import and call the logger from the appropriate Next.js startup location

Implementation Notes:
- Only run in development mode (process.env.NODE_ENV === 'development')
- Mask sensitive values (anything with "key" in the name)
- Use console.group() for organized output
- Should appear in the webapp dev server logs after the Next.js startup banner

Acceptance Criteria:
- Server-side environment logger utility created
- Logger called at webapp startup
- Environment variables properly categorized and displayed
- Sensitive values are masked
- Only runs in development mode
- Output appears in webapp dev server logs
… 6b0052f2)

Research and document built-in Next.js debugging features for environment variables during development.

**Objective:**
Find out if Next.js has any built-in ways to debug and inspect environment variables on both server and client side during development.

**Research Areas:**
- Next.js CLI flags or options for environment variable debugging
- Built-in dev server logging for environment variables  
- Official Next.js debugging tools and developer features
- Environment variable inspection in development mode
- Next.js documentation on debugging environment variables
- Any built-in environment variable display or validation

**Deliverables:**
1. Create specs/nextjs-builtin-env-debugging.md with comprehensive findings
2. Document any built-in flags, options, or features discovered
3. Include practical code examples and usage instructions
4. Note what is NOT available built-in and limitations
5. Provide clear assessment of whether built-in features meet our debugging needs
6. Commit findings to repository

**Success Criteria:**
- Thorough research of official Next.js documentation and features
- Clear documentation of all available built-in debugging capabilities
- Practical examples ready for immediate use
- Honest assessment of gaps in built-in functionality
Research alternative implementation approaches for environment variable
debugging in Next.js applications to solve Edge Runtime issues.

## Key Research Findings:

**API Routes Approach**
- Clear server/client boundary separation
- On-demand debugging without runtime conflicts
- Secure with development-only guards

**Instrumentation Improvements**
- Added runtime-specific guards (NEXT_RUNTIME)
- Proper Edge Runtime detection and handling
- Maintains startup diagnostics capabilities

**Client-Side Safety**
- Restricted to NEXT_PUBLIC_ variables only
- Enhanced masking for sensitive values
- Clear separation from server-side concerns

## Architectural Benefits:

✅ Solves Edge Runtime processing issues
✅ Clear separation of server/client debugging
✅ Runtime-aware programming patterns
✅ Development-only activation with proper guards
✅ No server code in client bundles
✅ On-demand debugging via secure API endpoints

## Implementation:

- Created comprehensive specs document with patterns analysis
- Added /api/debug/environment route for server-side debugging
- Enhanced client component with safety guards
- Updated instrumentation.ts with runtime detection
- Included validation tests for approach verification

## Files:

- specs/env-debugging-architecture-patterns.md (comprehensive research)
- apps/webapp/app/api/debug/environment/route.ts (new API route)
- apps/webapp/components/debug/EnvironmentLogger.tsx (improved safety)
- apps/webapp/instrumentation.ts (runtime guards added)
- test-debug-endpoints.js (validation tests)

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
…ation

Remove the implementation examples and revert file modifications to keep
only the comprehensive research documentation in specs/.

## Changes:
- ❌ Removed apps/webapp/app/api/debug/environment/route.ts (API route example)
- ❌ Removed test-debug-endpoints.js (validation tests)
- ↩️  Reverted apps/webapp/components/debug/EnvironmentLogger.tsx to original state
- ↩️  Reverted apps/webapp/instrumentation.ts to original state
- ✅ Kept specs/env-debugging-architecture-patterns.md (comprehensive research)

## What Remains:
Only the comprehensive research documentation with architectural analysis,
implementation patterns, best practices, and migration strategies.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants