-
Notifications
You must be signed in to change notification settings - Fork 263
[2.x] Add initial Shared Prop Support for Initial Page Load #760
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 2.x
Are you sure you want to change the base?
Conversation
d60872e
to
d93dba7
Compare
d93dba7
to
04d84a3
Compare
Interesting idea! Can't we just introduce an |
8d1b576
to
bfae861
Compare
Thanks — I really like that idea as well. I agree that I’ve also updated the code to integrate |
…method for clarity
What do you have in mind for the frontend? I've played around with it, and is your idea that you're using this in the createInertiaApp({
setup({ el, App, props, plugin }) {
const { translations } = props.initialPage.initialProps // <=
createApp({ render: () => h(App, props) })
.use(plugin)
.mount(el)
},
}) |
Thanks for sharing your thoughts! For the frontend, I'm indeed working within the createInertiaApp({
title: (title, initialProps) => `${title} - ${initialProps.appName || 'MyApp'}`,
setup({ el, App, props, plugin, initialProps }) {
const { appName, translations } = initialProps;
console.log('App Name:', appName); // For debugging
createApp({ render: () => h(App, props) })
.use(plugin)
.mount(el);
},
}); A few notes on the setup:
Note There’s no need to include Let me know if you have specific plans for frontend logic! |
While working with Inertia.js, I discovered that we can define the public function initialPropsResolver(): array
{
return function (Request $request) {
return [
'locale' => 'en',
'appName' => 'Test App',
'translations' => [
'en' => 'English',
'es' => 'Spanish',
],
];
};
} Previous approach using public function share(Request $request): array
{
return [
...parent::share($request),
'user' => auth()->user(),
'locale' => Inertia::initial(fn() => 'en'),
'appName' => Inertia::initial(fn() => 'Test App'),
'translations' => Inertia::initial(fn() => [
'en' => 'English',
'es' => 'Spanish',
]),
];
} Note In the old approach, developers might unintentionally misuse class UserController
{
public function __invoke(): void
{
Inertia::share(['initial' => Inertia::initial(fn() => true)]);
return inertia('Users/Index', [
'users' => User::all(),
]);
}
} This can lead to unexpected behavior where the shared Would it be more reliable to centralize these values using |
This PR adds support for an
initial
prop option in Inertia.js shared props. It allows developers to define props that should be sent only with the initial page load and excluded from subsequent Inertia responses.This enhancement is particularly useful for values like configuration settings, application metadata, or other static data that doesn't need to be included with every request — reducing payload size and improving response efficiency.
Example:
In this example, the defined props (
locale
,appName
,translations
) will be shared only on the first Inertia response.Why this matters:
As discussed in Inertia.js Discussion #993, there's a recurring need to send certain props — such as app settings, tokens, or translation strings — only with the initial page load, during the app's initial load. This feature provides a clean, built-in solution for that use case.
Note
If this PR proves useful and is approved for integration, I'd be happy to work on adding the corresponding support in the Inertia.js client-side adapter as well.