GitHub

Lazy pagelets

A part of the page that is not needed right away is not rendered with it. The page gets a placeholder, and the browser asks for the pagelet when the placeholder becomes visible, or once the browser is idle.
  • Pagelet::lazy($url)

    The placeholder of a pagelet class, loaded from the URL.

  • LazyPagelet::LOAD_VISIBLE

    When the placeholder scrolls into view (the default).

  • LazyPagelet::LOAD_IDLE

    Once the browser has nothing else to do.

  • $response->pagelet()

    The endpoint answers with the pagelet, which replaces its placeholder.

Controller.php
// The page gets only a placeholder: nothing is rendered yet
$comments = CommentsPagelet::lazy(
    route('comments.pagelet', $post),
    placeholder: '<p>Loading the comments…</p>',
);

return view('post', compact('post', 'comments'));

// The endpoint
public function comments(Post $post)
{
    return (new AsyncResponse())
        ->pagelet(new CommentsPagelet($post))
        ->send();
}
Try it

Loaded once the browser is idle

The page was ready without it. Right after the page loaded, the browser requested it in the background: see the first response.

Scroll down
Not loaded yet

Loaded when it becomes visible

Nothing was requested for this one until its placeholder scrolled into view. Infinite scroll works the same way: see the blog of the demo app.