From 78b38da2c7ff427834ec4383edde22dd343e4a0e Mon Sep 17 00:00:00 2001 From: Roy Duineveld Date: Wed, 27 Nov 2024 16:03:55 +0100 Subject: [PATCH] @includeCached directive (#18) --- README.md | 27 +++++++++++++++++++++++++- src/BladeDirectivesServiceProvider.php | 14 +++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 29bb93f..1d83323 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,14 @@ # Rapidez Blade Directives -This package adds blade directives that we found we needed in Laravel during development of Rapidez. Like `@slots`, which lets you define optional slots so your `attributes->merge()` always works. Or `@includeFirstSafe` which works the same as `@includeFirst` but will not throw errors if no template was found. +This package adds blade directives that we found we needed in Laravel during development of Rapidez. Like `@slots`, which lets you define optional slots so your `attributes->merge()` always works. Or `@includeFirstSafe` which works the same as `@includeFirst` but will not throw errors if no template was found. All directives included within this package: + +- [`@attributes`](#attributes) +- [`@includeFirstSafe`](#includefirstsafe) +- [`@markdown`](#markdown) +- [`@return`](#return) +- [`@slots`](#slots) +- [`@slotdefault` + `@endslotdefault`](#slotdefault) +- [`@includeCached`](#includecached) ## Installation @@ -152,6 +160,23 @@ If you only wish to change the text without changing attributes you can also pas ``` +### @includeCached + +Just like [`@include`](https://laravel.com/docs/11.x/blade#including-subviews) but cached. Everything returned will be cached with [`Cache::flexible()`](https://laravel.com/docs/11.x/cache#swr) for 5 minutes; and refreshed in the background until it expires after 24 hours. After that it will be refreshed as usual. The cache key is a combination of the view name and the current slugified url. That way this can be used with multisite setups: +``` +include-cache::site-{ Str::slug(url('/')) }-{ $viewName }' +``` + +#### Usage + +```blade +@includeCached('view.name') +``` + +#### Notes + +Keep in mind that any dynamic things within the view will not be executed when cached. For example `@push`, see [Blade Stacks](https://laravel.com/docs/11.x/blade#stacks). Also [Blade Icons Deferring](https://github.com/blade-ui-kit/blade-icons#deferring-icons) doesn't work, you have to use these things outside the cached include! + ## Helpers ### optionalDeep diff --git a/src/BladeDirectivesServiceProvider.php b/src/BladeDirectivesServiceProvider.php index d0f97bd..cf4b661 100644 --- a/src/BladeDirectivesServiceProvider.php +++ b/src/BladeDirectivesServiceProvider.php @@ -62,6 +62,20 @@ public function register() Blade::directive('endslotdefault', function ($expression) { return ''; }); + + Blade::directive('includeCached', function ($expression) { + $expression = Blade::stripParentheses($expression); + + // The rest of the cacheKey needs to be within the generated views. + // That way it's stays dynamic. The Str::slug(url('/')) is used + // to support multi sites; a cache per application url. + // You could use URL::forceRootUrl(...) for this. + $cacheKey = trim($expression, '\'"'); + + // So it's cached for 5 minutes and refreshed in the background until + 24 hours. + // Which never happens but it's required; after that it refreshed directly. + return "addMinutes(5), now()->addDay()], fn() => \$__env->make({$expression}, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render()); ?>"; + }); } public function boot()