Skip to content

Commit

Permalink
Merge pull request #64 from aerni/fix/replacer
Browse files Browse the repository at this point in the history
Fix static caching
  • Loading branch information
jonassiewertsen authored Jun 26, 2024
2 parents 443651e + d35ab10 commit 580f26d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 37 deletions.
48 changes: 29 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Visit my newest project Statamictutorials.com. Many tutorials are free.
[<img src="https://statamictutorials.com/images/seo/statamictutorials.png" width="600" />](https://statamictutorials.com)

## Description
A third-party [Laravel Livewire](https://laravel-livewire.com/) integration for Statamic.
A third-party [Laravel Livewire](https://laravel-livewire.com/) integration for Statamic.

It's as easy as it gets to get started with Livewire if using Statamic.

Expand All @@ -23,21 +23,21 @@ composer require jonassiewertsen/statamic-livewire
### Manually including Livewire's frontend assets
By default, Livewire injects the JavaScript and CSS assets it needs into each page that includes a Livewire component. If you want more control over this behavior, you can [manually include the assets](https://livewire.laravel.com/docs/installation#manually-including-livewires-frontend-assets) on a page using the following Antlers tags or Blade directives:

```html
```html
<html>
<head>
<!-- If using Antlers -->
{{ livewire:styles }}

<!-- If using Blade -->
@livewireStyles
</head>
<body>

...
<!-- If using Antlers -->
{{ livewire:scripts }}

<!-- Blade -->
@livewireScripts
</body>
Expand All @@ -47,21 +47,21 @@ By default, Livewire injects the JavaScript and CSS assets it needs into each pa
### Manually bundling Livewire and Alpine
If you need to include some custom Alpine plugins, you need to [manually bundle the Livewire and Alpine assets](https://livewire.laravel.com/docs/installation#manually-bundling-livewire-and-alpine) and disable the automatic injection by using the following Antlers tag or Blade directive. Do not forget to include the Livewire styles as well.

```html
```html
<html>
<head>
<!-- If using Antlers -->
{{ livewire:styles }}

<!-- If using Blade -->
@livewireStyles
</head>
<body>

...
<!-- If using Antlers -->
{{ livewire:scriptConfig }}

<!-- Blade -->
@livewireScriptConfig
</body>
Expand All @@ -77,6 +77,16 @@ This addon adds an `AssetsReplacer` class to make Livewire compatible with half
],
```

If you are using full measure static caching and you're manually bundling Livewire and Alpine as per the instructions above, you need to make sure to only start Livewire once the CSRF token has been replaced.

```js
if (window.livewireScriptConfig?.csrf === 'STATAMIC_CSRF_TOKEN') {
document.addEventListener('statamic:nocache.replaced', () => Livewire.start());
} else {
Livewire.start();
}
```
## Upgrade
Make sure to read the Livewire upgrade guide, in case you're updating to `Statamic Livewire` 3, as there are breaking changes:
Expand All @@ -86,13 +96,13 @@ https://livewire.laravel.com/docs/upgrading
[Livewire Docs](https://livewire.laravel.com/docs/quickstart)
### Include components
You can create Livewire components as described in the [general documentation](https://livewire.laravel.com/docs/components).
You can create Livewire components as described in the [general documentation](https://livewire.laravel.com/docs/components).
To include your Livewire component:
```html
<body>
<!-- If using Antlers -->
{{ livewire:your-component-name }}

<!-- If using Blade -->
<livewire:your-component-name />
</body>
Expand Down Expand Up @@ -195,7 +205,7 @@ You can paginate results by using the WithPagination trait.
To use pagination with Blade, please use the `Livewire\WithPagination` namespace for your trait as described in the [Livewire docs](https://livewire.laravel.com/docs/pagination#basic-usage).
### Antlers
Pagination with Antlers does work similar. Make sure to use the `Jonassiewertsen\Livewire\WithPagination` namespace for your trait if working with Antlers.
Pagination with Antlers does work similar. Make sure to use the `Jonassiewertsen\Livewire\WithPagination` namespace for your trait if working with Antlers.
In your Livewire component view:
```html
Expand Down Expand Up @@ -230,7 +240,7 @@ class ShowArticles extends Component
```
### EXPERIMENTAL: Statamic Support
As a little experiment, support for an Entry or EntryCollection has been added, so you can make an entry or a entry collection simply a public property and it just works.
As a little experiment, support for an Entry or EntryCollection has been added, so you can make an entry or a entry collection simply a public property and it just works.
Supported types:
- Statamic\Entries\EntryCollection;
Expand All @@ -252,7 +262,7 @@ class Foo extends Component
}
```
To make it work, you need to enable that feature first.
To make it work, you need to enable that feature first.
1. php artisan vendor:publish
2. Select statamic-livewire in the list
Expand All @@ -263,14 +273,14 @@ In case you want to share state between Livewire and Alpine, there is a Blade di
```html
<!-- With Antlers -->
<div x-data="{ open: {{ livewire:entangle property='showDropdown' modifier='live' }} }">

<!-- With Blade -->
<div x-data="{ open: @entangle('showDropdown').live }">
```
It's worth mentioning that, since Livewire v3 now builds on top of Alpine, the `@entangle` directive is not documented anymore. Instead, it's possible to entangle the data via [the `$wire` object](https://livewire.laravel.com/docs/javascript#the-wire-object).
```html
<div x-data="{ open: $wire.entangle('showDropdown', true) }">
<div x-data="{ open: $wire.entangle('showDropdown', true) }">
```
### This: Accessing the Livewire component
You can access and perform actions on the Livewire component like this:
Expand All @@ -280,7 +290,7 @@ You can access and perform actions on the Livewire component like this:
document.addEventListener('livewire:initialized', function () {
// With Antlers
{{ livewire:this set="('name', 'Jack')" }}

// With Blade
@this.set('name', 'Jack')
})
Expand Down Expand Up @@ -324,9 +334,9 @@ Thanks to:
- Statamic 4 or 5
# Support
I love to share with the community. Nevertheless, it does take a lot of work, time and effort.
I love to share with the community. Nevertheless, it does take a lot of work, time and effort.
[Sponsor me on GitHub](https://github.com/sponsors/jonassiewertsen/) to support my work and the support for this addon.
# License
# License
This plugin is published under the MIT license. Feel free to use it and remember to spread love.
58 changes: 42 additions & 16 deletions src/Replacers/AssetsReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace Jonassiewertsen\Livewire\Replacers;

use Illuminate\Http\Response;
use Illuminate\Support\Str;
use Livewire\Features\SupportAutoInjectedAssets\SupportAutoInjectedAssets;
use Livewire\Features\SupportScriptsAndAssets\SupportScriptsAndAssets;
use Livewire\Livewire;
use Livewire\Mechanisms\FrontendAssets\FrontendAssets;
use Statamic\StaticCaching\Cacher;
use Statamic\StaticCaching\Cachers\NullCacher;
use Statamic\StaticCaching\Replacer;

class AssetsReplacer implements Replacer
Expand All @@ -18,31 +18,57 @@ public function prepareResponseToCache(Response $responseToBeCached, Response $i
return;
}

if (! $assets = SupportScriptsAndAssets::getAssets()) {
// Don't disturb Livewire's assets injection when caching is off.
if (app(Cacher::class) instanceof NullCacher) {
return;
}

$assetsHead = '';
$assetsBody = '';

$assets = array_values(SupportScriptsAndAssets::getAssets());

if (count($assets) > 0) {
foreach ($assets as $asset) {
$assetsHead .= $asset."\n";
}
}

if ($this->shouldInjectLivewireAssets()) {
$assetsHead .= FrontendAssets::styles()."\n";
$assetsBody .= FrontendAssets::scripts()."\n";

/**
* Ensure Livewire injects its assets on the initial request.
*
* @see \Livewire\Features\SupportAutoInjectedAssets\SupportAutoInjectedAssets
*/
app(FrontendAssets::class)->hasRenderedStyles = false;
app(FrontendAssets::class)->hasRenderedScripts = false;
}

$responseToBeCached->setContent(
SupportAutoInjectedAssets::injectAssets(
html: $content,
assetsHead: implode('', $assets),
assetsBody: ''
)
SupportAutoInjectedAssets::injectAssets($content, $assetsHead, $assetsBody)
);
}

public function replaceInCachedResponse(Response $response)
protected function shouldInjectLivewireAssets()
{
if (Str::contains($response, FrontendAssets::scripts())) {
return;
if (! SupportAutoInjectedAssets::$forceAssetInjection && config('livewire.inject_assets', true) === false) {
return false;
}

if (Str::contains($response, FrontendAssets::scriptConfig())) {
return;
if ((! SupportAutoInjectedAssets::$hasRenderedAComponentThisRequest) && (! SupportAutoInjectedAssets::$forceAssetInjection)) {
return false;
}
if (app(FrontendAssets::class)->hasRenderedScripts) {
return false;
}

app(FrontendAssets::class)->hasRenderedScripts = false;
return true;
}

Livewire::forceAssetInjection();
public function replaceInCachedResponse(Response $response)
{
//
}
}
4 changes: 2 additions & 2 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public function bootAddon(): void
protected function bootReplacers(): void
{
config()->set('statamic.static_caching.replacers', array_merge(
config('statamic.static_caching.replacers'),
config('statamic-livewire.replacers')
config('statamic-livewire.replacers'),
config('statamic.static_caching.replacers')
));
}

Expand Down

0 comments on commit 580f26d

Please sign in to comment.