Skip to content

Commit

Permalink
@includeCached directive (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
royduin authored Nov 27, 2024
1 parent 2c5cf38 commit 78b38da
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -152,6 +160,23 @@ If you only wish to change the text without changing attributes you can also pas
</div>
```

### @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
Expand Down
14 changes: 14 additions & 0 deletions src/BladeDirectivesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ public function register()
Blade::directive('endslotdefault', function ($expression) {
return '<?php endif; ?>';
});

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 "<?php echo \Illuminate\Support\Facades\Cache::flexible('include-cache::site-'.\Illuminate\Support\Str::slug(url('/')).'-{$cacheKey}', [now()->addMinutes(5), now()->addDay()], fn() => \$__env->make({$expression}, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render()); ?>";
});
}

public function boot()
Expand Down

0 comments on commit 78b38da

Please sign in to comment.