fix: add middleware to maintain locale across livewire requests (resolves #3037)#3038
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## dev #3038 +/- ##
============================================
- Coverage 96.99% 96.97% -0.03%
- Complexity 2472 2476 +4
============================================
Files 379 380 +1
Lines 11287 11294 +7
============================================
+ Hits 10948 10952 +4
- Misses 339 342 +3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
jobara
left a comment
There was a problem hiding this comment.
I'm not sure this is the best approach as the middleware will be touched by many if not all of the requests made, whether using livewire or not. There may be unintended side effects.
In looking at this more, I wonder if it's related to https://livewire.laravel.com/docs/3.x/upgrading#localization The docs there say that Livewire 3 doesn't respect the locale prefix anymore. You will need to change the update route. However, some people are reporting some other issues with this. livewire/livewire#8233
In Livewire 4 it seems you update this in a service provider and also allow for a hash value. https://livewire.laravel.com/docs/4.x/installation
This makes me think you should do the Livewire update and see if the issue still persists and if so, if you can change the update endpoint based on the Livewire 4 docs.
|
I checked the Livewire docs and also looked into similar discussions around this. The project uses chinleung/laravel-multilingual-routes, which registers separate routes for each locale instead of using a route group prefix approach. I also tried the setUpdateRoute() option, but even if I follow the approach mentioned in the Livewire docs (https://livewire.laravel.com/docs/3.x/upgrading#localization), it still requires middleware to store and restore the locale. In our case, the locale is dynamic per user, so customizing the update route alone does not fully solve the issue. I believe this would be the same situation even if we upgrade to a newer Livewire version. That is why I thought handling this through middleware might be a safer approach. |
| } else { | ||
| session(['locale' => app()->getLocale()]); |
There was a problem hiding this comment.
Why is the else condition necessary? Is there another similar issue with a different request?
There was a problem hiding this comment.
The else case saves the locale to the session on page requests, so it can be restored on Livewire requests.
Ideally you' probably want to be working with the latest versions to make sure that they haven't addressed the issue. There is also a new release of Laravel out now. If you don't do the update before hand, you'll need to leave a tasks to revisit the issue when you do the upgrades. |
Right, I'm also thinking to upgrade and check if it can be resolved. |
When searching or filtering on a page in a non-default locale, Livewire update requests fail with an error for pages that use translatable slugs. This middleware stores the locale in the session on page requests and restores it on Livewire requests.
Resolves #3037
Changes
ResolveRequestLocalemiddleware.Prerequisites
If this PR changes PHP code or dependencies:
composer formatand fixed any code formatting issues.composer analyzeand addressed any static analysis issues.php artisan testand ensured that all tests pass.composer localizeto update localization source files and committed any changes.If this PR changes CSS or JavaScript code or dependencies:
npm run lintand fixed any linting issues.npm run buildand ensured that CSS and JavaScript assets can be compiled.