Spaces:
Paused
Paused
dr-data
Fix preview component: eliminate blinking, ensure HTML updates, add smooth transitions
dcd5e1d
Preview HTML Changes Not Reflecting - FIXED
Problem Reported
The preview was showing the starter HTML code but not reflecting HTML code changes smoothly. Changes to the HTML content were not being displayed in the preview.
Root Causes Identified
1. Competing useEffect Hooks
- Two different useEffects were trying to update
displayHtml - Circular dependencies in the initialization useEffect
[html, displayHtml] - Conflicting update logic causing race conditions
2. Complex Dependency Arrays
- Main update useEffect had
[html, isAiWorking, injectContentSeamlessly] - The
injectContentSeamlesslydependency was unnecessary and problematic - Changes to
htmlprop weren't reliably triggering updates
3. Iframe Content Not Syncing
secondaryHtmlstate wasn't being updated whenhtmlchanged- Only one iframe was getting updated content
- No forced re-renders when content changed
4. Update Detection Issues
- Multiple conditions preventing HTML updates
- Complex timing logic interfering with basic functionality
Fixes Applied
β Fix 1: Consolidated Update Logic
// BEFORE: Two competing useEffects
useEffect(() => {
if (html && html !== displayHtml) {
setDisplayHtml(html);
}
}, [html, displayHtml]); // Circular dependency!
useEffect(() => {
// Complex update logic...
}, [html, isAiWorking, injectContentSeamlessly]);
// AFTER: Single, reliable useEffect
useEffect(() => {
if (html !== displayHtml) {
setDisplayHtml(html);
setSecondaryHtml(html); // Keep both iframes in sync
prevHtmlRef.current = html;
}
// Enhanced updates only during AI streaming
}, [html, isAiWorking]); // Simplified dependencies
β Fix 2: Removed Circular Dependencies
- Eliminated
displayHtmlfrom dependency arrays - Simplified to only depend on
[html, isAiWorking] - Removed unnecessary
injectContentSeamlesslydependency
β Fix 3: Synchronized Iframe Content
// Now both iframes stay in sync
setDisplayHtml(html);
setSecondaryHtml(html);
β Fix 4: Added Forced Re-renders
// Force iframe re-render when content changes
<iframe
srcDoc={displayHtml}
key={`primary-${displayHtml.length}`} // Forces re-render
/>
<iframe
srcDoc={secondaryHtml || displayHtml}
key={`secondary-${(secondaryHtml || displayHtml).length}`} // Forces re-render
/>
β Fix 5: Enhanced Debug Logging
- Added detailed logging for HTML changes
- Track when displayHtml gets updated
- Monitor iframe loading and content updates
- Preview HTML content in logs for debugging
Expected Results
β Immediate HTML Updates
- Manual HTML edits should reflect in preview instantly
- No delay or lag in displaying HTML changes
- Starter HTML displays correctly on load
β Smooth AI Updates
- AI-generated content updates smoothly during streaming
- Enhanced transition effects during AI work
- Final AI output always displays correctly
β Reliable State Management
- No more competing update logic
- Consistent iframe content synchronization
- Proper cleanup and state management
β Better Debugging
- Console logs show HTML update flow
- Easy to track when changes occur
- Clear visibility into iframe loading
Technical Implementation
Key Changes Made:
- Single source of truth for HTML updates
- Immediate state updates when HTML prop changes
- Forced iframe re-renders via key prop
- Synchronized dual iframe content
- Simplified dependency management
Files Modified:
/components/editor/preview/index.tsx- Main Preview component
Testing Recommendations:
- β Edit HTML manually in the code editor - should update preview immediately
- β Test AI streaming updates - should show smooth progress
- β Verify starter HTML displays on fresh load
- β Check that final AI output always appears
- β Test rapid HTML changes for responsiveness
The preview should now immediately and smoothly reflect all HTML code changes! π