Menü schliessen
Created: July 15th 2022
Last updated: September 8th 2022
Categories: IT Development,  Laravel
Author: Miljan Puzovic

Add defer attribute to the Laravel Vite script tags

Tags:  github,  Laravel,  package,  vite
Donation Section: Background
Monero Badge: QR-Code
Monero Badge: Logo Icon Donate with Monero Badge: Logo Text
82uymVXLkvVbB4c4JpTd1tYm1yj1cKPKR2wqmw3XF8YXKTmY7JrTriP4pVwp2EJYBnCFdXhLq4zfFA6ic7VAWCFX5wfQbCC

Laravel now officially uses Vite for bundling the assets. While everything works fine and fast we have noticed that defer attribute is missing from the scripts of the compiled bundle. We have resolved this by creating a new class which extends Vite class and overrides a method responsible for creating script tags. Then, we have created new service provider which overrides the the whole Vite class with our own newly created class. And of course, the new provider need to be registered (added in the list of the all providers).

In order to avoid this manual process, since we would have to do this on multiple Laravel apps, we have created a small package for this called lexo/defer-vite.

The script can be installed using:

composer require lexo/defer-vite

The code can be seen on GitHub.