How does `php artisan view:cache` work? I wonder how the php artisan view:cache works, as in the documentation, it says :
This command precompiles all your Blade views so they are not compiled on demand, improving the performance of each request that returns a view.
Do the newly added pages, after running this command also get cached too?
Thanks!
Check the folder /storage/framework/views after running the command. All it does is write a php file for each blade file in this folder :) It is the same as manually visiting each blade file through a controller..
Laravel doesnt actually load the blade file as php. Instead it converts it to a php file and puts it into that folder. So the command just does this for you, so the file doesnt need to be created on the first visit to the page
@Sinnbeck will the new blade file get cached too? or should I run this command every time I create a new blade file?
@adrianriyadi you only run it in production as part of your deployment script
Just in case, in Laravel 10 , a cached filename generation is done in the following function:
// \Illuminate\View\Compilers\Compiler::getCompiledPath($path)
return $this->cachePath.'/'.hash('xxh128', 'v2'.Str::after($path, $this->basePath)).'.'.$this->compiledExtension;
The Artisan command view:cache calls the function in the following order:
// \Illuminate\Foundation\Console\ViewCacheCommand::handle()
// \Illuminate\Foundation\Console\ViewCacheCommand::compileViews(Collection $views)
// \Illuminate\View\Compilers\BladeCompiler::compile($path = null)
// \Illuminate\View\Compilers\Compiler::getCompiledPath($path)
Evidently, only the path of the Blade file determines the cached file filename.
The function used for the hashing is from the following set: https://github.com/Cyan4973/xxHash
Please sign in or create an account to participate in this conversation.