Defer off-screen Query Loop blocks to a viewport placeholder#27
Draft
roborourke wants to merge 1 commit into
Draft
Defer off-screen Query Loop blocks to a viewport placeholder#27roborourke wants to merge 1 commit into
roborourke wants to merge 1 commit into
Conversation
Rendering a Query Loop's edit component mounts the core block, which immediately fires a REST request to fetch posts for the preview. On a page with many query loops, every one of those requests is dispatched up front on editor load. Add a withViewportPlaceholder HOC (registered last, so it wraps the plugin's other core/query enhancements) that replaces an off-screen Query Loop with a cheap placeholder issuing no requests. An IntersectionObserver — built from the target node's own defaultView so it works whether or not the canvas is iframed — swaps in the real block once it nears the viewport (300px rootMargin preload). Selecting the block or one of its inner blocks renders it immediately, and once rendered a block stays rendered so scrolling away neither discards edits nor refetches. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EAvqR2G3Q3aJkTtePzmLg7
Playwright test resultsDetails
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
An alternative approach to making pages with many Query Loop blocks more performant in the editor.
Rendering a Query Loop's edit component mounts the core block, which immediately fires a REST request to fetch its posts for the preview. On a page with lots of query loops, every one of those requests is dispatched up front on editor load — slow, and it can hammer the server.
This adds a
withViewportPlaceholderHOC that replaces an off-screen Query Loop with a cheap placeholder that issues no requests. The real block is only mounted once it scrolls near the viewport, spreading the API requests out as the user scrolls rather than firing them all at load.How it works
withViewportPlaceholderwrapscore/query'seditor.BlockEdit. It is registered last, so it is the outermost HOC — while the placeholder is shown, none of the plugin's othercore/queryenhancements (nor the core block itself) are mounted.QueryLoopPlaceholderrenders a<Placeholder>+<Spinner>and attaches anIntersectionObserver. The observer is constructed from the target node's ownownerDocument.defaultView, so it uses the correct viewport whether or not the editor canvas is iframed (site editor / block-themed post editor). A300pxrootMarginpreloads a block just before it comes on screen. IfIntersectionObserveris unavailable, the block renders immediately as a safe fallback.Changes
src/index.js— newQueryLoopPlaceholdercomponent andwithViewportPlaceholderHOC + filter registration.src/index.scss— placeholder styling (reserves min-height so the block is observable and the layout doesn't jump on swap-in).tests/e2e/viewport-placeholder.spec.js— e2e coverage: off-screen loops render placeholders and load on scroll; a selected loop renders immediately.CLAUDE.md— documents the new lazy-rendering behavior.Testing
npm run build— compiles cleanly.npm run lint:js/npm run lint:css— clean.🤖 Generated with Claude Code
Generated by Claude Code