-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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 a preload_pack_asset helper #2124
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR, I have a few concerns. Is there a reason why preload
is preferred to prefetch
or preconnect
? I am under the impression that preload
is for priority loading things like fonts & workers that will be required on the user's current page. The rails docs also uses a web worker as an example.
Depending on where javascript_packs_with_chunks_preload_tag
is put it can negatively effect browser scheduling. This entirely negates the usefulness of lazy loading and progressive web enhancement.
I also worry that the user will assume that their scripts will be run.
This is what I would change:
- support preload and prefetch
- convey poor preload adoption https://caniuse.com/#search=preload
- focus on preload specific files like fonts or images instead every chunk in a pack
- convey how webpack has it's own methods of preload and prefetching: https://medium.com/webpack/link-rel-prefetch-preload-in-webpack-51a52358f84c
Thank you for the feedback ! I was not aware that rails provides the In the other hand, I don't know we can preload only images and fonts with Webpacker, if you have any ideas ? I read your comment in the related issue and I got your point. Maybe we could add a helper just for fonts and images, and let Webpacker handle only JS ? |
I'll let @gauravtiwari chime in on this. I am not sure about that, I don't want to break things for some users.
Agreed. I think the best course of action is to create a <%= preload_pack_asset 'fa-regular-400.woff2' %>
<%# turns into something like %>
<link href="/packs/fa-regular-400.d8O6LxGN.woff2" rel="preload" as="font" type="font/woff2" crossorigin> What do you think? |
@guillaumebriday We may be able to get away with something like this: #2125 (comment) |
@jakeNiemiec I ended up doing a very basic wrapper for I think we should insist in the documentation on what you said in this comment #1874 (comment) What do you think ? |
Hey, I added a condition to check if Rails version is greater than 5.2.x. The tests are now green, what do you think ? Thank you ! |
I would need to defer to @gauravtiwari, I don't have too much experience with Rails versions inter-op. I would raise an error/warning when that if statement is false though. |
Thanks @guillaumebriday @jakeNiemiec How about we define a link tag if one isn't available? So pre-5.2 we define a link tag by hand and set the preload option otherwise use the one provided by 5.2+ Something like this: if self.class.method_defined?(:preload_link_tag)
preload_link_tag(current_webpacker_instance.manifest.lookup!(name), options)
else
# define_tag
# https://github.com/rails/rails/blob/b9ca94caea2ca6a6cc09abaffaad67b447134079/actionview/lib/action_view/helpers/asset_tag_helper.rb#L268
end |
Thank you @gauravtiwari for the idea !
However, it's not that easy if it's not defined and I'm facing multiple issues with < 5.2 versions. For example: The method At this point I think the best solution is to made it available only for Rails > 5.2 with a clear warning in the documentation. Why do you think @gauravtiwari @jakeNiemiec ? Thank you |
Thanks @guillaumebriday for your efforts, really appreciate it. What do you think of failing in the else clause so a user knows that tag isn't supported. Something like, "you need Will also clarify this in changelog. |
@gauravtiwari Yes I think it's a pretty reasonable compromise ! I pushed it if you want to check again. Thanks |
Thanks @guillaumebriday 🍰 |
Thanks @gauravtiwari @jakeNiemiec for the feedbacks ! |
Cf: #2125 and #2125 (comment)