Skip to content
This repository was archived by the owner on Jan 27, 2025. It is now read-only.

Commit 580f26d

Browse files
Merge pull request #64 from aerni/fix/replacer
Fix static caching
2 parents 443651e + d35ab10 commit 580f26d

File tree

3 files changed

+73
-37
lines changed

3 files changed

+73
-37
lines changed

README.md

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Visit my newest project Statamictutorials.com. Many tutorials are free.
99
[<img src="https://statamictutorials.com/images/seo/statamictutorials.png" width="600" />](https://statamictutorials.com)
1010

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

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

@@ -23,21 +23,21 @@ composer require jonassiewertsen/statamic-livewire
2323
### Manually including Livewire's frontend assets
2424
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:
2525

26-
```html
26+
```html
2727
<html>
2828
<head>
2929
<!-- If using Antlers -->
3030
{{ livewire:styles }}
31-
31+
3232
<!-- If using Blade -->
3333
@livewireStyles
3434
</head>
3535
<body>
36-
36+
3737
...
3838
<!-- If using Antlers -->
3939
{{ livewire:scripts }}
40-
40+
4141
<!-- Blade -->
4242
@livewireScripts
4343
</body>
@@ -47,21 +47,21 @@ By default, Livewire injects the JavaScript and CSS assets it needs into each pa
4747
### Manually bundling Livewire and Alpine
4848
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.
4949

50-
```html
50+
```html
5151
<html>
5252
<head>
5353
<!-- If using Antlers -->
5454
{{ livewire:styles }}
55-
55+
5656
<!-- If using Blade -->
5757
@livewireStyles
5858
</head>
5959
<body>
60-
60+
6161
...
6262
<!-- If using Antlers -->
6363
{{ livewire:scriptConfig }}
64-
64+
6565
<!-- Blade -->
6666
@livewireScriptConfig
6767
</body>
@@ -77,6 +77,16 @@ This addon adds an `AssetsReplacer` class to make Livewire compatible with half
7777
],
7878
```
7979

80+
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.
81+
82+
```js
83+
if (window.livewireScriptConfig?.csrf === 'STATAMIC_CSRF_TOKEN') {
84+
document.addEventListener('statamic:nocache.replaced', () => Livewire.start());
85+
} else {
86+
Livewire.start();
87+
}
88+
```
89+
8090
## Upgrade
8191
8292
Make sure to read the Livewire upgrade guide, in case you're updating to `Statamic Livewire` 3, as there are breaking changes:
@@ -86,13 +96,13 @@ https://livewire.laravel.com/docs/upgrading
8696
[Livewire Docs](https://livewire.laravel.com/docs/quickstart)
8797
8898
### Include components
89-
You can create Livewire components as described in the [general documentation](https://livewire.laravel.com/docs/components).
99+
You can create Livewire components as described in the [general documentation](https://livewire.laravel.com/docs/components).
90100
To include your Livewire component:
91101
```html
92102
<body>
93103
<!-- If using Antlers -->
94104
{{ livewire:your-component-name }}
95-
105+
96106
<!-- If using Blade -->
97107
<livewire:your-component-name />
98108
</body>
@@ -195,7 +205,7 @@ You can paginate results by using the WithPagination trait.
195205
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).
196206
197207
### Antlers
198-
Pagination with Antlers does work similar. Make sure to use the `Jonassiewertsen\Livewire\WithPagination` namespace for your trait if working with Antlers.
208+
Pagination with Antlers does work similar. Make sure to use the `Jonassiewertsen\Livewire\WithPagination` namespace for your trait if working with Antlers.
199209
200210
In your Livewire component view:
201211
```html
@@ -230,7 +240,7 @@ class ShowArticles extends Component
230240
```
231241
232242
### EXPERIMENTAL: Statamic Support
233-
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.
243+
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.
234244
235245
Supported types:
236246
- Statamic\Entries\EntryCollection;
@@ -252,7 +262,7 @@ class Foo extends Component
252262
}
253263
```
254264
255-
To make it work, you need to enable that feature first.
265+
To make it work, you need to enable that feature first.
256266
257267
1. php artisan vendor:publish
258268
2. Select statamic-livewire in the list
@@ -263,14 +273,14 @@ In case you want to share state between Livewire and Alpine, there is a Blade di
263273
```html
264274
<!-- With Antlers -->
265275
<div x-data="{ open: {{ livewire:entangle property='showDropdown' modifier='live' }} }">
266-
276+
267277
<!-- With Blade -->
268278
<div x-data="{ open: @entangle('showDropdown').live }">
269279
```
270280
271281
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).
272282
```html
273-
<div x-data="{ open: $wire.entangle('showDropdown', true) }">
283+
<div x-data="{ open: $wire.entangle('showDropdown', true) }">
274284
```
275285
### This: Accessing the Livewire component
276286
You can access and perform actions on the Livewire component like this:
@@ -280,7 +290,7 @@ You can access and perform actions on the Livewire component like this:
280290
document.addEventListener('livewire:initialized', function () {
281291
// With Antlers
282292
{{ livewire:this set="('name', 'Jack')" }}
283-
293+
284294
// With Blade
285295
@this.set('name', 'Jack')
286296
})
@@ -324,9 +334,9 @@ Thanks to:
324334
- Statamic 4 or 5
325335
326336
# Support
327-
I love to share with the community. Nevertheless, it does take a lot of work, time and effort.
337+
I love to share with the community. Nevertheless, it does take a lot of work, time and effort.
328338
329339
[Sponsor me on GitHub](https://github.com/sponsors/jonassiewertsen/) to support my work and the support for this addon.
330340
331-
# License
341+
# License
332342
This plugin is published under the MIT license. Feel free to use it and remember to spread love.

src/Replacers/AssetsReplacer.php

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
namespace Jonassiewertsen\Livewire\Replacers;
44

55
use Illuminate\Http\Response;
6-
use Illuminate\Support\Str;
76
use Livewire\Features\SupportAutoInjectedAssets\SupportAutoInjectedAssets;
87
use Livewire\Features\SupportScriptsAndAssets\SupportScriptsAndAssets;
9-
use Livewire\Livewire;
108
use Livewire\Mechanisms\FrontendAssets\FrontendAssets;
9+
use Statamic\StaticCaching\Cacher;
10+
use Statamic\StaticCaching\Cachers\NullCacher;
1111
use Statamic\StaticCaching\Replacer;
1212

1313
class AssetsReplacer implements Replacer
@@ -18,31 +18,57 @@ public function prepareResponseToCache(Response $responseToBeCached, Response $i
1818
return;
1919
}
2020

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

26+
$assetsHead = '';
27+
$assetsBody = '';
28+
29+
$assets = array_values(SupportScriptsAndAssets::getAssets());
30+
31+
if (count($assets) > 0) {
32+
foreach ($assets as $asset) {
33+
$assetsHead .= $asset."\n";
34+
}
35+
}
36+
37+
if ($this->shouldInjectLivewireAssets()) {
38+
$assetsHead .= FrontendAssets::styles()."\n";
39+
$assetsBody .= FrontendAssets::scripts()."\n";
40+
41+
/**
42+
* Ensure Livewire injects its assets on the initial request.
43+
*
44+
* @see \Livewire\Features\SupportAutoInjectedAssets\SupportAutoInjectedAssets
45+
*/
46+
app(FrontendAssets::class)->hasRenderedStyles = false;
47+
app(FrontendAssets::class)->hasRenderedScripts = false;
48+
}
49+
2550
$responseToBeCached->setContent(
26-
SupportAutoInjectedAssets::injectAssets(
27-
html: $content,
28-
assetsHead: implode('', $assets),
29-
assetsBody: ''
30-
)
51+
SupportAutoInjectedAssets::injectAssets($content, $assetsHead, $assetsBody)
3152
);
3253
}
3354

34-
public function replaceInCachedResponse(Response $response)
55+
protected function shouldInjectLivewireAssets()
3556
{
36-
if (Str::contains($response, FrontendAssets::scripts())) {
37-
return;
57+
if (! SupportAutoInjectedAssets::$forceAssetInjection && config('livewire.inject_assets', true) === false) {
58+
return false;
3859
}
39-
40-
if (Str::contains($response, FrontendAssets::scriptConfig())) {
41-
return;
60+
if ((! SupportAutoInjectedAssets::$hasRenderedAComponentThisRequest) && (! SupportAutoInjectedAssets::$forceAssetInjection)) {
61+
return false;
62+
}
63+
if (app(FrontendAssets::class)->hasRenderedScripts) {
64+
return false;
4265
}
4366

44-
app(FrontendAssets::class)->hasRenderedScripts = false;
67+
return true;
68+
}
4569

46-
Livewire::forceAssetInjection();
70+
public function replaceInCachedResponse(Response $response)
71+
{
72+
//
4773
}
4874
}

src/ServiceProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public function bootAddon(): void
3030
protected function bootReplacers(): void
3131
{
3232
config()->set('statamic.static_caching.replacers', array_merge(
33-
config('statamic.static_caching.replacers'),
34-
config('statamic-livewire.replacers')
33+
config('statamic-livewire.replacers'),
34+
config('statamic.static_caching.replacers')
3535
));
3636
}
3737

0 commit comments

Comments
 (0)