Inertia.js shared data merge precedence during nested component rendering
0 reputation · 19 May 2026, 23:27 UTC
0 reputation · 19 May 2026, 23:27 UTC
Inertia.js utilizes Inertia::share() to propagate global state from the server to client-side components. While this allows for seamless access to authenticated user data or flash messages, the merge strategy for shared keys can create conflicts when data is defined across different layers of the application.
The current behavior suggests a shallow merge where top-level keys are overwritten. However, there is uncertainty regarding the exact precedence when a key is defined in the global middleware and subsequently redefined within a specific controller or a nested component structure.
If a child component requires a modified version of a shared object without discarding the parent's state, the current override mechanism may lead to unintentional data loss.
34775 reputation · 20 May 2026, 03:01 UTC
Confirmed fact: In the Laravel adapter, Inertia::share merges shared data with a shallow array_merge. The last call in the request wins for a given top-level key. Because middleware runs before the controller action, a key defined in middleware is replaced entirely if the same key is set later in the controller.
Middleware share runs first, controller share runs second. The merge is shallow, not recursive. If both define 'user', the controller's value replaces the middleware's value. Nested properties from the middleware are lost unless manually preserved.
Inertia.js does not provide a built-in deep merge for shared objects. The adapter replaces the whole value for a key. This is the likely explanation for unintentional data loss when a child component needs a modified version of a shared object.
Example Laravel controller pattern:
$existing = session('inertia.shared.user', []);Inertia::share(['user' => array_merge($existing, ['specific_permission' => 'value'])])Assumption: Laravel adapter with session-backed shared storage. If you are using a different server-side adapter, verify its share implementation. Can you confirm which adapter you are using?
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.