-
-
Notifications
You must be signed in to change notification settings - Fork 40
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
Fragment caching within component not working #105
Comments
Hey @davidalejandroaguilar, unfortunately Phlex doesn't support fragment caching, but it's fast enough that you shouldn't need it. Instead, you can use "low-level caching" to cache your database queries and then render the views fresh. |
Thanks for responding @joeldrapper, I'm mostly interested in avoiding db queries, rather than view rendering. Usually, there are many database calls being done within a view. e.g. if you render a Are you suggesting to wrap every single one of those db calls within a def any_payments?
Rails.cache.fetch(order) do
order.payments.any?
end
end That might be a bit cumbersome, but doable. Also, as noted in the issues I linked, seems like fragment caching was supported and was removed since "compiled components" would make them unnecessary. Why was that? Just curious. |
@joeldrapper Or do you think we should rather always wrap a Phlex component inside a Rails partial if fragment caching is desired, once #106 is fixed? e.g. show.html.erb <%= cache [some, objects] do %>
<%= render "my_component_wrapper" %>
<% end %> my_component_wrapper.html.erb <%= render MyComponent.new(some:, objects:) %> my_component.rb class MyComponent < ApplicationComponent
def initialize(some:, objects:)
# ...
end
def template
# ... use objects
end
end |
Personally, I don’t think the cache is worth it. I would write all my queries in the controller, using strict loading and load async and wouldn't use a cache at all. As long as you don't have N+1s (which you can't do if you use strict loading), the database should be fast enough. The view layer is definitely fast enough using Phlex, and you can render parts of the view while querying the database concurrently if you use |
Expected result:
The component is fragment cached.
Actual result:
How to reproduce:
Notes
Cache
Phlex helper for Rails.The text was updated successfully, but these errors were encountered: