Upgrade guide
From 1.x to 2.0
2.0 is a rewrite that implements the Inertia.js v3 protocol. Upgrade your client adapter at the same time (@inertiajs/react, @inertiajs/vue3 or @inertiajs/svelte ^3.0); v1 and v2 clients are not supported.
Requirements
- Yii
~2.0.55(all earlier releases are affected by published security advisories). - PHP 8.1+ (unchanged).
Root view
The page data is no longer a data-page attribute. Replace the root element in views/layouts/inertia.php:
- <div id="app" data-page="<?= htmlspecialchars(json_encode($page), ENT_QUOTES, 'UTF-8') ?>"></div>
+ <?= Inertia::app($page, $ssr) ?>Replace hard-coded asset URLs (which never matched Vite's hashed file names) with the Vite helper:
- <script type="module" crossorigin src="/dist/assets/index.js"></script>
- <link rel="stylesheet" crossorigin href="/dist/assets/index.css">
+ <?= Inertia::vite()->tags('src/main.jsx') ?>See stubs/inertia.php for a complete root view.
Configuration
config/inertia.phpwas removed (it was never loaded). Configure theinertiaapplication component instead:php'components' => [ 'inertia' => [ 'class' => \Crenspire\Yii2Inertia\Manager::class, 'rootView' => '@app/views/layouts/inertia.php', 'shared' => [...], ], ],The component bootstraps itself. Asset version checks,
Varyheaders, redirect handling and CSRF support now run for every request instead of insideInertia::render().Crenspire\Yii2Inertia\ViewRendererwas removed. It could not work (the class did not compile, and Yii selects renderers by file extension). UseInertia::render()in controllers.Crenspire\Yii2Inertia\InertiaResponsewas removed; useInertia::render()/Inertia::location().
API changes
| 1.x | 2.0 |
|---|---|
Inertia::version() (getter) | Inertia::getVersion() — always returns a string |
Inertia::version($v) | unchanged; Inertia::version(null) now restores the default |
Crenspire\Yii2Inertia\inertia() | global inertia() function |
Default version: filemtime(@webroot/dist/manifest.json) or '1' | md5 of @webroot/dist/.vite/manifest.json (or manifest.json), or '' |
Behaviour changes
- Page URL: the query string is no longer duplicated (
/users?page=2?page=2). - JSON responses:
Inertia::render()sets$response->datato the encoded JSON string (formatraw) instead of an array. Tests shouldjson_decode()it. Props are encoded identically on the first visit and on Inertia visits. - Partial reloads only apply when
X-Inertia-Partial-Componentmatches the rendered component, and they filter shared props too. UseInertia::always()for shared props that must always be sent.X-Inertia-Partial-Exceptand dot-notation paths are supported. - Closures in props are evaluated (previously they were encoded as
{}), and only when the prop is sent. - Version mismatches return
409only forGETrequests, with an absoluteX-Inertia-Location. Requests without anX-Inertia-Versionheader are treated as outdated, as the protocol requires: functional tests that sendX-Inertia: truemust also sendX-Inertia-Version: <Inertia::getVersion()>. Inertia::location()accepts routes (['site/index']) and no longer addsX-Inertia-Locationto non-Inertia redirects.- Invalid UTF-8 in props throws a
JsonExceptioninstead of rendering an empty page. Vary: X-Inertiais sent instead ofVary: Accept.
New features
Validation errors (withErrors()), flash data, error bags, deferred / optional / once / merge / infinite scroll props, history encryption, fragment redirects, CSRF cookie support, JSON request bodies, Vite integration and SSR. See the documentation.