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 support for always and except #152

Merged
merged 8 commits into from
Nov 8, 2024

Conversation

skryukov
Copy link
Contributor

@skryukov skryukov commented Nov 2, 2024

This PR "upstreams" some features from #132 which are related to Inertia.js 1.x, it includes:

  • New InertiaRails.always prop from inertia.js 1.3: https://inertiajs.com/partial-reloads#lazy-data-evaluation
  • Support for the except option from inertia.js 1.3: https://inertiajs.com/partial-reloads#except-certain-props
  • Lazy props bug fix: this PR also fixes InertiaRails.lazy(false) and InertiaRails.lazy(nil) raising errors
  • Simplification for lazy props: we don't wrap simple values into procs anymore
  • Performance fix for merging props: we don't symbolyze keys deeply anymore when deep merge config is turned off
  • request.inertia_partial? update: with introduction of the X-Inertia-Partial-Except header, X-Inertia-Partial-Data is now can be omitted when router.visit(path, {except: 'foo'}) is called.

PR is intended to be reviewed commit by commit.

@bknoles bknoles mentioned this pull request Nov 4, 2024
4 tasks
def drop_partial_except_keys(hash)
partial_except_keys.each do |key|
parts = key.to_s.split('.').map(&:to_sym)
*initial_keys, last_key = parts
Copy link
Collaborator

@bknoles bknoles Nov 5, 2024

Choose a reason for hiding this comment

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

this supports dot notation, yea? so like except: ['users.some_association.expensive_thing_to_compute']? [edit: I see the dot notation in the specs! question below remains]

do the other adapters support that? I don't see it documented anywhere.

either way, if we support it for :except, should we also support it for :only? (genuine question)

Copy link
Contributor Author

@skryukov skryukov Nov 5, 2024

Choose a reason for hiding this comment

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

do the other adapters support that? I don't see it documented anywhere.

yup, Laravel adapter supports that

if we support it for :except, should we also support it for :only?

Laravel adapter does not 😄 To be honest the whole except + only thing is poorly documented, we probably should help the core team make it happen.

Copy link
Contributor Author

@skryukov skryukov Nov 5, 2024

Choose a reason for hiding this comment

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

Actually they reverted back dot notation for only: inertiajs/inertia-laravel#641

While this would be a nice feature to eventually have, this particular implementation introduced some breaking changes.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh nice find!

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok, i dug into this a bit. TL;DR: I don't think those issues will affect our implementation.

Best I can tell (caveat: using my brain's PHP interpreter) is that the original implementation of Laravel's share shallow merged all data. However, dot notation allowed you to deep merge dot-notated shared data. Not sure whether that was intentional or a happy accident since I don't think the feature was ever documented. When dot notation was added to only and except, it changed the execution order and changed the behavior such that all shared data was a shallow merge. So, people relying on the deep merge behavior had breaking changes. (There was another reported but with lazy props that I didn't fully diagnose... looks to be something related to array manipulation quirks in PHP).

InertiaRails 1) has explicit patterns for deep merging shared data and 2) doesn't support dot notation in shared data anyway, so we should be totally fine.

I don't see a reason why we couldn't support dot notation for only as well. Do you think it's worth adding? Seems like it'd be nice if some prop had multiple expensive child props and you only wanted to grab one of them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, let's try adding support for ’only’ in a separate PR 👍


module InertiaRails
# Base class for all props.
class BaseProp
Copy link
Collaborator

Choose a reason for hiding this comment

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

I like this. Half-baked thought: I wonder if we could move more rendering logic into the Prop classes. The computed_props method is doing more and more things in a sort of procedural style. Maybe it could iterate over the props one time and each instance would know what to do with itself.

(Not suggesting changes, more just thinking out loud)

@bknoles bknoles merged commit e9b2b9f into inertiajs:master Nov 8, 2024
13 checks passed
@skryukov skryukov deleted the refactor-always-prop branch November 8, 2024 05:40
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.

2 participants