Skip to content

Livewire 3 + Localization 404 Not found - How to solve #941

@crgAdrian

Description

@crgAdrian

Add in web.php

    Livewire::setUpdateRoute(function ($handle) {
        return Route::post('/livewire/update', $handle);
    });

in your app.blade

<script>
       (function() {
           const originalFetch = window.fetch;
           const originalXHROpen = XMLHttpRequest.prototype.open;
           const originalXHRSend = XMLHttpRequest.prototype.send;

           @php
               $locale = request()->segment(1);
               $supportedLocales = array_keys(config('app.supported_locales', []));
               $shouldAddLocale = in_array($locale, $supportedLocales);
           @endphp

           @if($shouldAddLocale)
           const currentLocale = '{{ $locale }}';

           window.fetch = function(url, options) {
               if (typeof url === 'string' && url.includes('/livewire/update')) {
                   url = url.replace('/livewire/update', `/${currentLocale}/livewire/update`);
               }
               return originalFetch.call(this, url, options);
           };

           XMLHttpRequest.prototype.open = function(method, url, ...args) {
               if (typeof url === 'string' && url.includes('/livewire/update')) {
                   url = url.replace('/livewire/update', `/${currentLocale}/livewire/update`);
               }
               return originalXHROpen.call(this, method, url, ...args);
           };
           @endif
       })();
   </script>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions