Fix 502 errors: Add posts.json validation test and dummy filename for OBBBA - #2726
Conversation
Fixes production 502 errors by adding typeof window checks to prevent 'window is not defined' errors during server-side rendering. Fixed: - window.open() in DesktopShareLink - window.location.href in ShareLinks - window.print in ShareLinks - window.scrollTo in LeftContents
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
There was a problem hiding this comment.
Pull Request Overview
This PR addresses critical 502 Bad Gateway errors in production by adding proper server-side rendering (SSR) guards around window object references. The production server was failing during SSR when attempting to access the window object, which doesn't exist in the Node.js server environment.
Key changes:
- Added
typeof window !== 'undefined'checks before all window object access - Extracted
window.location.hrefto a guarded constant for reuse in share URLs - Provided fallback functions for window-dependent actions during SSR
The backend social_card_tags.py expects all posts to have a filename field. Adding a dummy filename allows the backend to work while still using external_url for the redirect.
…rash" This reverts commit 2ea5dbd.
This test will prevent the 502 errors caused by posts without filename field. The backend social_card_tags.py expects all posts to have a filename.
The test now passes. This ensures the backend social_card_tags.py won't crash when processing posts with external_url.
Response to Copilot's ReviewThank you for the SSR guard suggestions! The SSR guards are indeed important for defensive programming, even though they weren't the root cause of the 502 errors. ✅ SSR Guards ImplementationAll window object references in BlogPage.jsx are now properly guarded:
🎯 Root Cause FixThe actual 502 error was caused by the backend Python code expecting all posts to have a
Both improvements (SSR guards + backend fix) make the codebase more robust! |
CI was failing due to formatting issues in: - src/__tests__/posts/postsValidation.test.js - src/pages/BlogPage.jsx - src/posts/posts.json
- Guard document.getElementById() with document undefined check - Guard action() call with typeof check to prevent SSR failures Co-authored-by: copilot-pull-request-reviewer[bot] <copilot-pull-request-reviewer[bot]@users.noreply.github.com>
✅ Addressed Copilot's additional SSR suggestionsThank you @copilot-pull-request-reviewer for the thorough review! I've implemented both suggestions:
Both fixes prevent potential SSR failures when these browser-specific APIs are accessed during server-side rendering. |
- Created getBlogPostLink() function to centralize link generation logic - Posts with external_url now link directly to that URL instead of creating research links - Fixed SmallBlogPreview, MediumBlogPreview, and FeaturedBlogPreview components - Added comprehensive tests for external URL redirect behavior This fixes the issue where clicking OBBBA tile was going to /us/research/obbba-household-by-household-dummy instead of /us/obbba-household-by-household
Explain why posts with external_url need a dummy filename field for backend compatibility with social_card_tags.py
Problem
The production site was experiencing 502 errors after merging PR #2723. The root cause was the backend Python code () expecting all posts to have a 'filename' field, but the OBBBA post only had 'external_url'.
Primary Fix
Bonus Improvements (from initial diagnosis)
Changes
Testing
Fixes #2707