Avoid script duplication in Laravel by using @once blade directive

489 0

One of the things that can affect your web application performance and usability is the excessive use of Javascript or CSS code, and one way of overusing it is the duplication of scripts or included css files. Hopefully, Laravel has a way to avoid script, css and even html code duplication when using multiple files included on the same layout. This is accomplished by using the @once Blade directive.

The @once Blade directive in Laravel is a useful tool for controlling the rendering of certain sections of your views. It allows you to specify that a certain section should only be rendered once, even if it is included multiple times in the same view. To use the @once directive, you simply wrap the section you want to render once in the @once block. For example, consider the following view:

<header><nav><a href="#">Home</a>
<a href="#">About</a>
<a href="#">Contact</a></nav></header>

@once
<aside>
<h2>Recent Posts</h2>
<ul>
 	<li>Post 1</li>
 	<li>Post 2</li>
 	<li>Post 3</li>
</ul>
</aside>
@endonce

In this example, the header section is included in the view as usual, but the aside section is wrapped in the @once block. This means that even if the view is included multiple times in the same request, the aside section will only be rendered once.

It’s worth noting that the @once directive works per request, not per session or per user. This means that if two separate users request the same view, both will see the aside section rendered. However, if the same user requests the view multiple times in the same request, the aside section will only be rendered once.

Using @once Blade directive to avoid script duplication

You can also use @once Blade directive to avoid javascript or css duplication by wraping the @push blade directive in the @once block. This way, if the script is already included in another section/layout/component, it will not be added again:

@once
@push('scripts')
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" crossorigin="anonymous"></script>
@endpush
@endonce

Since Laravel 9, there is also a @pushonce Blade directive, that joins @once and @push directives in one, for convenience:

@pushOnce('scripts')
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" crossorigin="anonymous"></script>
@endpushOnce

In conclusion, the @once Blade directive in Laravel is a useful tool for controlling the rendering of certain sections of your views. It allows you to specify that a certain section should only be rendered once, even if it is included multiple times in the same view. By using the @once directive, you can ensure that your views are clean, optimized, and render efficiently.

(Visited 346 times, 1 visits today)

Elisio Leonardo

Elisio Leonardo is an experienced Web Developer, Solutions Architect, Digital Marketing Expert, and content producer with a passion for technology, artificial intelligence, web development, and entertainment. With nearly 15 years of writing engaging content on technology and entertainment, particularly Comic Book Movies, Elisio has become a trusted source of information in the digital landscape.