AI summary by TwinMind
Inertia 2.0: It's like Next but better (and you can use React!)
Theo - t3․gg
19m 1s
Initial Laravel Experience & Inertia Introduction
- Speaker tried Laravel few months ago, had rough DX experience as React full-stack dev
- Laravel team listened, made improvements
- Inertia = router for React apps, works with Laravel/Phoenix/Elixir
- Allows hosting React apps on non-JS backend platforms
- Previously missing features but DX/ergonomics improving
PostHog Sponsorship
- Speaker uses PostHog for analytics across all revenue-generating projects
- 90% users on generous free tier
- Usage-based pricing exclusively
- All-in-one product suite: web analytics, session replay, feature flags, experiments, surveys
Inertia 2.0 Core Concept
- Acts as bridge between server-side apps and JS frontend
- Enables SPA building while maintaining Laravel's server-side routing/ORM
- Major change: completely rewrote request handling layer
- Backend devs can use popular frontend frameworks without building APIs
How Inertia Works - Basic Flow
- PHP controller function queries ORM (users where active, ordered by name)
- Returns
inertia.render('users', data)instead of HTML/JSON - Component specified gets rendered with passed props
- Can server-render HTML or client-render
- Passes props from PHP to JavaScript without API layer
- Reduces friction compared to traditional REST API approach
Form Handling & Mutations
- React form with vanilla handle changes/submit
- On submit: posts to users route with state values
- Server store function creates user, then returns redirect to users.index
- Server determines client navigation after backend operations
- Frontend told by backend what to do after changes occur
- Eliminates need for frontend to decide post-operation navigation
Inertia 1.0 Limitations
- Too focused on backend interrupting frontend navigation
- Problematic for live chat widgets, infinite scroll
- Whole page redirects/re-renders for dynamic content
- Not viable for polling/real-time features
Inertia 2.0 New Features - Polling
usePollhook from inertia-js/react- Polls server for new information on current page
- Stops polling when page unmounted
- Optional start/finish functions
- Returns stop/start controls for manual triggering
- Accepts router reload options for selective prop revalidation
Prefetching Capabilities
- Default: prefetch on hover after 75ms
- Data cached for 30 seconds before eviction
- Customizable with
cache-forprop on links - Multiple prefetch strategies:
hover(default)click(starts on mousedown)mount(always prefetch)- Can combine strategies in array
- Programmatic prefetching:
router.prefetch() - Can prefetch data for previous/next pages
flushAll()to clear cache (useful for logout)- SWR support with freshness/validity timeouts
Deferred Props - Game Changer
- Similar to Remix's defer functionality
- Allows fast data (user info) to load immediately
- Slow data (reports/charts) loads later via streaming
- Eliminates blocking on slowest API call
- Groups props together while maintaining parallel fetching
- Server wraps slow data with
inertia.defer() - Client uses Suspense-like pattern with fallback states
- Uses string keys (no TypeScript type safety)
- Only non-TypeScript framework with defer functionality
Deferred Props Implementation
- Server:
permissions: inertia.defer(expensiveQuery()) - Client:
<div>{deferredData.permissions || <div>Loading...</div>}</div> - Fallback loading states while waiting for promise resolution
- Prevents entire page blocking on slow operations
Merging Props & Infinite Scroll
mergeoption for pagination- Arrays: appends new response to current prop value
- Objects: merges response with current prop value
- Can combine with defer for advanced patterns
- Enables infinite scroll by merging paginated results
Lazy Loading - When Visible
whenVisibleoption loads data only when component visible- Prevents expensive queries until user scrolls to section
- Configurable buffer for preloading before visibility
- Shows loading state until component becomes visible
Industry Impact Assessment
- Inertia no longer just Laravel-React bridge
- One of best ways to bridge backend/frontend gaps
- Works regardless of specific backend/frontend framework choice
- Enables better backend-frontend communication
- Reduces need for TypeScript backend in same project
- Makes traditional REST API patterns feel antiquated
