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

Unoptimized partial reload warnings #165

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

bknoles
Copy link
Collaborator

@bknoles bknoles commented Nov 26, 2024

I revisited this after:

  1. Refactoring the renderer a bit in Support dot notation for :only keys in partial reloads #163
  2. Perusing the Rails source for the behavior on unpermitted parameters (which is a analogous to this feature IMO)

The refactored renderer was much easier to add this feature to, and I liked Rails' use of ActiveSupport::Notifications for logging/raising errors. So here we are!

TODO

  • Add documentation

@bknoles bknoles mentioned this pull request Nov 26, 2024
4 tasks
@@ -12,5 +12,20 @@ class Engine < ::Rails::Engine
include ::InertiaRails::Controller
end
end

initializer "inertia_rails.subscribe_to_notifications" do
if Rails.env.development? || Rails.env.test?
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I want the default experience to add warnings to the logs in development / test only.

Comment on lines +30 to +37
set_vary_header

validate_partial_reload_optimization if rendering_partial_component?

return render_inertia_response if @request.headers['X-Inertia']
return render_ssr if configuration.ssr_enabled rescue nil

render_full_page
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Moved these into methods purely for readability. IMO this method should be super readable since it spells out the core functionality

if @deep_merge
shared_data.deep_symbolize_keys.deep_merge!(props.deep_symbolize_keys)
def merged_props
@merged_props ||= if @deep_merge
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

memoizing just to future proof. it's only called once, but can't hurt

@@ -166,5 +146,72 @@ def excluded_by_only_partial_keys?(path_with_prefixes)
def excluded_by_except_partial_keys?(path_with_prefixes)
partial_except_keys.present? && (path_with_prefixes & partial_except_keys).any?
end

def validate_partial_reload_optimization
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

don't love the long method name... i'm open to suggestions!

Comment on lines +156 to +158
paths: prop_transformer.unoptimized_prop_paths,
controller: controller,
action: controller.action_name,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@skryukov you mentioned wanting this feature to accept a callable... with this anyone can register a subscriber with that sort of functionality. any other options you'd like to see passed to the subscriber context?

end
end

class PropTransformer
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I moved the transformation logic into a nested subclass so 1) all the behavior is encapsulated in a single place and 2) I had a place to store unoptimized_prop_paths while traversing the props hash. The transformation code is the same, and it was super easy to drop in the track_unoptimized_prop call.

The caller (above) uses a .select_transformed method with a block, so the caller only has to worry about filtering now (instead of both filtering and transforming). We also get to remove the admittedly-clunky keep/dont keep enum!

I considered pulling this out of the renderer class, but I don't really see a need for it. It's tested by our request specs and never going to be used outside that context.

@@ -153,11 +153,60 @@
end
end
end

describe '.action_on_unoptimized_partial_reload' do
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Much cleaner specs than in #154 :)

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.

1 participant