Skip to content

[6.x] Fix Livewire CSRF script error when livewireScriptConfig is undefined - #15483

Open
joshuablum wants to merge 1 commit into
6.xfrom
fix/livewire-csrf-script-config-guard
Open

joshuablum wants to merge 1 commit into
6.xfrom
fix/livewire-csrf-script-config-guard

Conversation

@joshuablum

Copy link
Copy Markdown
Member

This fixes a JS error thrown by the full measure static caching CSRF replacement script on standard Livewire 3 setups.

#13403 added a livewire:init listener for when the /!/csrf request resolves before Livewire has started. The listener sets window.livewireScriptConfig.csrf, but livewireScriptConfig only exists when Livewire is bundled manually through @livewireScriptConfig. With @livewireScripts or auto-injection, Livewire puts the token in the data-csrf attribute on its script tag and never defines livewireScriptConfig. The listener then throws:

Uncaught TypeError: can't access property "csrf", window.livewireScriptConfig is undefined

The error only shows up when the CSRF request finishes before Livewire starts, so it appears intermittently. Livewire keeps working, because it reads the token from data-csrf on every request and the script already replaces that attribute. The error still ends up in the console and in error trackers.

The listener now only sets the token when livewireScriptConfig exists. Behaviour for manually bundled Livewire stays as it was.

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.

1 participant