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

javascript_pack_tag handles repetition of individual chunks. Closes #39 #91

Closed
wants to merge 3 commits into from

Conversation

vtamara
Copy link
Contributor

@vtamara vtamara commented Mar 30, 2022

This one would close #39.

The idea is to record what chunks have been emitted and with what defer value.

When a chunk previously emitted is going to be emitted again, skip repeating it so long as the defer value is the same as the previous one, or raises an error if different.

But what about the order of chunks?

This way, the components in the loaded packs could be used anywhere, and it would be possible to repeat
javascript_pack_tag without actually loading the chunks repeated.

I guess there are situations I'm not considering.

I also think that before merging this, it is essential to fix #88 (we don't want to confuse what is causing that bug with this!)

@vtamara vtamara changed the title javascript_pack_tag handles repetition of individual chunks javascript_pack_tag handles repetition of individual chunks. Closes #39 Mar 30, 2022
@vtamara
Copy link
Contributor Author

vtamara commented Mar 30, 2022

Rubocop tests passed locally (I wonder why not in the CI).

@tomdracz
Copy link
Collaborator

tomdracz commented Mar 30, 2022

@vtamara Thanks for the PR, from a quick glance, I think it might hit similar problem that I had when trying to make this work with repeated declarations.

Essentially what we're lacking is the context from where javascript_pack_tag has been called. Let's say that you have one declaration in the main layout loaded in the head, other loaded somewhere in the partial for specific part of the page only.

Once the page gets processed, the partial one would be processed first and emit script tags wherever it was declared and we would end up with no repetition in the head. Some scripts will however complain about not being declared in the head tag.

This might cause some fubars with Turbo also but that's just a suspicion 🤔

@vtamara
Copy link
Contributor Author

vtamara commented Mar 30, 2022

@vtamara Thanks for the PR, from a quick glance, I think it might hit similar problem that I had when trying to make this work with repeated declarations.

Essentially what we're lacking is the context from where javascript_pack_tag has been called. Let's say that you have one declaration in the main layout loaded in the head, other loaded somewhere in the partial for specific part of the page only.

Once the page gets processed, the partial one would be processed first and emit script tags wherever it was declared and we would end up with no repetition in the head. Some scripts will however complain about not being declared in the head tag.

This might cause some fubars with Turbo also but that's just a suspicion thinking

Thank you.

I guess it is good idea to enhance the example with:

  1. Partials
  2. Turbolinks

And to see what it does and how to deal.

…ent once those of application and layout and after the queue
end

# Determine if we are in a partial template or in the main one
partial = caller.filter{|c| c.include?('partial_renderer')}.count > 0
Copy link
Collaborator

Choose a reason for hiding this comment

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

@vtamara i think we're getting closer but don't quite like this bit. I'll try and figure out if there are some better ways to do it.

Alternatively, it's more overhead and bit more difficult for users to use but if we split the implementation here into a helper that populates a queue and then javascript_pack_tag is still only callable once (and uses the queue), I think we will have an implementation that solves the main problems we're facing with this.

Copy link
Member

Choose a reason for hiding this comment

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

This has issues on several levels:

  1. What if the method name filtered for changes?
  2. Performance?
  3. Completely unconventional to have production code using the call stack to figure something out.

#
# <%= javascript_pack_tag 'calendar' %>
# <%= javascript_pack_tag 'map' %>
# There should be at least one javascript_pack_tag in the main template
Copy link
Member

Choose a reason for hiding this comment

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

main template, main one ==> main view

partials, can be used on any views, including:

  1. Other partials
  2. Main view for the controller action
  3. Layout for the controller action

Order:

  1. Main View => includes partials, content_for blocks
  2. Layout => will use the main view and any content_for blocks, and any partials directly on the layout


options[:defer] = defer
if !defined?(@emitted)
@emitted = {}
Copy link
Member

Choose a reason for hiding this comment

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

Why not

@emitted ||= {}

@justin808
Copy link
Member

Closing this. Too fragile.

I gave @vtamara some ideas on how we can remove duplicates from the final result HTML string after rendering the whole layout and before the server sends the result to the browser.

@justin808 justin808 closed this Apr 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants