Nitro

Going further

Limitations

Nitro runs your methods and views in the browser, and that has a few consequences. Server work stays on the server, numbers and regular expressions follow the browser's rules, a few PHP functions aren't available, and a few things Blade allows can't be used in a component's view. None of these ever gives a different result silently.

On this page

Keeping server work in #[Server] methods

Queries, services, Auth, Cache, files, mail and the clock (now(), date()) all belong to the server. If a browser method uses them, the build fails and tells you the file and line.

PHP
// A browser method: the build fails, because the clock belongs to the server.
public function stamp(): void
{
    $this->savedAt = now()->toTimeString();
}

// A #[Server] method runs on the server, where now() is available.
#[Server]
public function stamp(): void
{
    $this->savedAt = now()->toTimeString();
}

Creating components with mount()

Only the server can create a component that has a mount() method. It does so on the first request, on a visit, or during a #[Server] call of the component whose view places it. A change made only in the browser, such as $set or a browser method, can't place a new one. So a <nitro:dynamic-component> that switches to such a component needs one of those first.

Numbers, strings and patterns

  • Numbers follow JavaScript. Integers are exact up to 253, so PHP_INT_MAX is 253-1 in the browser. A whole float arrives as an int. Functions such as tanh may differ in the last digit, just as they do between PHP builds.
  • Strings are UTF-8. Byte functions such as strlen and substr count bytes, as PHP does. A result that isn't valid UTF-8 appears as U+FFFD.
  • Regular expressions follow PCRE, translated to JavaScript. Features without an exact equivalent fail with a message: recursion, conditional groups, \K, backtracking verbs, callouts, (?|, inline modifiers inside the pattern, and duplicate group names. Without the /u flag, patterns match bytes.
  • Unicode follows the browser. Case mapping and character classes use the browser's Unicode version, which can be older than PHP's.

What isn't available in the browser

  • The $count argument of preg_replace(), and the percentage of similar_text()
  • strip_tags() with allowed tags
  • mb_convert_case() modes other than upper, lower and title
  • Str:: methods that haven't been ported (such as plural, uuid and of), and Str::slug and Str::ascii in languages other than en
  • htmlentities() and html_entity_decode() with ENT_HTML5, ENT_XHTML or ENT_XML1

Using one of these fails the build or fails with a message. They never give a different result from PHP. Call them from a #[Server] method instead.

Blade in a component's view

A component's view is compiled once and rendered by the server and the browser by the same rules, and both are tested against Laravel's Blade. Your layouts, mail and other plain Blade views are untouched: Laravel renders them as always. In a component's view, these fail the build with the file, the line and what to write instead:

Fails the build Write instead
@php , custom directives, @aware Work out values in the component (a property, a #[Computed] method)
Class-based Blade components ( app/View/Components ) Use an anonymous component ( resources/views/components )
<x-dynamic-component> , <x-slot :name> with a computed name Use <nitro:dynamic-component :is> ; name slots with a string
A loop's variable read after the @if (or outer loop) the loop is in has ended Read it inside that block. Right after the loop itself it works, as in PHP
An element opened in @if or @foreach and closed outside it Open and close the element inside the block
A block element inside a <p> ( <p><div> ), also through a component Close the <p> first, or use a <div>
A row inside an open row, a cell inside an open cell Close the row or cell first
Rows a loop renders directly in a <table> Wrap them in <tbody> . Rows written directly in the table are fine: the <tbody> is added for you, as browsers add it
Markup inside an <option> ( <option><b>x</b></option> ) Put only text in it: browsers disagree about markup there
A Blade component reading data the views share ( View::share() ) without being given it Pass it as an attribute

And a few things render, but not quite as Laravel would:

  • Data shared during a request, by a view composer or a middleware, doesn't reach an @included view or a Blade component inside a component's view. Pass it as data or as an attribute.
  • @includeFirst picks its view when the view is compiled, not on every render.
  • The server's HTML is written from the compiled view. The page the browser builds from it is exactly the one Blade's HTML gives, but the bytes can differ (attributes in double quotes, for example). In tests, check what a component shows (assertSee), not Blade's exact HTML.

Teleporting to portals

@teleport targets a named @portal instead of a CSS selector, so the server can render it too. You can define each @portal only once per page.

Checked in both places

Nitro's own tests run thousands of random methods and views in PHP and, compiled, in the browser, and treat any difference as a bug. What this page lists is what remains different by design.

Next steps