[5.x] Prevent multiple invocations of the {{ vite }}
tag to eliminate asset duplication
#11298
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.
Description
This PR addresses the issue of duplicate CSS and JS asset links in the generated
index.html
caused by multiple invocations of the{{ vite }}
tag during the Static Site Generation (SSG) process. By introducing a static flag within theVite
tag class, the{{ vite }}
tag is now rendered only once per request, effectively eliminating redundant<link>
and<script>
tags.Problem
Previously, the
{{ vite }}
tag was being called multiple times (approximately 10 times) during the SSG process. This resulted in duplicated CSS and JS asset links in theindex.html
file for every generated page. Such duplication can lead to increased page load times, redundant asset fetching, and potential conflicts in script execution.Solution
Introduced a Static Flag:
private static $hasRendered
property initialized tofalse
to track whether the{{ vite }}
tag has already been rendered.Modified the
index()
Method:index()
method to determine if the{{ vite }}
tag has been rendered previously.self::$hasRendered
istrue
, the method returns an empty string, preventing further rendering of asset links.self::$hasRendered
is set totrue
to block subsequent invocations.Enhanced Logging:
Log::info('Vite tag called with src: ' . implode(', ', $src));
to monitor the invocation of the{{ vite }}
tag and ensure it is called only once.Benefits
Performance Improvement:
Eliminates redundant asset loading by ensuring each CSS and JS file is linked only once, reducing page load times and bandwidth usage.
Maintainability:
Simplifies asset management within the SSG process, making the codebase cleaner and easier to maintain.
Debugging Ease:
Enhanced logging provides clear insights into the invocation of the {{ vite }} tag, facilitating easier troubleshooting in the future.
Testing
Add Logging for debugging purposes:
Log::info('Vite tag called with src: ' . implode(', ', $src));
Build Assets:
npm run build
Generate Static Site:
php please ssg:generate
Verify Logs:
Open storage/logs/laravel.log.
Ensure that the Vite tag is called only once:
Inspect index.html:
Navigate to storage/app/static/index.html.
Confirm the absence of duplicated and <script> tags (may be unrelated):
Possible related issues:
https://github.com/statamic/cms/issues/11295