Getting started
Installation
Nitro is a Composer package for Laravel 13. Install it, give your app a layout with two directives, and check it works with a page of a few lines.
On this page
Installing Nitro
-
Require the package
Install Nitro with Composer, in a Laravel 13 application:
Terminalcomposer require nitro/nitroLaravel finds Nitro by itself: there is no service provider to register and no configuration file to publish. Nitro's endpoint,
/_nitro, is ready. -
Create your layout
A fresh Laravel app has no layout yet, so create this one. Pages render inside it unless they name another (see Choosing a layout). If your app already has a layout, from a starter kit for example, add the two highlighted lines to it instead.
resources/views/components/layouts/app.blade.php<!DOCTYPE html> <html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> @nitroHead </head> <body> {{ $slot }} @nitroScripts </body> </html>@nitroHead, in the<head>, prints the page's<title>and the tags search engines and link previews read (see The page's head).@nitroScripts, just before</body>, loads Nitro in the browser with each component's state. It is what makes the page come alive.
Nothing happens when you click?
Without
@nitroScripts, the page still shows, because the server rendered it, but nothing in it responds and no error appears. Check that the layout has it, and that the page uses that layout. -
Check that it works
Make a page, its class and its view:
Terminalphp artisan make:nitro Hello --pageGive the class a property and a method, the view a button, and the page a route:
app/Nitro/Pages/Hello.php<?php namespace App\Nitro\Pages; use Nitro\Attributes\Title; use Nitro\Component; #[Title('Hello')] class Hello extends Component { public int $count = 0; public function increment(): void { $this->count++; } }resources/views/nitro/pages/hello.blade.php<div> <button nitro:click="increment">Clicked {{ $count }} times</button> </div>routes/web.phpuse App\Nitro\Pages\Hello; Route::nitro('/hello', Hello::class);Open
/helloand click the button. The number goes up without a reload, and the browser's Network tab shows no request:increment()ran in the browser, compiled from your PHP. The first visit after you change a component takes a moment longer, while Nitro compiles it. -
Install esbuild, for production
In development, Nitro needs nothing more: it serves the code it compiles unminified. For production,
php artisan nitro:buildminifies it with esbuild, which runs on Node. Install it in your app:Terminalnpm install --save-dev esbuildWithout Node or esbuild,
nitro:buildstops with a message that says which one is missing. If Node isn't on yourPATH, setNITRO_NODEin.envto its binary. -
Publish the configuration (optional)
Nitro works without a configuration file. When you want to change a default, publish
config/nitro.php, and keep only what you change:Terminalphp artisan vendor:publish --tag=nitro-config
Where your files go
Nitro has two kinds of classes. Components are parts of a page that you place in a view. Pages are whole screens, with their own route, layout and title.
| Components | Pages | |
|---|---|---|
| Class |
app/Nitro/Components
|
app/Nitro/Pages
|
| View |
resources/views/nitro/components
|
resources/views/nitro/pages
|
| Name |
posts.table
for
Posts\Table
|
pages::posts.index
for
Posts\Index
|
| Used |
placed in a view:
<nitro:posts.table />
|
routed:
Route::nitro()
|
| Layout and title | no |
#[Layout]
,
#[Title]
|
You can change these directories, namespaces and view paths in config/nitro.php.
Preparing for production
During development, Nitro compiles a component on the first request after you change it. When you
deploy, build everything ahead of time instead. The build compiles every component, minifies it, and
publishes the files to public/nitro.
php artisan nitro:build
Run it on every deploy
php artisan optimize doesn't run nitro:build, so add it to your deploy script. Also add /public/nitro to .gitignore.
Using Nitro next to Livewire
Nitro and Livewire can run in the same application, and even on the same page. Nitro's tags,
attributes, scripts and endpoints all use the nitro: prefix, so they never clash. This
means you can move to Nitro one component at a time.