Laravel — P63: Optimizing Views

Render faster, shine brighter

We’re wrapping up our discussion on views and what better place to finish up on than optimizing our views.

Normally, Blade templates are compiled when requested by the user. Laravel will check to see if a compiled version of the view exists first, and if it does, it will send it, otherwise it will compile it and then send it.

If a compiled version exists, Laravel will determine if the uncompiled version has modifications that are dated after the compiled version. If they are, Laravel will recompile the view before sending it.

As you can see, there’s a lot of compiling/recompiling going on in Laravel. To eliminate this necessity to compile/recompile, we can precompile all views with the view:cache command. The command is normally run during the deployment process.

php artisan view:cache
If you would like to clear the view cache, you may run the view:clear artisan command.
php artisan view:clear

After running it on my docker instance, I receive the following message:

# php artisan view:cache

   INFO  Blade templates cached successfully.  

#

If we visit any of our routes now, everything is still rendering properly. Where are the cached files stored? storage/framework/views directory. Once your run the command, check them out there.

Initially, the folder is empty.

Running the command, you’ll see that the folder populates.

Running the view:clear command removes the files from the directory again.

If you visit any page in your browser now, you’ll see Laravel’s view caching in the works. For example, I’ll visit http://0.0.0.0/fast-cars.

I can see the view was generated.

That’s it for optimizing views. You might notice a difference in larger applications with a ton of computation, but most of the time it’ll be unnoticeable. However, whenever you deploy, you should try to incorporate this command into your deployment process.

Laravel Series

Continue your Laravel Learning.

Laravel — P62: View Composers

Wire data into Blade with view composers

Laravel – P62: View Composers

In part 61 of our Laravel fundamentals series, learn techniques to share global data with every Blade view. Explore View::share, service-provider boot methods, view composers, and caching tips for consistent, performant layouts across your app.

Laravel — P63: Optimizing Views

Render faster, shine brighter

Laravel – P63: Optimizing Views

Part 63 of our Laravel fundamentals series dives into squeezing every millisecond out of your Blade views. Learn view caching, compiled templates, smart component design, and asset optimization to deliver snappy, user-friendly pages.

 

Laravel — P64: Rendering JSON in Blade

Serve structured data straight from Blade

Laravel – P64: Rendering Json In Blade

Part 64 of our Laravel fundamentals series shows you how to safely render JSON from Blade templates. Learn encoding strategies, escaping rules, inline scripts, and performance tips to deliver structured data to front-end frameworks with zero headaches.

 

Leave a Reply