Search code examples
laravelsubdomaininertiajs

Subdomain routing not working in Laravel 9 and Inertia


I'm working on a Larave, Inertiajs and Vue project where I want every product to open on a route like this https://{user:username}.example.test/{product:slug}

So basically I need my website to run on example.test and any product to show on username.example.test/productSlug

I had an issue where I'm getting CORS errors and I wrote a thread here Laravel Inertia apps doesn't support subdomain redirects - No 'Access-Control-Allow-Origin'

Unfortunately I didn't get the help I needed here but after hours of searching I found this on stackoverflow which have helped me to finally succeed on making the redirect to the correct URL works but the problem is that It doesn't really work.

To explain this:

The controller store action finishes what It's supposed to do and then redirects me to a working page of my product without cors errors, the only problem is that it shows in the browser as example.test/productSlug while it's supposed to be username.example.test/productSlug. When I refresh the page when I'm on that incorrect url It gets me page not found error which make sense because the actual correct route contains my desired page.

When I tracked the request on firefox I could see that the host is correct (username.example.test) but the url I'm reaching is 'example.test', also when I'm redirected to that incorrect route I could see my product and everything works just fine but it's not a working url because when I refresh It gives me page not found and when I manually write the correct url username.example.test/productSlug It directs me to a working page of my product.

I hope you're not confused.

This is the redirect line in my ProductsController@store:

public function store(StoreProductRequest $request)
    {
        
        // Code
        
        return redirect()->route('products.show', [$user, $product]);
    }

This is how my route looks like:

Route::domain('{user:username}.' . env('APP_URL'))->group(function () {

    Route::get('{product:slug}', [ProductController::class, 'show'])->name('products.show');
    
});

And this is how I show the product:

    public function show(User $user, Product $product)
    {
        return Inertia::render('Products/Show', [
            'user' => $user,
            'product' => $product,
            'thumbnails' => $product->productimages
        ]);
    }

I made those changes to cors.php:

    'paths' => ['api/*', '*', 'sanctum/csrf-cookie'],

    'allowed_methods' => ['*'],

    'allowed_origins' => ['*'],

    'allowed_origins_patterns' => [],

    'allowed_headers' => ['*'],

    'exposed_headers' => ['x-inertia'],

    'max_age' => 0,

    'supports_credentials' => false,

Solution

  • After days of research I'm sure now that it is impossible to route redirect to a subdomain route from root domain without full page refresh. Any change to the host will require the page to reload which is out of Laravel and JetStream scope and out of InertiaJS hands. As far as I understand that this is for security purposes. See more here https://github.com/inertiajs/inertia-laravel/issues/431