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

Vary Header should not be controlled by the client #138

Merged
merged 1 commit into from
Oct 25, 2024

Conversation

skryukov
Copy link
Contributor

The Vary header is a way for servers to control caching mechanisms, and it is purely a server-side mechanism. The client does not control or affect the Vary header. Issue #136 introduced logic that does not conform to this: https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-semantics-19#section-12.5.5

There are several approaches we can take with the Vary header:

  1. Apply the original Laravel plugin logic: force the Vary: X-Inertia header.
  2. Set the Vary header if it wasn't set before, which is the default logic used in Rails: https://github.com/rails/rails/blob/b8720e7cb8c9894d142b8576c21065c92cd1907a/actionpack/lib/action_controller/metal/rendering.rb#L220-L224
  3. Append X-Inertia if the Vary header was set previously, or set the Vary: X-Inertia header if it was not.

I believe the third option makes the most sense: it allows users to add other headers, like Accept-Language, to the Vary list, but it also prevents them from omitting X-Inertia.

@@ -16,7 +16,11 @@ def initialize(component, controller, request, response, render_method, props: n
end

def render
@response.set_header('Vary', [@request.headers['Vary'], 'X-Inertia'].compact.join(', '))
if @response.headers["Vary"].blank?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, I prefer the original functional approach, but it generates unnecessary objects, which can affect production performance due to additional GC load.

Copy link
Collaborator

@BrandonShar BrandonShar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @skryukov !

To make sure I understand, is this PR purely a performance optimization? It looks to me like the specs you added will pass with the existing logic, right?

@skryukov
Copy link
Contributor Author

@BrandonShar Not quite - previously we passed the value from the request's Vary header, which seems unusual since I don't see this behavior in either the Laravel plugin or the HTTP caching RFC 🤔

Copy link
Collaborator

@BrandonShar BrandonShar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read over this change like 3 times before I left that comment and still missed that it said @request in the original line, not @response 😆. Thanks for calling that out!

@BrandonShar BrandonShar merged commit 5afa24e into inertiajs:master Oct 25, 2024
9 checks passed
@skryukov skryukov deleted the fix-vary-header branch October 25, 2024 21:33
@bknoles
Copy link
Collaborator

bknoles commented Oct 27, 2024

Released in 3.3.0!

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

Successfully merging this pull request may close these issues.

3 participants