Inline PDF Previews reset to page 1 when scrolling through a topic

Summary

Inline PDF Previews consistently reset their scroll position (back to page 1) when scrolling through a long topic.
This happens because the PDF embed is being destroyed and re-inserted when the post scrolls out of view, likely due to Discourse’s post-stream recycling / virtualization.

This affects all browsers, all PDFs, and occurs even with all plugins and themes disabled.

Reproduction Steps

  1. Create a topic with a PDF attachment.
  2. Ensure the Inline PDF Previews component is active.
  3. Scroll down through the topic until the post containing the PDF leaves the viewport.
  4. Scroll back up to the same PDF preview.
  5. Result:
    • The PDF reloads from the beginning.
    • The preview scroll position is lost.
    • The PDF is re-rendered as if freshly loaded.

No network request for the PDF occurs, which indicates the DOM node was removed locally and recreated.

Expected Behaviour

  • The PDF preview should maintain its scroll position and should not reload simply due to scrolling the topic.
  • Other embedded content (images, videos, Oneboxes) does not reset state on scroll; PDF previews should behave consistently.

Actual Behaviour

  • As soon as the post goes out of visible range, Discourse unloads the DOM node.
  • When the post scrolls back into view, the embed is reconstructed.
  • This forces the browser’s native PDF renderer to start again from page 1.
  • This occurs every time the post leaves the viewport (up or down).

Environment

  • Occurs on all tested browsers: Chrome, Firefox, Safari, Edge
  • Occurs with all plugins disabled (Safe Mode)
  • Occurs with all theme components disabled
  • Occurs with multiple PDF files of different sizes and formats
  • Occurs on Discourse (tests-passed) latest branch (as of date posted)

Technical Notes

Discourse appears to be unmounting off-screen post components as part of stream virtualization.
Because the PDF preview uses a simple <embed> / <iframe>, it has no persistence mechanism, so it completely resets whenever:

  • willDestroyElement() is triggered
  • the post’s container is re-rendered
  • the DOM node is recreated on re-entry into viewport

This is unique to PDFs because browser-native PDF viewers do not preserve scroll state across DOM reconstruction.

This makes long topics with PDFs frustrating to read, especially for multi-page attachments.

Requested Resolution

  • Prevent the Inline PDF Preview component from being destroyed during post recycling OR
  • Provide a way for plugin/theme components to declare “persistent state” so Discourse will not unmount them
  • Or expose a site setting to disable post-stream recycling for posts containing inline PDF viewers

Even a partial fix — e.g., skipping recycling for posts containing PDF embeds — would solve the issue.

Additional Context

This is not a one-off environment issue:
It has been reproduced reliably under clean test builds, multiple browsers, and across PDFs.

There are no Meta topics describing this behaviour, so this may be an unreported regression or an unintended consequence of stream virtualization improvements.

1 Like