When making a non-GET Inertia request, via <Link>
or manually, be sure to always respond with a proper Inertia response.
For example, if you're creating a new user, have your "store" endpoint return a redirect back to a standard GET endpoint, such as your user index page.
Inertia will automatically follow this redirect and update the page accordingly. Here's a simplified example.
class UsersController extends Controller
{
public function index()
{
return Inertia::render('Users/Index', [
'users' => User::all(),
]);
}
public function store()
{
User::create(
Request::validate([
'name' => ['required', 'max:50'],
'email' => ['required', 'max:50', 'email'],
])
);
return Redirect::route('users.index');
}
}
Note, when redirecting after a PUT
, PATCH
or DELETE
request you must use a 303
response code, otherwise the subsequent request will not be treated as a GET
request. A 303
redirect is the same as a 302
except that the follow-up request is explicitly changed to a GET
request.
If you're using one of our official server-side adapters, redirects will automatically be converted.
Sometimes it's necessary to redirect to an external website, or even another non-Inertia endpoint in your app, within an Inertia request. This is possible using a server-side initiated window.location
visit.
return Inertia::location($url);
This will generate a 409 Conflict
response, which includes the destination URL in the X-Inertia-Location
header. Client-side, Inertia will detect this response and automatically do a window.location = url
visit.