UNPKG

@rsc-kit/mcp

Version:

An MCP server over what an rsc-kit build decided: the routes, why each one is static or not, and what it costs the browser.

109 lines (84 loc) • 4.38 kB
# BAP vs Inertia > Two ways to put React in front of Laravel, and when each is the right one. Inertia and a BAP both let a Laravel application keep its routes, its classes, its auth and its models, and put React in front. They differ in where the page is made. ## The same page, both ways **Inertia** keeps the page in Laravel. A controller loads the data and hands it to a React component as props: ```php title="routes/web.php" Route::get('/orders', fn () => Inertia::render('Orders', [ 'orders' => auth()->user()->orders()->latest()->take(5)->get(), ])); ``` ```tsx title="resources/js/Pages/Orders.tsx" export default function Orders({ orders }: { orders: Order[] }) { return <ul>{orders.map((o) => <li key={o.id}>{o.number}</li>)}</ul> } ``` The browser renders it, and server-side rendering is an optional second process. React is the view layer. **A BAP**, [Backend-Answered Pages](/hosts/backend-answered-pages), puts a renderer in front. The page is a React Server Component, and it asks Laravel for what it needs: ```php title="app/Rsc/Orders.php" class Orders { public function recent(int $limit = 5): array { return auth()->user()->orders()->latest()->take($limit)->get()->all(); } } ``` ```tsx title="resources/js/app/orders/page.tsx" export default async function Orders() { const orders = await rpc('Orders.recent', 5) // typed from the PHP signature return <ul>{orders.map((o) => <li key={o.id}>{o.number}</li>)}</ul> } ``` Laravel answers that call, the session's guards and the server actions. React owns the page. ## At a glance | | Inertia | BAP | | --- | --- | --- | | Maturity | Years in production, a large community | Pre-1.0; the contract still moves | | Who decides the data | The controller, for the whole page | Each component, for itself | | Slow data | The page waits for the slowest query | Each part streams in under its own `<Suspense>` | | Pages that are the same for everyone | Rendered on every request | Stored at build time, served without PHP | | JavaScript a page ships | All of it: every page is a client component | Only the interactive parts | | Types between PHP and TypeScript | Written by hand, or generated by a separate tool | Generated from the PHP signatures on every build | | Processes in production | Laravel, plus an SSR server if you use one | Laravel and the renderer, always | | Where a request goes first | Laravel | The renderer, which forwards what it doesn't own | | What you learn | Laravel and React | Laravel, React Server Components, and where code runs | ## When to choose Inertia - You want the mature, well-trodden option and a large community to ask. - Your pages are mostly behind a login and not worth storing at build time. - Your team knows Laravel deeply and wants React only as the view. - You would rather not run a second process in production. ## When to choose a BAP - A slow part of a page shouldn't hold back the rest: dashboards, feeds, pages that read several services. - Some pages are the same for everyone, such as marketing, docs and pricing, and should be served from disk or a CDN as plain HTML. - You want the contract between PHP and TypeScript generated, so removing a field in PHP fails the typecheck where it was used. - You want to ship less JavaScript: server components send none. ## What a BAP asks of you - **Know where code runs.** Server components run in the renderer, client components in the browser, and your classes in Laravel. Most confusion in a BAP is a question about which of the three a line is in. - **Accept a younger contract.** Every adapter is checked by a [conformance suite](/hosts/your-own-backend/conformance), but it is pre-1.0, and a mismatch at the boundary is still possible. - **Run the renderer.** It is a Node or Bun process beside PHP. Deployment is [two processes](/hosts/laravel/production). ## Moving between them Nothing in Laravel is Inertia-specific except the controllers that return `Inertia::render`. A BAP reaches the same models and policies through `app/Rsc` classes, so you can move page by page. A React page wins its URL, and every URL it doesn't have is still Laravel's: ```text resources/js/app/orders/page.tsx /orders is now the BAP's routes/web.php every other route is still Inertia's ``` See [Coming from Inertia](/coming-from-inertia).