Skip to content
This repository was archived by the owner on Jul 6, 2026. It is now read-only.

Fix 502 errors: Add posts.json validation test and dummy filename for OBBBA - #2726

Merged
MaxGhenis merged 9 commits into
masterfrom
hotfix/ssr-window-errors
Aug 11, 2025
Merged

MaxGhenis merged 9 commits into
masterfrom
hotfix/ssr-window-errors

Conversation

@MaxGhenis

@MaxGhenis MaxGhenis commented Aug 11, 2025 •

Copy link
Copy Markdown
Contributor

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

  1. Added test coverage () to validate posts.json structure
  2. Added dummy filename to the OBBBA post to prevent backend crash
  3. Used TDD approach: Test failed first, then passed with fix

Bonus Improvements (from initial diagnosis)

  • Added SSR guards for window object references in BlogPage.jsx
  • While these weren't the root cause, they're good defensive programming for SSR compatibility

Changes

  • ✅ Added validation test for posts.json structure
  • ✅ Added 'filename': 'obbba-household-by-household-dummy.md' to OBBBA post
  • ✅ Added SSR guards for window references (defensive improvement)

Testing

  • Test suite validates that posts with external_url also have filename
  • The OBBBA tile correctly redirects to /us/obbba-household-by-household
  • Backend social_card_tags.py won't throw KeyError anymore

Fixes #2707

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
Copilot AI review requested due to automatic review settings August 11, 2025 17:02
@MaxGhenis MaxGhenis added the bug Something isn't working label Aug 11, 2025
@vercel

vercel Bot commented Aug 11, 2025 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Project Status Preview Comments Updated (UTC)
policyengine-app ✅ Ready Visit Preview 💬 Add feedback Aug 11, 2025 7:48pm

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.href to a guarded constant for reuse in share URLs
  • Provided fallback functions for window-dependent actions during SSR

Comment thread src/pages/BlogPage.jsx Outdated
Comment thread src/pages/BlogPage.jsx Outdated
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.
@MaxGhenis MaxGhenis changed the title CRITICAL: Fix 502 errors by adding SSR guards for window references Fix backend crash by adding dummy filename to OBBBA post Aug 11, 2025
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.
@MaxGhenis
MaxGhenis marked this pull request as draft August 11, 2025 19:23
The test now passes. This ensures the backend social_card_tags.py
won't crash when processing posts with external_url.
@MaxGhenis MaxGhenis changed the title Fix backend crash by adding dummy filename to OBBBA post Fix backend crash: Add validation test and dummy filename to OBBBA post Aug 11, 2025
@MaxGhenis
MaxGhenis marked this pull request as ready for review August 11, 2025 19:25
@MaxGhenis MaxGhenis changed the title Fix backend crash: Add validation test and dummy filename to OBBBA post Fix 502 errors: Add posts.json validation test and dummy filename for OBBBA Aug 11, 2025
@MaxGhenis

Copy link
Copy Markdown
Contributor Author

Response to Copilot's Review

Thank 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 Implementation

All window object references in BlogPage.jsx are now properly guarded:

  • Line 626-630: Guarded window.open() in ShareButton click handler
  • Line 658: Guarded window.location.href extraction for share URLs
  • Line 692: Guarded window.print() function
  • Line 743-751: Guarded window.scrollTo() and document.getElementById() in TableOfContents

🎯 Root Cause Fix

The actual 502 error was caused by the backend Python code expecting all posts to have a filename field. We've:

  1. Added a test to validate posts.json structure
  2. Added a dummy filename to the OBBBA post to prevent backend crashes
  3. Used TDD approach - test failed first, then passed with the fix

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>
@MaxGhenis

Copy link
Copy Markdown
Contributor Author

✅ Addressed Copilot's additional SSR suggestions

Thank you @copilot-pull-request-reviewer for the thorough review! I've implemented both suggestions:

  1. Line 743: Added typeof document \!== 'undefined' check alongside the window check
  2. Line 629: Added typeof action === 'function' check before calling action()

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
@MaxGhenis
MaxGhenis marked this pull request as ready for review August 11, 2025 19:46
@MaxGhenis
MaxGhenis merged commit baab093 into master Aug 11, 2025
3 checks passed
@MaxGhenis
MaxGhenis deleted the hotfix/ssr-window-errors branch August 11, 2025 19:47
@github-project-automation github-project-automation Bot moved this from Todo to Done in policyengine-app Aug 11, 2025

This branch was successfully deployed

1 active deployment
Preview — 0dcccd53 Deployed Aug 11, 2025 by vercel[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

bug Something isn't working

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants