Skip to content
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

Add Inertia::shareOnce() #37

Closed
gzhihao opened this issue Jun 13, 2019 · 5 comments
Closed

Add Inertia::shareOnce() #37

gzhihao opened this issue Jun 13, 2019 · 5 comments

Comments

@gzhihao
Copy link

gzhihao commented Jun 13, 2019

If I'm not mistaken, Inertia::share() adds share data to each response. However, in most of the case we only need to fetch the shared data once, for example, auth user usually won't change.

I was wondering if we can add an shareOnce method to avoid repeatedly sending share data. This implies that server need to store a state telling if the data is shared already. To avoid using session/cookie, we might need to pass some meta data in request/response. Here is a raw idea in the context of vue

  • To start with, server returns share data also indicates which data is shared
  • Client set the share data (in this.$page)
  • Inertial-link will check if the share data is already in the $page object, if yes add those share data keys into inertia request header indicating they are loaded already
  • Server detects there are share data keys in the request, ignore those when adding data in the response

It adds complexity, but it can reduce lots of unnecessary payloads. I'd love to do some experiment and create a PR if this feature request is accepted. pls let me know your thoughts.

@georgehanson
Copy link
Contributor

I can see why this might be useful.

One thing to consider would be how to "re-share" information that you have shared with the proposed shareOnce method.

For example, if you are sharing user details the once and they updated their username, email, name etc you would want that to then be re-shared. So in that instance the implementation would need to overrule the request header saying that the information is already on the client side.

@gzhihao
Copy link
Author

gzhihao commented Jun 13, 2019

yep, re-share is a bit tricky, not sure if there is an elegant solution.

@georgehanson
Copy link
Contributor

It sounds more like something that would be on the client side. Perhaps on the client side you could do something like $inertia.clear('auth') which will remove it from the request header to the backend. So from the servers point of view it would return the data.

@gzhihao
Copy link
Author

gzhihao commented Jun 14, 2019

@reinink would like to know your thoughts on it.

@reinink
Copy link
Member

reinink commented Aug 8, 2019

Hey folks! My gut reaction is that this feels like too much complexity for what you gain in performance. Passing the currently authenticated user on each request is adding very little overhead, and typically this shared data is pretty minimal.

For most apps it will include:

  1. The current user
  2. Flash data (only when they exist)
  3. Errors (only when they exist)

I like to think of Inertia like a server-side framework. With a classic server-side framework, literally each page load you send the entire site template on each request, which includes the currently authenticated user (ie. in the header). And, that's a totally accepted way of doing things. So passing a little extra JSON in each Inertia request seems fine too. In fact, it's obviously way better than a classic server-side rendered app, since the entire template isn't being resent each time.

This is something we can always revisit in the future. 💪

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants