-
Notifications
You must be signed in to change notification settings - Fork 5
Fix HN content not rendering for iframe-blocked URLs #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -828,22 +828,96 @@ function renderContentViewState(contentView: ContentView): void { | |
| } | ||
|
|
||
| if (iframe) { | ||
| // Track if content loaded successfully | ||
| let loadSuccessful = false; | ||
| let loadTimeout: ReturnType<typeof setTimeout> | null = null; | ||
|
|
||
| const showError = () => { | ||
| const loading = document.getElementById('content-loading'); | ||
| const error = document.getElementById('content-error'); | ||
| if (loading) loading.classList.add('hidden'); | ||
| if (error) error.classList.remove('hidden'); | ||
| iframe.style.display = 'none'; | ||
| }; | ||
|
|
||
| const showContent = () => { | ||
| loadSuccessful = true; | ||
| if (loadTimeout) { | ||
| clearTimeout(loadTimeout); | ||
| loadTimeout = null; | ||
| } | ||
| const loading = document.getElementById('content-loading'); | ||
| if (loading) loading.classList.add('hidden'); | ||
| iframe.style.display = 'block'; | ||
| }; | ||
|
|
||
| // Set a timeout to detect if iframe fails to load (X-Frame-Options, CSP blocks) | ||
| // Many sites block iframe embedding but don't trigger error events | ||
| loadTimeout = setTimeout(() => { | ||
| if (!loadSuccessful) { | ||
| // After timeout, check if iframe has any accessible content | ||
| try { | ||
| // Try to access iframe content - will throw for blocked/cross-origin frames | ||
| const doc = iframe.contentDocument; | ||
| // If we can access and it's empty or about:blank, likely blocked | ||
| if (doc && (doc.body?.innerHTML === '' || doc.URL === 'about:blank')) { | ||
| showError(); | ||
| return; | ||
| } | ||
| // If accessible and has content, show it | ||
| if (doc && doc.body?.innerHTML) { | ||
| showContent(); | ||
| return; | ||
| } | ||
| // Cross-origin but might be loaded - give benefit of doubt | ||
| showContent(); | ||
| } catch { | ||
| // Cross-origin access denied - this is normal for loaded external sites | ||
| // The iframe might still be showing content, so show it | ||
| showContent(); | ||
| } | ||
| } | ||
| }, 5000); | ||
|
|
||
| iframe.addEventListener( | ||
| 'load', | ||
| () => { | ||
| const loading = document.getElementById('content-loading'); | ||
| if (loading) loading.classList.add('hidden'); | ||
| // iframe loaded - but might be empty if blocked by X-Frame-Options | ||
| // Check if we can detect empty content | ||
| setTimeout(() => { | ||
| try { | ||
| const doc = iframe.contentDocument; | ||
| // If we can access the document and it's essentially empty, likely blocked | ||
| if (doc) { | ||
| const bodyContent = doc.body?.innerHTML?.trim() || ''; | ||
| const hasContent = bodyContent.length > 0 && doc.URL !== 'about:blank'; | ||
| if (hasContent) { | ||
| showContent(); | ||
| } else { | ||
| // Empty content likely means blocked | ||
| showError(); | ||
| } | ||
| } else { | ||
| // Can't access document (cross-origin) - assume loaded successfully | ||
| showContent(); | ||
| } | ||
| } catch { | ||
| // Cross-origin access denied - iframe is loaded with external content | ||
| showContent(); | ||
| } | ||
| }, 100); | ||
| }, | ||
| { signal: getSignal() } | ||
| ); | ||
|
|
||
| iframe.addEventListener( | ||
| 'error', | ||
| () => { | ||
| const loading = document.getElementById('content-loading'); | ||
| const error = document.getElementById('content-error'); | ||
| if (loading) loading.classList.add('hidden'); | ||
| if (error) error.classList.remove('hidden'); | ||
| if (loadTimeout) { | ||
| clearTimeout(loadTimeout); | ||
| loadTimeout = null; | ||
| } | ||
| showError(); | ||
| }, | ||
| { signal: getSignal() } | ||
| ); | ||
|
Comment on lines
+831
to
923
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This new client-side logic for detecting blocked iframes is quite complex and has a couple of issues:
Given that the new server-side pre-check is the most reliable mechanism, you might consider simplifying or removing this complex client-side fallback. The current implementation adds significant complexity without reliably handling the primary cross-origin failure case. |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
catchblock currently assumes a URL is embeddable if thefetchrequest fails (e.g., due to a timeout). This is an optimistic approach that can lead to a poor user experience: the user waits for the check to time out, then the UI attempts to load the URL in an iframe, which might also fail, resulting in a blank panel.A safer approach would be to assume the URL is not embeddable on error. This would redirect to an external browser, which is a better fallback than a blank panel, preventing the user from seeing a blank, non-functional view.