Skip to content

Core\Response::sendSlotted

Core\Response::sendSlotted(): void

Sends the page to the browser as soon as the main script ends, and each slow part after it.

A page with Core\Html::later parts normally waits for every part before it is sent. Call Core\Response::sendSlotted() in the main script, and the page is sent first, with the placeholders in it. Each part then follows as soon as it is ready, and the browser puts it in the place of its placeholder. The visitor sees the page early.

A route can do the same with #[Core\Route(..., slotted: true)]. Use the call when the decision depends on the request. Calling it twice is the same as calling it once. A page with no later part is sent whole.

Two small inline scripts put each part in place. If the page's Content-Security-Policy limits scripts, Novis adds the hashes of both scripts to it for you.

Good to know: call it before the main script ends. Inside a callable that later or Core\Task::runAfterResponse runs, it throws a LogicError. A failed part shows its error markup, and the status stays the same.

The examples below show a page with two slow parts, a decision for one path only, and a part that fails.

Return value

Returns void. Nothing. Calling it twice is the same as calling it once.

Errors

  • LogicErrorThe main script has already ended. This is the case in a Core\Html::later function and in Core\Task::runAfterResponse work.

Changelog

VersionChange
0.0.6The Core\Response methods that send a body now start with send: sendJson, sendHtml, sendText, sendBytes and sendSlotted. startStream starts a streamed response. The old names were json, html, text, bytes, slotted and stream.

Examples

Send the page early

<?nvs
// Shows how `Core\Response::sendSlotted()` sends a page first and its slow parts 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) {
// The page is sent when the main script ends. Each part follows when it is ready.
Core\Response::sendSlotted();
// The browser shows the `placeholder` markup until the part arrives.
Core\Html\Markup $posts = Core\Html::later(fn(): Core\Html\Markup => {
Core\Time::sleep(30ms);
return html`<p>3 new blog posts</p>`;
}, { placeholder: html`<p>Loading 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>`;
}, { placeholder: html`<p>Loading offers</p>` });
// The finished page has each part in the place of 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();
}
Output
<h1>Welcome back</h1>
<p>3 new blog posts</p>
<p>2 offers in the shop</p>

Decide for one path

<?nvs
// Shows how a page calls `Core\Response::sendSlotted()` for one path and not for another.
// `Core\Test::request` runs this file again for each request. In those runs,
// `Core\Env::context()` returns `Request`, and this part writes the page.
if (Core\Env::context() == Core\Env::Context::Request) {
tainted string $path = Core\Request::path();
// Only the blog page is sent early. The shop page waits for its part.
if ($path == '/blog') {
Core\Response::sendSlotted();
}
Core\Html\Markup $list = Core\Html::later(fn(): Core\Html\Markup => {
Core\Time::sleep(10ms);
return html`<ul><li>first entry</li></ul>`;
});
// Both pages print the same heading and list.
echo html`<h1>{$path}</h1>`, "\n", $list, "\n";
} else {
// From the command line, this part sends two requests.
// It prints the blog page and then the shop page.
var $blog = Core\Test::request(Core\Http\Method::Get, '/blog');
var $shop = Core\Test::request(Core\Http\Method::Get, '/shop');
echo $blog->body(), $shop->body();
}
Output
<h1>/blog</h1>
<ul><li>first entry</li></ul>
<h1>/shop</h1>
<ul><li>first entry</li></ul>

A part that fails

<?nvs
// Shows what a page sent with `Core\Response::sendSlotted()` contains when one part fails.
// `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) {
Core\Response::sendSlotted();
// The anonymous function throws an error, so the `error` markup is sent in its place.
Core\Html\Markup $reviews = Core\Html::later(fn(): void => {
throw new RuntimeError('the review service is down');
}, { placeholder: html`<p>Loading reviews</p>`, error: html`<p>No reviews right now.</p>` });
// The status was sent with the page, so it is still 200.
echo html`<h1>Garden chair</h1>`, "\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();
}
Output
200
<h1>Garden chair</h1>
<p>No reviews right now.</p>