Nitro
New Nitro 2.x: browser-first components for Laravel

Write PHP.
It runs in the browser.

Nitro compiles your Laravel components, PHP classes and Blade views, to JavaScript. Methods run instantly in the browser, with no request. Mark one #[Server] when it needs the server.

Get started GitHub
$ composer require nitro/nitro

Everything an app needs, the Laravel way

Try it right here

Each example below is a Nitro component, running on this page.

Search

Browser

Filtered as you type, kept in the URL

Typing filters the list in the browser, with no request. #[Url(as: 'q')] keeps the search in the address, so a refresh or a shared link shows the same results. Try a word like "forms".

class PostSearch extends Component
{
    #[Url(as: 'q')]
    public string $search = '';

    public array $posts = [];

    public function mount(): void
    {
        $this->posts = Post::latest()->get()->toArray();
    }

    #[Computed]
    public function found(): array
    {
        return array_filter($this->posts, fn ($post) => stripos($post['title'], $this->search) !== false);
    }
}
<div>
    <input nitro:model="search" placeholder="Search posts...">

    <ul>
        @foreach ($this->found as $post)
            <li>{{ $post['title'] }}</li>
        @endforeach
    </ul>
</div>
Live running on this page
blog.test/posts
  • Hello, Nitro Sara · 3 min
  • Helpers every Laravel app needs Omar · 6 min
  • Writing forms that validate as you type Lena · 8 min
  • Uploads, previews and progress Omar · 5 min
  • Pages that change without a reload Sara · 4 min
  • When a method needs the server Lena · 7 min

Forms

Browser + server

Laravel's validation, as you know it

The rules sit on the property. The server checks them before your method runs, and the message lands in @error. nitro:loading covers the wait. Try a wrong address, then a right one.

#[Validate('required|email')]
public string $email = '';

#[Server]
public function subscribe(): void
{
    $this->validate();

    Subscriber::create(['email' => $this->email]);
}
<form nitro:submit="subscribe">
    <input nitro:model="email" type="email">
    @error('email') <p>{{ $message }}</p> @enderror

    <button nitro:loading.attr="disabled">Subscribe</button>
</form>
Live running on this page
app.test/newsletter

Uploads

Browser + server

Files, previewed at once

Bind a file input like any other. The browser shows the preview before anything is saved; the upload, the rules and the storage stay on the server.

public ?Upload $photo = null;

#[Server]
public function save(): void
{
    $this->validate(['photo' => 'required|image|max:2048']);

    $this->photo->store('photos', 'public');
}
<input type="file" nitro:model="photo">

@if ($photo)
    <img src="{{ $photo->temporaryUrl() }}">
@endif
Live running on this page
app.test/profile

Navigation

Browser

Pages without a reload

nitro:navigate swaps the page in place and keeps back and forward working. nitro:prefetch loads it when the pointer comes near, so the click is instant.

<a nitro:navigate href="/posts">Posts</a>

<a nitro:navigate nitro:prefetch href="/posts/hello-nitro">
    Hello, Nitro
</a>
Live running on this page

Browser-first

The server, only when it's needed

Where every click and keystroke goes to the server, each one waits on the network. In Nitro your methods run in the browser, so they answer at once, and the server hears from the page when a #[Server] method needs it.

How Nitro works
You Nitro Every event to the server

Type "forms" in a search box

filtered in the browser

0
5

Count to ten

a browser method

0
10

Open three tabs of a panel

state in the browser

0
3

Save the form

a #[Server] method

1
1

No magic, just a compiler

See exactly what runs where

The class and the view are yours. Open Compiled to read the JavaScript Nitro makes of them: the same component, the same rules.

Browser + server
<?php

namespace App\Nitro\Components;

use Nitro\Attributes\Locked;
use Nitro\Attributes\Server;
use Nitro\Attributes\Validate;
use Nitro\Component;

class Counter extends Component
{
    #[Validate('integer|min:0|max:1000')]
    public int $count = 0;

    #[Locked]
    public string $savedAt = '';

    public function mount(): void
    {
        $this->count = session('counter.count', 0);
        $this->savedAt = session('counter.saved_at', '');
    }

    public function increment(): void
    {
        $this->count++;
    }

    #[Server]
    public function save(): void
    {
        $this->savedAt = now()->format('H:i:s');

        // This demo keeps the count in the session. Your app would save it to the database.
        session(['counter.count' => $this->count, 'counter.saved_at' => $this->savedAt]);
    }
}
Live
Not saved yet

Build your first component in five minutes

Install the package, write a class and a view, and watch it run in the browser.