Comments (21)
@opanegro You no longer need to do this manually. The Laravel adapter now has a method for this, called Inertia::location
. For example:
class LoginController extends Controller
{
public function showLoginForm()
{
if (Request::inertia()) {
return Inertia::location(url()->current());
}
return view('auth.login');
}
}
More on that here.
from inertia-laravel.
Here is another interesting use case for this feature.
Consider if you have a page in your app that isn't an Inertia page, but you somehow end up redirecting to that page via an Inertia request. You can use the asset conflict handling to force a full page load to these pages.
Something like this:
class LoginController extends Controller
{
public function showLoginForm()
{
if (Request::inertia()) {
return response('', 409)
->header('X-Inertia-Location', url()->current());
}
return view('auth.login');
}
}
from inertia-laravel.
This is brilliant!
One possible addition to the Laravel example in the Inertia documentation is that this can be implemented as middleware and then quite easily applied to multiple routes.
It makes it easy to then set apart routes that you know will never return an inertia response from those that will. It also might help with apps where devs want to implement Inertia into Laravel progressively.
Middleware like this:
class NonInertiaRoutes
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
if ($request->inertia()) {
return Inertia::location($request->fullUrl());
}
return $next($request);
}
}
Allows you to wrap routes like this:
// Non Inertia route for redirects to GrapesJS pages
Route::middleware(['non-inertia'])->group(function () {
Route::get('/', [PageController::class, 'show']);
});
I had an issue using Laravel Breeze set up for Vue.js alongside laravel-grapesjs where if you visited a page to be edited by grapesjs before logging in (for example after session expiry) then you were redirected to login page and then again redirected to the edit route. But the edit route was not going to return an Inertia response so I would get the modal.
Alternatively if I logged out and wanted to be redirected to a grapesjs created page I would have the same issue.
In both cases I did not have control of the link that sent users to the URL as they were redirects - so this came in handy when trying to 'pop-out' of inertia for those specific routes.
from inertia-laravel.
I think I might have another (similar) use case for this. My app has to authenticate at Shopify which involves a redirect offsite to do an oauth handshake. I therefore have to have my login form be a plain old POST form with an action and a hidden crsf token field.
This all works fine but the problem occurs when a session times out. I'm redirected to my login screen as expected on session expiry but a subsequent attempt to login results in a 419 token mismatch. This makes sense as there needs to be a full page refresh to get a new session token.
I've tried the solution in the snippet above and it works though it would be great to have a hardVisit
solution that doesn't trigger a browser console error and if possible otherwise allows us to use the same Inertia API e.g.
public function showLoginForm()
{
if (Request::inertia()) {
return Inertia::hardVisit('Pages/Login')
}
return Inertia::render('Pages/Login')
}
Edit: on reflection I'm not really sure the above snippet makes much sense (or at least it might be a request for slightly different problem). I guess what I'm wondering is whether there could be a way of forcing an inertia view to hard reload under certain circumstances?
from inertia-laravel.
@danrichards Hey there! So, this feature is already on the redirects page: https://inertiajs.com/redirects#external-redirects
from inertia-laravel.
Thanks @sebastiandedeyne I was going nuts with the logout.
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
if (!function_exists('redirectWithoutInertia')) {
function redirectWithoutInertia(string $url)
{
return response('', SymfonyResponse::HTTP_CONFLICT)->header('x-inertia-location', $url);
}
}
from inertia-laravel.
I would love to see this feature as well. Building an inertia checkout code and need to have an ability to redirect the user to external payment gateway.
from inertia-laravel.
We added this little helper function in our app, but it feels hacky: ...
A rails version of the above workaround, for rails users come here via google search
# application_controller.rb
# Use `409 Conflict` to force client <InertiaApp> to go to +url+ with a full-page refresh
def redirect_without_inertia(url:)
headers['X-Inertia-Location'] = url
head :conflict
end
from inertia-laravel.
Inertia::location(url()->current());
I would suggest a mention under Manual Visits
or Redirects
in the docs. I wish I found this faster.
Thank you
from inertia-laravel.
Yep, I like it. I've had a use-case for this once when redirecting to a 3rd party auth provider.
Not sure on two things:
- If using the conflict HTTP code is right for this use case. I almost wonder if we should have some type of Inertia "hard visit" response that's proper JSON.
- If
forceRedirect
is the right language. Right now it's called a "hard visit". Maybe it should beInertia::hardVisit($url)
? Admittedly that isn't as obvious as the word redirect.
from inertia-laravel.
I have come across the use-case where logout gets redirected to a normal blade rendered page in laravel. The option 2 will be very handy.
from inertia-laravel.
I've also had to use this, both with logout links & Socialite redirects. Would be really handy to have in core
from inertia-laravel.
I had an issue where I had to throw an exception in the Authenticate
middleware since it doesn't allow for setting headers.
class Authenticate extends Middleware
{
protected function redirectTo($request)
{
if (! $request->expectsJson() && Request::inertia()) {
abort(409, '', ['X-Inertia-Location' => url()->route('login')]);
} else if (! $request->expectsJson()) {
return route('login');
}
}
}
from inertia-laravel.
Not sure [...] If forceRedirect is the right language. Right now it's called a "hard visit". Maybe it should be Inertia::hardVisit($url)? Admittedly that isn't as obvious as the word redirect.
How about hardRedirect()
?
I had an issue where I had to throw an exception in the Authenticate middleware since it doesn't allow for setting headers.
@Livijn You could just override the unauthenticated()
method from the parent class.
Edit: I'm wrong - the value from that method isn't returned as I first thought - but I think the Illuminate\Foundation\Exceptions\Handler::unauthenticated()
method could be overridden in App\Exception\Handler
.
from inertia-laravel.
I recently update the deps from v0.2.6
to v0.2.7
and using SymfonyResponse::HTTP_CONFLICT
no longer working as expected.
from inertia-laravel.
Here is another interesting use case for this feature.
Consider if you have a page in your app that isn't an Inertia page, but you somehow end up redirecting to that page via an Inertia request. You can use the asset conflict handling to force a full page load to these pages.
Something like this:
class LoginController extends Controller { public function showLoginForm() { if (Request::inertia()) { return response('', 409) ->header('X-Inertia-Location', url()->current()); } return view('auth.login'); } }
thanks it works
from inertia-laravel.
well done, thanks @reinink
from inertia-laravel.
wow thanks @reinink
from inertia-laravel.
Amazing work, man! Amazing work! @reinink
from inertia-laravel.
this is my solution for that in the front i check the response is not a valid inertia response and is a 200 status then I take the URL from the response and I make a redirection with window.location
Inertia.on("invalid", (event) => {
if (event.detail.response.status == 200) {
event.preventDefault();
window.location.href = event.detail.response.request.responseURL;
}
});
from inertia-laravel.
This is brilliant!
One possible addition to the Laravel example in the Inertia documentation is that this can be implemented as middleware and then quite easily applied to multiple routes.
It makes it easy to then set apart routes that you know will never return an inertia response from those that will. It also might help with apps where devs want to implement Inertia into Laravel progressively.
Middleware like this:
class NonInertiaRoutes { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse */ public function handle(Request $request, Closure $next) { if ($request->inertia()) { return Inertia::location($request->fullUrl()); } return $next($request); } }
Allows you to wrap routes like this:
// Non Inertia route for redirects to GrapesJS pages Route::middleware(['non-inertia'])->group(function () { Route::get('/', [PageController::class, 'show']); });
I had an issue using Laravel Breeze set up for Vue.js alongside laravel-grapesjs where if you visited a page to be edited by grapesjs before logging in (for example after session expiry) then you were redirected to login page and then again redirected to the edit route. But the edit route was not going to return an Inertia response so I would get the modal.
Alternatively if I logged out and wanted to be redirected to a grapesjs created page I would have the same issue.
In both cases I did not have control of the link that sent users to the URL as they were redirects - so this came in handy when trying to 'pop-out' of inertia for those specific routes.
thanks @bluekable
from inertia-laravel.
Related Issues (20)
- Release notes for 1.0 could make it more clear that "Fix URL generation (#592)" is a breaking change HOT 5
- Ziggy2.0 and Laravel11 and Breeze for Inertia + React, TypeScript and SSR starter kit throws error on initial setting HOT 2
- assertInertia() fails when testing Inertia visits HOT 1
- Inertia components/hooks wrapped with redux Provider stopped working HOT 2
- client-side validation libraries HOT 1
- SSR high CPU usage HOT 1
- Serve Connection Reset HOT 1
- Default Layout HOT 1
- adding to HandleInertiaRequests->share array no longer works in version 1.2.0 HOT 5
- Running inertia:start-ssr continuosly fails with message 'Node.js v18.19.0' HOT 2
- Popups instead of redirects due to the first response to API request is randomly returned as "200 text/html" HOT 1
- Partial reload behaviour change in 1.1.0 causes lazy attributes not to be reloaded correctly HOT 2
- PreserveScroll not working without PreserveState for inertia form HOT 1
- How to update favicon, meta tags like descripiotn, keywords, openGraph(og) etc. HOT 1
- Using inertia component inside blade file HOT 1
- inertia returning encoded html in output for data-page HOT 5
- Problem with URL generation HOT 2
- SSR Not Rendering Content After Changing Default Port HOT 3
- [Feature] Does Inertia-Laravel support Nuxt.js? If not, will it in the future? HOT 1
- SSR rendering errors HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from inertia-laravel.