Mate Pušić
Mate Pušić

Reputation: 1

Laravel redirect to causes "All inertia requests must receive a valid Inertia response" when accessing over internet

Using Laravel 11 with InertiaJS and Svelte.

I have an admin.php file which has some routes guarded by the auth middleware. I want the "/admin" route to redirect to /admin/posts, or /login if the user is not logged in. Using Inertia's <Link> component, navigating to this route should redirect to /admin/posts or /login and not refresh the app.

Instead it returns All Inertia requests must receive a valid Inertia response, however a plain JSON response was received. Searching a bit, I found Inertia needs the X-Inertia header, but it's present in the response headers.

The website is behind Nginx Proxy Manager. It's set to pass headers X-Inertia and X-Inertia-Version.

Website is live for now on https://pwa.skmedia.cc. Navigating to "Administracija" should return the error. Code is on github here

The website works locally when served with php artisan serve, but when deployed on a server behind Nginx Proxy Manager, redirected Inertia Links return the error. Redirecting back with redirect()->back() works fine, but I want to redirect to some other page.

I can use Inertia::location(), but this refreshes the page which I would like to avoid.

e.g. in web.php if i have:

Route::get('/redirected', function(){ return redirect()->to('/destination'); });
Route::get('/destination', function(){ return Inertia::render('Example'); });

In resources/js/Pages an empty Example.svelte file, and in my navigation something like:

<script>
    import { Link } from "@inertiajs/svelte";
</script>

<Link href="/redirected">Redirected</Link>

Clicking on that link on the website in "production" returns:

All Inertia requests must receive a valid Inertia response, however a plain JSON response was received.

while locally it redirects correctly to /destination .

Upvotes: 0

Views: 359

Answers (1)

Mate Pušić
Mate Pušić

Reputation: 1

SOLVED!

It seems the problem was serving the website over the internet with https, while it ran on the server listening to port 80 using http. I used nginx proxy manager to add a certificate and encrypt the traffic, but it seemed to cause issues with InertiaJS.

I created a self signed cert for the website to use and listen on port 443 with this guide, and I set Nginx Proxy Manager to forward port 443 instead of 80 and now it redirects correctly.

I assume it thought it contained an invalid Inertia response because the returned location header contained the http url of the website, instead of https.

Upvotes: 0

Related Questions