X Tutup
The Wayback Machine - https://web.archive.org/web/20220710021845/https://github.com/github/view_component/pull/980
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 鈥淪ign up for GitHub鈥, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fragment Caching POC #980

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft

Conversation

Matt-Yorkley
Copy link
Contributor

@Matt-Yorkley Matt-Yorkley commented Jun 24, 2021

Summary

This is a rough draft POC to explore adding compatibility for Fragment Caching in ViewComponent.

It makes some modifications to ActionView::Digestor and ActionView::ERBTracker in order to get digests working correctly with components, which means (for example) if you cache a rails view that renders a component inside it and the contents of the component class file change, the digest used in the cache key will be updated and the cached fragment will be correctly invalidated.

Initial notes in issue #476 (comment)

Current state:

  • It more or less works 馃コ (see point two)
  • It needs a lot of tests
  • It was thrown together with the aim of getting quick results, but the way these ActionView classes are being overridden probably needs a rethink and iterating on before it'll start to look like something acceptable 馃槄
  • Currently works with ERB, but other template engines register dependency trackers in different ways, for example here in the haml-rails gem

@lfalcao
Copy link
Contributor

@lfalcao lfalcao commented Jun 24, 2021

ops. 馃憤

@Matt-Yorkley Matt-Yorkley force-pushed the fragment-caching branch 3 times, most recently from 672d38b to 63a08f8 Compare Jun 25, 2021
@jonspalmer
Copy link
Collaborator

@jonspalmer jonspalmer commented Jun 26, 2021

This feature set is super exciting. Curious about the monkey patch - Are these changes that could reasonably be a part of Rails itself? Could we eliminate the monkey patch entirely and make this just about registering trackers?

@Matt-Yorkley
Copy link
Contributor Author

@Matt-Yorkley Matt-Yorkley commented Jun 26, 2021

Are these changes that could reasonably be a part of Rails itself?

Joel said the exact same thing 馃槃 It needs some thought/discussion.

Could we eliminate the monkey patch entirely and make this just about registering trackers?

I think it won't work without some changes to ActionView::Digestor, but I'm open to any ideas 馃憤

@Matt-Yorkley
Copy link
Contributor Author

@Matt-Yorkley Matt-Yorkley commented Jun 26, 2021

I started working on this in a Rails 6.0 app, but there's a couple of things that are slightly broken in 6.1+. I'll push some fixes shortly and add some initial tests.

@Matt-Yorkley
Copy link
Contributor Author

@Matt-Yorkley Matt-Yorkley commented Jun 27, 2021

If anyone's interested what the Digestor's dependency tree object actually looks like, I've made a Gist here with some sample debugging output.

Some of the changes around the introduction of ActionView::PathParser and ActionView::Template::Types in 6.1 meant component files were more difficult to find here...
Fixes static dependency tracking for 6.1+, where some minor breaking changes to the arguments of #add_static_dependency were introduced. Should be backwards-compatible with =< 6.0.
Matt-Yorkley added 7 commits Jul 1, 2021
The MatchData object returned by Regexp#last_match throws an error if an undefined group is requested. In the case where layout dependencies are passed to #add_dependencies, the :component match group will not exist. MatchData#named_captures returns a simpler Hash which returns nil for undefined groups instead of raising.
The engine fails to use the correct paths in the test environment without this, and looks in the root of the repo instead of the dummy app.
There were several breaking changes to digests between Rails 5.2 and 6.0 which are not compatible
@Matt-Yorkley
Copy link
Contributor Author

@Matt-Yorkley Matt-Yorkley commented Jul 1, 2021

I had a go at reformulating this as a PR on ActionView and it looks a lot better. Preview here.

@dkniffin
Copy link
Contributor

@dkniffin dkniffin commented Mar 8, 2022

@Matt-Yorkley Any progress on this? Are you planning on making a PR to ActionView for that change?

@Matt-Yorkley
Copy link
Contributor Author

@Matt-Yorkley Matt-Yorkley commented Mar 8, 2022

Any progress on this? Are you planning on making a PR to ActionView for that change?

Yes and no. I did make that PR, but it was rejected: rails/rails#42850

If anyone else wants to take up the baton on this one, be my guest.

@JWShuff
Copy link
Contributor

@JWShuff JWShuff commented Jun 16, 2022

We took a really similar approach to this in setting up a VC fragment caching gem which we are evaluating in our code base. I was let down to see the hard no on extending how rails template lookups work, but I think this approach can work without causing too much unintended fun.

The gem we're doing this work on lives here, and the current PR is what we are seeing some success with in our code at G2.

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.

None yet

5 participants
X Tutup