Core\Html::later
Core\Html::later(
callable(): mixed $fn,
{placeholder?: Core\Html\Markup, error?: Core\Html\Markup, deadline?: Core\Time\Duration},
): Core\Html\MarkupBuilds a slow part of a page in a separate task, while the rest of the page is written.
Core\Html::later takes a callable and returns a placeholder at once. You write the placeholder where the part belongs. When the page is finished, the output of the callable replaces the placeholder. Several later calls run at the same time, so a page with three slow parts waits for the slowest one, not for all three in a row.
If the callable throws an error, the page shows the error markup in its place, and the status stays the same. deadline sets the longest time the callable may run. On a route with slotted: true, the browser gets the page first and each part as soon as it is ready.
Good to know: a callable in later cannot set a header, a cookie or the status. It can read the request with Core\Request, but not its body. A body method throws a LogicError there, so read the body in the main script and pass the value to the callable.
The examples below show two parts that run at the same time, a part that fails, and a part that takes too long.
Parameters
$fncallable(): mixedThe function to run. It takes no arguments. Its output is what it echoes, followed by theCore\Html\Markupit returns, if any. It can read the request withCore\Request, but not the body. A body read throws aLogicError.
Options
placeholderCore\Html\MarkupoptionalThe markup the page shows until the output is ready. The default is empty.errorCore\Html\MarkupoptionalThe markup shown instead of the output when$fnthrows an error or runs past itsdeadline. The default is empty.deadlineCore\Time\DurationoptionalThe longest time$fnmay run. Without it, only the request's own limits apply.
Return value
Returns Core\Html\Markup. A Core\Html\Markup placeholder. Outside an HTML response, $fn runs at once and the result is its output.
Errors
LogicErrorWhen the page writes the same placeholder twice. A placeholder that the page never writes is not an error: its$fndoes not run, and a warning is logged.
Changelog
| Version | Change |
|---|---|
0.0.5 | A callable in Core\Html::later can read the request with Core\Request. The path, the query, a header, a cookie and the route parameters are the same as in the main script. A body read in the callable throws a LogicError. |
Examples
Two parts of a page
<?nvs// Shows how a page builds two of its parts at the same time with `Core\Html::later`.
// `Core\Test::request` runs this file again for the request. In that run,// `Core\Env::context()` returns `Request`, and this part writes the page.if (Core\Env::context() == Core\Env::Context::Request) { // Each call starts its anonymous function and returns a placeholder at once. // The two anonymous functions run at the same time, so the page waits about 30 ms, not 50 ms. Core\Html\Markup $posts = Core\Html::later(fn(): Core\Html\Markup => { Core\Time::sleep(30ms); return html`<p>3 new blog posts</p>`; }); Core\Html\Markup $offers = Core\Html::later(fn(): Core\Html\Markup => { Core\Time::sleep(20ms); return html`<p>2 offers in the shop</p>`; });
// Write each placeholder where its part belongs. // Before the page is sent, the output of each anonymous function replaces its placeholder. echo html`<h1>Welcome back</h1>`, "\n", $posts, "\n", $offers, "\n";} else { // From the command line, this part sends the request and prints the page. var $page = Core\Test::request(Core\Http\Method::Get, '/'); echo $page->body();}<h1>Welcome back</h1><p>3 new blog posts</p><p>2 offers in the shop</p>A part that fails
<?nvs// Shows what a page contains when one of its `later` parts throws an error.
// `Core\Test::request` runs this file again for the request. In that run,// `Core\Env::context()` returns `Request`, and this part writes the page.if (Core\Env::context() == Core\Env::Context::Request) { // The anonymous function throws an error, so the page shows the `error` markup in its place. Core\Html\Markup $reviews = Core\Html::later(fn(): void => { throw new RuntimeError('the review service is down'); }, { error: html`<p>Reviews are not available right now.</p>` });
// The rest of the page is sent as normal, and the status is still 200. echo html`<h1>Garden chair</h1>`, "\n", html`<p>Price: 49 EUR</p>`, "\n", $reviews, "\n";} else { // From the command line, this part sends the request and prints the status and the page. var $page = Core\Test::request(Core\Http\Method::Get, '/shop/garden-chair'); echo $page->status(), "\n", $page->body();}200<h1>Garden chair</h1><p>Price: 49 EUR</p><p>Reviews are not available right now.</p>A part with a time limit
<?nvs// Shows how `deadline` stops a slow part, so it does not make the whole page wait.
// `Core\Test::request` runs this file again for the request. In that run,// `Core\Env::context()` returns `Request`, and this part writes the page.if (Core\Env::context() == Core\Env::Context::Request) { // This anonymous function needs 2 seconds, but its `deadline` is 50 ms. // After 50 ms it stops, and the page shows the `error` markup instead. Core\Html\Markup $weather = Core\Html::later(fn(): Core\Html\Markup => { Core\Time::sleep(2s); return html`<p>Sunny, 24 degrees</p>`; }, { deadline: 50ms, error: html`<p>No weather report.</p>` });
// This part is fast, so its output is shown. Core\Html\Markup $news = Core\Html::later(fn(): Core\Html\Markup => html`<p>The blog has a new post.</p>`);
echo html`<h1>Today</h1>`, "\n", $news, "\n", $weather, "\n";} else { // From the command line, this part sends the request and prints the page. var $page = Core\Test::request(Core\Http\Method::Get, '/today'); echo $page->body();}<h1>Today</h1><p>The blog has a new post.</p><p>No weather report.</p>