Using a script tag instead of data attribute to pass initial data. #1389
Replies: 3 comments
-
Hey thanks for this suggestion, but I think we're going to just keep this the way it is for now. What you're suggesting is actually how Inertia used to work, but we switched it to the attribute approach. I wish I could remember off hand why we did that, but I don't recall at this moment. |
Beta Was this translation helpful? Give feedback.
-
You can provide Inertia page object when call So the HTML would look like this : <div id="app"></div>
<script id="__INERTIAJS__" type="application/json">{"component":"Home","props":{},"url":"/","version":null}</script> and in client-side : createInertiaApp({
page: JSON.parse(document.getElementById('__INERTIAJS__')!.innerHTML),
...otherOptions,
}); Hope this helps. |
Beta Was this translation helpful? Give feedback.
-
With gzipping enabled in your server, this doesn't make a difference. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I think it could be worth to change a default mechanism of passing initial data to inertia. For now page data is sent as
data-page
attribute to app element, and since it's JSON placed within HTML attribute it requires all"
characters to be encoded into entities which radically increases the amount of data that must be sent in response.Just for example know you have something like this sent in the response (notice all
"
entities):JSON length is 179 characters.
Instead this mechanism could be replaced with something like this:
Here JSON length is 98 characters.
Above ones didn't contain any extra props sent, so let's send a standard Laravel's user data in props:
Now JSON length is 497 characters, where using the script tag:
The length is 296 characters.
As you can see a difference is quite significant between the two, and it's just for one model sent in props. For larger projects with big amount of data being sent from backend this could be a nice improvement, especially on slow networks.
Let me know what you think and if it's worth making a PR for it? 😉
Beta Was this translation helpful? Give feedback.
All reactions