inertiajs / inertia-laravel

The Laravel adapter for Inertia.js.
https://inertiajs.com
MIT License
2.08k stars 234 forks source link

Help: using different apps Frontend/Admin with innertia.js #114

Closed Neophen closed 4 years ago

Neophen commented 4 years ago

Use case: My setup i'm building two completely different apps within the same laravel I have separate mix.js files and separate tailwind config files. and i use laravel-mix-manifest merge to merge all of the built stuff into one manifest.

Trying to use $inertia.visit to change the apps does not work. Since it tries to look for the page component in the previous app, eg:

  1. from admin to frontent: looks for page component in admin app
  2. and vice versa.

The hack is to simply use a normal link or window.location.href The only thing is trying to catch all the way you can go from one app to the other is difficult.

I though that Inertia::setRootView('tenant-admin.app'); would do the trick, but it does not as while it sets the root view ('checked in the Inertia core and logged it out) that doesn't mean anything to the visit request as it just gets a json response.

Questions:

  1. is this intended behaviour?
  2. what is the correct way to approach having two apps, different css different vue setup, etc.

Any help is greatly appreciated.

rodrigopedra commented 4 years ago

@Neophen , just want to share my experience.

I have a very similar setup, one app (with its own mix config, css, etc) to the public frontend and one for the admin frontend.

In some cases I also need to link a resource from the admin to the public frontend. For that I use regular anchor tags (<a href></a>)

I am not a maintainer of inertia, but I guess this is intended behavior. By design after the first request, which knows which blade template to use and bootstrap the inertia app, all the next requests would respond with a JSON payload with the changes needed.

In the case you described, as the navigation is switching blade templates, it would need either way to bootstrap the new app all over again. Basically the same behavior you get with a regular anchor tag. For me it makes sense as it navigating trough two different apps, we could think of them as separated sites, in that case you would need to use a regular anchor tag either way.

I guess, if you want to use <InertiaLink /> you could circumvent it in the backend by responding with a 409 status code and a X-Inertia-Location header. Similar to what Inertia already does when you have the assets versioning feature in place:

https://github.com/inertiajs/inertia-laravel/blob/e95b09e188d27b633869290f6fa0f71b4d7095b9/src/Middleware.php#L19-L25

In the frontend if the response has that status code and header, inertia will hard redirect to the new location instead of trying to hydrate the app with the expected JSON response.

But, in my opinion this is more work, and also add one more check to each request, than just using a regular anchor tag when switching applications.

Hope it helps anyhow.

Neophen commented 4 years ago

Yup makes sense. I wish this was supported out of the box. And awesome i've hacked together some things and now this looks amazeballs!

lukaskleinschmidt commented 3 years ago

I had the same issue and solved it like this. Perhaps this helps someone else. https://gist.github.com/lukaskleinschmidt/714435e77e7b7a46f5960acaee54153a