Skip to content

Make Livewire work with full measure static caching#7894

Merged
jasonvarga merged 1 commit into
statamic:3.4from
aerni:patch-5
Apr 13, 2023
Merged

Make Livewire work with full measure static caching#7894
jasonvarga merged 1 commit into
statamic:3.4from
aerni:patch-5

Conversation

@aerni
Copy link
Copy Markdown
Contributor

@aerni aerni commented Apr 12, 2023

This PR makes it possible to use Livewire together with full measure static caching. It ensures that the STATAMIC_CSRF_TOKEN placeholder is replaced when the cached page is loaded.

Why this is needed

Statamic replaces all token occurrences in the CsrfTokenReplacer:

$response->setContent(str_replace(
$token,
self::REPLACEMENT,
$content
));

So Livewire doesn't have a valid token to work with:

CleanShot 2023-04-12 at 14 08 49@2x

Which results in this error when interacting with the Livewire component after a cached page is loaded:

CleanShot 2023-04-12 at 14 13 14@2x

With this PR, the livewire_token is replaced and the error is gone:

CleanShot 2023-04-12 at 14 10 35@2x

@what-the-diff
Copy link
Copy Markdown

what-the-diff Bot commented Apr 12, 2023

PR Summary

  • Added a new condition to the getNocacheJs() function
    This change ensures that window.livewire_token is set to data.csrf properly, enhancing the security and reliability of the application.

@jasonvarga
Copy link
Copy Markdown
Member

Is half measure broken when using livewire?

@aerni
Copy link
Copy Markdown
Contributor Author

aerni commented Apr 13, 2023

No, Half Measure works because it's hitting PHP and replaces the STATAMIC_CSRF_TOKEN placeholder here:

public function replaceInCachedResponse(Response $response)
{
if (! $response->getContent()) {
return;
}
$response->setContent(str_replace(
self::REPLACEMENT,
csrf_token(),
$response->getContent()
));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants