Skip to content

Latest commit

 

History

History
605 lines (479 loc) · 22.3 KB

File metadata and controls

605 lines (479 loc) · 22.3 KB

FIX LOG - AgriTech Blog

🚨 CRITICAL: Always check this log before making deployment or API changes!


FIX #011: Global Scroll Reveal Animation Fix & Prevention System

Date: April 9, 2026
Status: ✅ RESOLVED
Priority: MEDIUM
Affected: Entire site (Home, About, Portfolio, Posts), UI visibility

Problem Description

  • Elements using the useScrollReveal hook were sporadically staying invisible (opacity-0).
  • Affected critical sections like Search Bar, Tag Cloud, and "Latest Articles".
  • The hook failed to trigger if elements were already above the fold on load or if the page was too short to scroll.

Root Causes Identified

  1. Strict Threshold: Default 0.15 (15%) required too much of the element to be visible before triggering.
  2. Negative Root Margin: -40px prevented elements near the edges from revealing.
  3. No Initial Check: The hook only waited for a scroll event/intersection change and didn't check if the element was already visible on mount.

Solution Implemented (The Prevention System)

Refactored the global client/src/hooks/useScrollReveal.ts hook to be bulletproof:

  1. Callback Ref Implementation: Changed the hook to use a useState for the DOM element instead of useRef. This ensures the hook reacts immediately when an element mounts, even if it was behind a loading state (e.g., if (isLoading) return ...).
  2. Multi-Layered Fallbacks: Added getBoundingClientRect() checks inside a useEffect. It checks once immediately on mount, and then at 50ms, 250ms, and 1000ms intervals to handle cases where layout shifts or slow-loading content might move an element into view.
  3. Scroll Listener Fallback: Added a window scroll listener as a final safeguard to guarantee elements reveal the moment a user interacts with the page.
  4. Zero Threshold: Changed threshold to 0 so the reveal starts as soon as a single pixel enters the frame.
  5. Positive Root Margin: Changed to 50px so animations trigger slightly before the user reaches them for a smoother experience.

Files Modified

  • client/src/hooks/useScrollReveal.ts - Core hook logic refactored with callback refs.
  • client/src/pages/posts.tsx - Removed temporary hardcoded visibility fixes in favor of the now-working global hook.

⚠️ PREVENTIONAL STANDARDS:

  1. ALWAYS use the useScrollReveal hook for fade-in animations rather than custom timeouts or hardcoded logic.
  2. NEVER assume useRef will capture elements that are conditionally rendered after an initial mount; use the callback ref pattern in useScrollReveal.
  3. TEST elements behind loading states (like Blog Grid or Featured Stories) to ensure they reveal correctly once data arrives.

FIX #010: React Rules of Hooks Violation - Edit Post Page Crashing

Date: January 20, 2025
Status: ✅ RESOLVED
Priority: CRITICAL
Affected: Edit post page (/edit-post/:id), admin functionality

Problem Description

  • Edit post page crashing with React error #310
  • Console showing "Rendered more hooks than during the previous render"
  • "Rules of Hooks" violation preventing page from loading
  • All form fields (title, content, tags, excerpt, featured image) not visible

Root Causes Identified

  1. Early conditional returns before hooks - if (isEditing && isLoading) and if (isEditing && error) happening before all React hooks were called
  2. Inconsistent hook order - Hooks being called in different orders between renders
  3. Rules of Hooks violation - React expects all hooks to be called in the same order every time

Solution Implemented

Step 1: Restructure Component Hook Order

BEFORE (BROKEN):

export default function CreatePost() {
  const { id } = useParams<{ id?: string }>();
  const [, setLocation] = useLocation();
  
  // Early return before all hooks - VIOLATES RULES OF HOOKS
  if (isEditing && isLoading) {
    return <LoadingSpinner />;
  }
  
  // More hooks called after conditional return
  const { data: post, isLoading, error } = useQuery({...});
  const autoSaveMutation = useMutation({...});
  // ... more hooks
}

AFTER (WORKING):

export default function CreatePost() {
  // ✅ ALL HOOKS CALLED FIRST, BEFORE ANY CONDITIONAL LOGIC
  const { id } = useParams<{ id?: string }>();
  const [, setLocation] = useLocation();
  const queryClient = useQueryClient();
  const { data: post, isLoading, error } = useQuery({...});
  const autoSaveMutation = useMutation({...});
  const saveMutation = useMutation({...});
  useEffect(() => {...}, [isEditing, post]);
  useEffect(() => {...}, [initialValues]);
  
  // ✅ Helper functions after hooks
  const handleAutoSave = async (data) => {...};
  const handleSave = async (data) => {...};
  
  // ✅ Conditional rendering after all hooks
  if (isEditing && isLoading) return <LoadingSpinner />;
  if (isEditing && error) return <ErrorState />;
  
  return <MainEditor />;
}

Step 2: Ensure Consistent Hook Order

  • All hooks moved to top of component
  • No conditional returns before hooks
  • Helper functions defined after hooks
  • Conditional rendering happens last

Files Modified

  • client/src/pages/create-post.tsx - Restructured hook order and component flow

Testing Verification

# Test edit post page
1. Navigate to /edit-post/5230605364
2. Check console - no React errors
3. Verify all form fields are visible
4. Test auto-save functionality
5. Confirm no "Rules of Hooks" violations

⚠️ CRITICAL RULES - NEVER VIOLATE THESE:

  1. NEVER put conditional returns before hooks - always call all hooks first
  2. NEVER call hooks inside loops, conditions, or nested functions
  3. ALWAYS call hooks in the same order every render
  4. ALWAYS test hook consistency - check for "Rendered more hooks" errors
  5. ALWAYS follow React's Rules of Hooks - they are not suggestions, they are requirements

Prevention Measures

  • Added comprehensive documentation in PRD.md
  • Created verification steps for hook consistency
  • Documented the fix pattern for future reference

FIX #001: Posts Endpoints Not Working - API Returning HTML Instead of JSON

Date: June 28, 2025
Status: ✅ RESOLVED
Priority: CRITICAL
Affected: Admin dashboard, blog posts API endpoints

Problem Description

  • Admin dashboard showing error: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
  • API endpoints /api/admin/blog-posts returning HTML instead of JSON
  • All API functions (TypeScript) returning 404 errors
  • Admin interface unable to load posts

Root Causes Identified

  1. Missing @vercel/node dependency - Required for TypeScript API functions in Vercel
  2. Complex vercel.json routing configuration - Interfering with Vercel's auto-detection
  3. Build configuration conflicts - Manual builds overriding zero-config approach

Solution Implemented

Step 1: Install Missing Dependency

npm install @vercel/node

Step 2: Simplify vercel.json Configuration

BEFORE (BROKEN):

{
  "version": 2,
  "builds": [
    {
      "src": "api/**/*.ts",
      "use": "@vercel/node"
    },
    {
      "src": "package.json", 
      "use": "@vercel/static-build",
      "config": {
        "distDir": "dist"
      }
    }
  ],
  "routes": [
    // Complex routing rules that interfered with API detection
  ]
}

AFTER (WORKING):

{
  "version": 2,
  "buildCommand": "npm run build",
  "outputDirectory": "dist"
}

Step 3: Let Vercel Auto-Detect API Functions

  • Removed manual builds configuration
  • Enabled zero-config approach for API routes
  • Vercel now automatically compiles TypeScript API functions

Files Modified

  • vercel.json - Simplified configuration
  • package.json - Added @vercel/node dependency
  • api/admin/blog-posts.ts - Enhanced error handling and debugging

Testing Verification

# Test API endpoint
curl -H "Accept: application/json" https://your-domain.vercel.app/api/admin/blog-posts

# Expected: JSON response with blog posts array
# Before fix: HTML error page

⚠️ CRITICAL RULES - NEVER VIOLATE THESE:

  1. NEVER remove @vercel/node from package.json
  2. NEVER add complex routing to vercel.json - Use zero-config approach
  3. NEVER manually specify API builds - Let Vercel auto-detect
  4. ALWAYS test API endpoints after deployment changes
  5. ALWAYS check this log before modifying Vercel configuration

Prevention Measures

  • Added comprehensive error handling to API functions
  • Documented zero-config approach as the standard
  • Created this fix log for future reference

FIX #002: Public Blog Endpoints Not Working - Frontend Unable to Fetch Posts

Date: June 28, 2025
Status: ✅ RESOLVED
Priority: CRITICAL
Affected: Blog frontend, public post display, home page featured posts

Problem Description

  • Blog frontend unable to load posts from /api/blog-posts endpoint
  • Frontend calling non-existent public API endpoints
  • Only admin endpoints (/api/admin/blog-posts) were working
  • Public blog features completely broken (posts grid, featured posts, individual posts)

Root Cause Analysis

Missing Public API Functions: The project had two types of endpoints:

  1. Admin endpoints (/api/admin/blog-posts) - Working (Vercel functions existed)
  2. Public endpoints (/api/blog-posts) - Missing (No Vercel functions created)

The frontend was calling public endpoints that didn't exist as Vercel API functions. The endpoints were defined in server/routes.ts (Express server) but no corresponding Vercel API functions existed.

Frontend Endpoints That Were Missing:

  • /api/blog-posts - Main posts list for blog grid
  • /api/blog-posts/featured - Featured posts for homepage
  • /api/blog-posts/[identifier] - Individual posts by slug/ID
  • /api/blog-posts/[id]/related - Related posts

Complete Solution Implemented

Step 1: Created Public Blog Posts API (api/blog-posts.ts)

// Key features:
- MongoDB connection and data mapping
- Filtered for published posts only (draft: { $ne: true })
- Support for query parameters (featured, category, limit, offset)
- Proper CORS headers and error handling
- Unique ID generation and deduplication

Step 2: Created Individual Post API (api/blog-posts/[identifier].ts)

// Key features:
- Supports both slug and numeric ID lookup
- Multiple ID resolution strategies (explicit ID, generated from ObjectId)
- Published posts only filtering
- Proper TypeScript type handling
- Comprehensive error handling

Step 3: Deployed and Verified

  • /api/blog-posts - Returns all published posts with full data
  • /api/blog-posts?featured=true - Returns only featured posts
  • /api/blog-posts/[slug] - Returns individual posts by slug
  • ✅ All endpoints return proper JSON with complete post data

Testing Commands That Now Work

# Test main posts endpoint
curl -H "Accept: application/json" https://your-domain.vercel.app/api/blog-posts

# Test featured posts
curl -H "Accept: application/json" https://your-domain.vercel.app/api/blog-posts?featured=true

# Test individual post by slug
curl -H "Accept: application/json" https://your-domain.vercel.app/api/blog-posts/your-post-slug

⚠️ CRITICAL PREVENTION RULES:

  1. NEVER assume frontend endpoints exist without verification
  2. ALWAYS create both admin AND public API functions
  3. ALWAYS test all endpoints the frontend calls
  4. REMEMBER: Server routes ≠ Vercel API functions
  5. CHECK frontend network requests to identify missing endpoints

Files Created/Modified

  • Created: api/blog-posts.ts (public posts list)
  • Created: api/blog-posts/[identifier].ts (individual posts)
  • Dependency: Used existing @vercel/node package
  • Database: Used existing MongoDB configuration

Architecture Insight

This project uses dual API architecture:

  • Admin APIs (/api/admin/*) - Full CRUD, drafts included
  • Public APIs (/api/*) - Read-only, published content only

When adding features, ALWAYS create both admin and public endpoints as needed.


FIX #003: Admin Page Not Accessible - Missing SPA Routing Configuration

Date: June 28, 2025
Status: ✅ RESOLVED
Priority: HIGH
Affected: Admin dashboard, all React SPA routes (admin, posts, etc.)

Problem Description

  • Admin dashboard at /admin returning 404 errors on Vercel deployment
  • React SPA routes not working on production (but working locally)
  • Users unable to access admin interface for content management
  • All frontend routes except root / inaccessible

Root Cause Analysis

Missing SPA Routing Configuration: When we simplified vercel.json to fix API issues in FIX #001, we removed the SPA routing configuration that React needs.

How React SPA Routing Works:

  • React apps are Single Page Applications - all routes are handled client-side
  • When user visits /admin, server must serve index.html and let React Router handle it
  • Without proper Vercel routing config, /admin returns 404 because no physical file exists

What Was Missing:

  • Route configuration to catch all URLs and serve index.html
  • Proper handling of filesystem requests (API, assets, etc.)

Complete Solution Implemented

Updated vercel.json Configuration:

BEFORE (BROKEN - Missing SPA Routes):

{
  "version": 2,
  "buildCommand": "npm run build",
  "outputDirectory": "dist"
}

AFTER (WORKING - With SPA Routing):

{
  "version": 2,
  "buildCommand": "npm run build",
  "outputDirectory": "dist",
  "routes": [
    {
      "src": "/googlec3cfbe8ec5429358.html",
      "dest": "/googlec3cfbe8ec5429358.html"
    },
    {
      "handle": "filesystem"
    },
    {
      "src": "/(.*)",
      "dest": "/index.html"
    }
  ]
}

How This Routing Works:

  1. Static Files First: Serves Google verification file directly
  2. Filesystem Handling: Serves API routes, assets, and static files
  3. SPA Fallback: Any other route serves index.html for React Router

Admin Features Now Accessible:

  • /admin - Main admin dashboard
  • /admin/seo - SEO performance dashboard
  • /create-post - Advanced post editor
  • /edit-post/:id - Post editing interface
  • ✅ All other React Router routes

Verification Commands:

# Test admin page (should return HTML, not 404)
curl -I https://your-domain.vercel.app/admin

# Test API still works (should return JSON)
curl https://your-domain.vercel.app/api/blog-posts

# Test SPA routes work
curl -I https://your-domain.vercel.app/posts
curl -I https://your-domain.vercel.app/create-post

⚠️ CRITICAL PREVENTION RULES:

  1. NEVER remove SPA routing from vercel.json - React apps require it
  2. ALWAYS test both API endpoints AND frontend routes after deployment
  3. REMEMBER: API routes ≠ SPA routes - Both need different handling
  4. SPA routing pattern: "src": "/(.*)", "dest": "/index.html" is essential
  5. Order matters: Static files → Filesystem → SPA fallback

Files Modified:

  • Modified: vercel.json - Added SPA routing configuration
  • Dependency: Maintains API auto-detection from FIX #001

Architecture Insight:

This project requires dual routing architecture:

  • API Routes: Auto-detected by Vercel (files in /api/)
  • SPA Routes: Configured in vercel.json to serve React app

Both are essential - removing either breaks functionality.


FIX #004: Dynamic API Route Not Working on Vercel

Date: June 28, 2025
Status: 🔄 IN PROGRESS
Priority: HIGH
Affected: Individual blog posts

Problem Description

The individual blog post API endpoint /api/blog-posts/[slug].ts returns 404 on deployed version, causing blog post pages to show "Article Not Found". Works correctly on localhost.

Root Causes Identified

  1. Vercel Function Detection Issue: Dynamic route not being recognized as valid serverless function
  2. Deployment Configuration: Possible issue with how Vercel handles dynamic routes in TypeScript
  3. File Naming Convention: Square bracket naming might not be properly detected during build

Current Status

  • ✅ Main API endpoints working (/api/blog-posts, /api/admin/blog-posts)
  • ❌ Dynamic route /api/blog-posts/[slug] returns generic 404
  • ✅ Local development works correctly
  • 🔄 Investigating Vercel dynamic route requirements

Attempted Solutions

  1. Renamed from [identifier].ts to [slug].ts
  2. Verified proper export structure (export default async function handler)
  3. Ensured file is properly committed and deployed
  4. Confirmed routing configuration allows API routes

Next Steps

  • Research Vercel dynamic route requirements for TypeScript functions
  • Consider alternative routing approaches
  • Test with simpler dynamic route patterns

Impact on User Experience

  • Blog post URLs work on localhost but fail on deployed site
  • Users see "Article Not Found" for valid blog posts
  • SEO and sharing functionality broken for individual posts

Solution Applied

  • Replaced Dynamic Routes with Query Parameters:
    • Removed problematic /api/blog-posts/[slug].ts
    • Created /api/blog-post.ts with query parameter support (?slug= or ?id=)
  • Updated Frontend API Calls:
    • Changed from /api/blog-posts/${slug} to /api/blog-post?slug=${slug}
  • Simplified API Structure: Moved individual post endpoint to root API directory
  • Maintained Backward Compatibility: Supports both slug and ID parameters

API Endpoint Details

// New working endpoint
GET /api/blog-post?slug=post-slug-here
GET /api/blog-post?id=123456789

// Old broken endpoint (removed)
GET /api/blog-posts/[slug]

Testing Results

  • https://tech-san.vercel.app/api/blog-post?slug=first-week-of-internship-tip-precision-and-quick-communication-has-needed → Returns correct JSON
  • ✅ Blog post pages now load correctly on deployed site
  • ✅ Local development continues to work

Prevention Guidelines

  • Avoid Nested Dynamic Routes: Use root-level API files for dynamic content
  • Prefer Query Parameters: More reliable than path parameters for Vercel deployment
  • Test Deployed APIs Separately: Always test API endpoints directly before testing frontend
  • Use Simple API Structure: Keep dynamic endpoints in /api/ root directory

Architecture Recommendation

For future API endpoints requiring dynamic content:

✅ Good: /api/endpoint.ts with ?param=value
❌ Avoid: /api/nested/[param].ts

FIX #012: Page Loading Performance Optimization

Date: April 9, 2026
Status: ✅ RESOLVED
Priority: MEDIUM
Affected: Entire site, specifically List views and Post transitions

Problem Description

  • Initial page loads for list views (Home, /posts) were slow due to large API payloads (transferring full Markdown content for all posts).
  • Transitions from list views to individual articles felt sluggish due to waiting for single-post API calls.

Solution Implemented

  1. API Payload Optimization: Modified api/posts.ts to strip the heavy content field from all list-based and featured post responses. This reduced the average payload size by >90%.
  2. Hover-Based Prefetching: Implemented background preloading in PostsPage, BlogGrid, and FeaturedStories. Full article data is now fetched as soon as a user hovers over a link, making the final click-to-transition feel instantaneous.

Files Modified

  • api/posts.ts - Optimized MongoDB mapping for list views.
  • client/src/pages/posts.tsx - Added prefetching to the main list.
  • client/src/components/blog-grid.tsx - Added prefetching to homepage articles.
  • client/src/components/featured-stories.tsx - Added prefetching to featured stories.

⚠️ PERFORMANCE STANDARDS:

  1. NEVER return full content in list APIs; only return metadata, excerpts, and IDs.
  2. ALWAYS use queryClient.prefetchQuery on hover for high-traffic links to provide an "instant" feel.

FIX #XXX: [Problem Title]

Date: [Date]
Status: [RESOLVED/IN PROGRESS/MONITORING]
Priority: [CRITICAL/HIGH/MEDIUM/LOW]
Affected: [What was broken]

Problem Description

[Detailed description of the issue]

Root Causes

[What caused the problem]

Solution

[How it was fixed]

Files Modified

[List of changed files]

Prevention Rules

[What to never do again]


Quick Reference Commands

Test API Endpoints

# Test blog posts API
curl -H "Accept: application/json" https://your-domain.vercel.app/api/admin/blog-posts

# Test with local development
npm run dev
curl http://localhost:3000/api/admin/blog-posts

Safe Deployment Process

# 1. Always test locally first
npm run dev

# 2. Check TypeScript compilation
npm run check

# 3. Build locally to verify
npm run build

# 4. Deploy with our script
npm run deploy

# 5. Test API endpoints immediately after deployment

Emergency Rollback

# If deployment breaks, immediately revert vercel.json to working state:
git checkout HEAD~1 vercel.json
vercel --prod

Dependencies That Must Never Be Removed

  • @vercel/node - Required for TypeScript API functions
  • mongodb - Database connection
  • cors - API CORS handling

⚠️ WARNING SIGNS - If You See These, Check This Log!

  • API returning HTML instead of JSON
  • 404 errors on /api/* routes
  • Unexpected token '<' errors in frontend
  • Vercel build warnings about unused build settings
  • Admin dashboard not loading posts

Last Updated: June 28, 2025
Next Review: [Set monthly review date]