Add vite_preload_assets template tag for performance optimization#169
Open
petermorrowdev wants to merge 2 commits into
Open
Add vite_preload_assets template tag for performance optimization#169petermorrowdev wants to merge 2 commits into
vite_preload_assets template tag for performance optimization#169petermorrowdev wants to merge 2 commits into
Conversation
Add new Django template tag to generate <link rel="preload"> tags for
Vite-bundled assets, enabling early loading of critical resources like
fonts and images to improve page load performance.
- Add `assets` field to ManifestEntry to support asset references in Vite manifest
- Implement `generate_preload_assets_tags` method in `DjangoViteAppClient`
- Add `vite_preload_assets` templatetag w/ `file_ext`, `as_type` and `**kwargs`
- Custom args like `crossorigin` for fonts supported via `**kwargs`
- Automatic MIME type detection using `mimetypes` from standard library
Usage: {% vite_preload_assets 'src/entry.js' file_ext='.woff2' as_type='font' %}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add new Django template tag to generate tags for Vite-bundled assets, enabling early loading of critical resources like fonts and images to improve page load performance.
assetsfield to ManifestEntry to support asset references in Vite manifestgenerate_preload_assets_tagsmethod inDjangoViteAppClientvite_preload_assetstemplatetag w/file_ext,as_typeand**kwargscrossoriginfor fonts supported via**kwargsmimetypesfrom standard libraryUsage: {% vite_preload_assets 'src/entry.js' file_ext='.woff2' as_type='font' %}
This template tag addresses the performance issue where fonts loaded via CSS
@font-facerules don't start downloading until after the CSS is parsed, causing layout shifts when fonts swap in. For example:You can put this in the
<head>of a base templateWhere
theme/fonts.css.tsis like:Also, the template tag name might cause confusion with the existing
vite_preload_asset(no "s") tag, so maybe I should rename it tovite_preload_linksor something..?Anyways, I hope that extra context helps explain why I'm making this contribution. I've tried my best to follow the repo's existing patterns and included unit tests to maintain the project's high test coverage.
Please let me know if you have any questions or would like me to make any changes.