Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: handle non-inertia location redirects #312

Merged
merged 1 commit into from
Dec 2, 2021
Merged

fix!: handle non-inertia location redirects #312

merged 1 commit into from
Dec 2, 2021

Conversation

innocenzi
Copy link
Contributor

Sometimes, we want to perform an external redirect that can be initiated on the first visit or on an Inertia visit.

In such a case, Inertia's location won't work because it will only simply return a normal 409 response with the X-Inertia-Location, but since the front-end is not loaded the end result will be a blank page.

To fix this, we need to check if the current request is an Inertia one, perform the location redirect, or return a classic RedirectResponse otherwise.

This PR fixes that issue by performing that check internally so the developer doesn't have to.

This is technically a breaking change since I had to modify the original test, but it's a breaking change that is more likely to fix issues than to create some.


TL;DR: with this PR, instead of doing:

if (request()->inertia()) {
    return inertia()->location($url);
}

return redirect()->to($url);

We can just do:

return inertia()->location($url);

Fixes #295

@narasimhajupally
Copy link

Hello, I just checked the code changes for this issue and tried to do similar in my controller

if (Request::inertia()) {
       return Inertia::location(tenantRoute($email->domain->domain, 'home'));
}
return redirect(tenantRoute($email->domain->domain, 'home'));

I am getting this exception
ErrorException Cannot unbind $this of closure using $this
any idea ?
Thanks.

@claudiodekker claudiodekker merged commit 34deb1d into inertiajs:master Dec 2, 2021
@innocenzi innocenzi deleted the fix/location-redirect branch December 2, 2021 10:28
@claudiodekker
Copy link
Member

Thanks @innocenzi!

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.

Potential Inertia::location() enhancement?
3 participants