Download the PHP package angus-mcritchie/blade-boost-directive without Composer
On this page you can find all versions of the php package angus-mcritchie/blade-boost-directive. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download angus-mcritchie/blade-boost-directive
More information about angus-mcritchie/blade-boost-directive
Files in angus-mcritchie/blade-boost-directive
Package blade-boost-directive
Short Description Lightning-Fast Blade Components with `@boost` Directive
License MIT
Homepage https://github.com/angus-mcritchie/blade-boost-directive
Informations about the package blade-boost-directive
Lightning-Fast Blade Components with @boost
Adds a @boost Blade directive to your Laravel application. Caches the HTML rendered from Blade code. Optionally you can perform a simple str_replace for any variables passed to the directive.
Table of Contents
- Installation
- Signature
- Parameters
- Usage
- Repeating Code Use Case
- But I Have an
ifStatment in There! - Large Component Use Case
- Simple Small Repeating Use Case
- Escaping
- Cache Store
- Configuration
- Benchmarks
- Testing
- Changelog
- Contributing
- Security Vulnerabilities
- Credits
- License
Installation
You can install the package via composer:
Signature
Parameters
Usage
The @boost directive can be used to wrap any Blade code. It will render the blade code once and cache the HTML and can optionally replace any variables passed to the directive via the second argument.
Repeating Code Use Case
A common use case would be that you're rendering a page of <x-post.card /> components which looks like this:
That is a total of 9 components per post, if we have 30 per page, that is 270 components in total and there are only 4 variables for the whole card.
Let's see how we can use the @boost directive to speed this up.
Wrapping the whole component in the @boost directive will store the HTML in the cache, then replace the passed variables with the values from the $post object and results in Blade only rendering 1 component instead of 270 🚀.
You could use the file cache store and render zero components (after the first request) but you'll need to clear the cache when you make changes to the component.
But I Have an if Statement in There!
If you want to conditionally show the badge, you'll want to update the key to include the condition so that the cache is different for each condition.
Passing the key as an array with all your conditions, @boost will join the array with a . and use that as the cache key.
Large Component Use Case
Another common use case would be that you're rendering a page with a single components with many smaller other components.
To speed this up we can use the @boost directive to cache the HTML for the footer and only render it once.
By default, @boost uses the array cache store, which is the fastest cache store available but is not persistent, this use case you'll want to use the file cache store.
You can do this by passing a third argument to the @boost directive.
Let's see how we can use the @boost directive to speed this up.
Now, Blade will just render this component once and store the HTML in the cache. The next time the page is loaded, the pre-rendered component will be stored in the file cache store. This is useful if you want to speed up large, but simple components that are used on every page and only rendered once.
Simple Small Repeating Use Case
Alternative syntax you can pass the key as the first parameter which is handle for simple cases where you don't need to pass any options, or you just prefer to pass the key as the first parameter.
Mind you that the @boost directive will use the Cache::rememberForever() under the hood so it's up to you to clear the cache when you make changes to the component.
Escaping
The @boost directive will escape the HTML by default using Laravel's e() function. If you want to render the HTML without escaping, you can pass the raw option to the directive. This will allow you to output raw HTML without any escaping, which can be useful in certain scenarios where you trust the content being rendered.
Cache Store
The @boost directive will prefix the cache key with the blade-boost-directive. prefix which is configurable in the config file. This is to avoid collisions with other packages or parts of your application.
If you wish to clear a specific cache key, you can use the following.
Configuration
You can publish the configuration file with the following command:
This is the contents of the published config file:
Benchmarks
Our benchmarks show the performance improvements you can expect when using the @boost directive. The benchmarks are not exhaustive and only serve as a demonstration of the performance improvements you can expect.
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Angus McRitchie
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of blade-boost-directive with dependencies
illuminate/contracts Version ^10.0||^11.0||^12.0||^13.0
spatie/laravel-package-tools Version ^1.16