Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Randy_Johnson's avatar

Inertia XHR POST also gives a GET

I set my application up in such a way that I wanted to put the least amount of stress on the server and have the front end do all the heavy lifting.

In my app when making a POST request, it also responses with a GET. How do I stop this GET.

0 likes
11 replies
Randy_Johnson's avatar

Wait, is the GET the server responding to say that everything is successful.

azimidev's avatar

What do you mean

it also responses with a GET. How do I stop this GET

Are you sure you are sending POST request?

1 like
Randy_Johnson's avatar

@azimidev

 function submit(e) {
        e.preventDefault();
        handleAddToCart(item)
        data.id = item.id;
        setShow(false);
        post(route('item.store'), {preserveScroll: true});
    }

The browser on the network tab returns.

POST
	
scheme
	http
host
	127.0.0.1:8000
filename
	/item

and then

GET
	http://127.0.0.1:8000/dashboard/products
Status
200
OK
VersionHTTP/1.1
Transferred8.69 kB (7.55 kB size)
Referrer Policystrict-origin-when-cross-origin
Request PriorityHighest

The dashboard/products is my Page with the components inside.

Randy_Johnson's avatar

@azimidev

    public function store(StoreItemRequest $request)
    {
        $validated = $request->validate([
            'id' => 'required|min:1|max:5',
        ]);

        $cart = Cart::where('id', '=', Auth::user()->id)->first();

        $item = Item::where('product_id', '=', $validated['id'])->first();


        if (!empty($item)) {
            $item->amount++;
            $item->save();
        } else {
            $nitem = new Item;
            $nitem->cart_id = $cart->id;
            $nitem->product_id = $validated['id'];
            $nitem->amount = 1;
            $nitem->save();
        }
    }
Sinnbeck's avatar

@Randy_Johnson Inertia automatically returns a redirect if you return nothing (and a white screen if you return blank). This was added the old way was to always return the white page

You should just not use inertia for this :) Do plain ajax requests with axios

It sounds a bit like you are doing a lot of work to work around inertia. Maybe using it was a wrong choice? You can use react without using inertia at all. I have done so in the past

2 likes
Randy_Johnson's avatar

yes, I really need to stop fighting with Inertia, its making me sick 😆. But in all honesty could I argue that saving these small get request could save potential server processing power, and in a large scale application which has thousands of users, this would potentially end up saving the company money?

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@Randy_Johnson In theory maybe a little. But I dont think it will ever be that big an issue. Laravel can handle ALOT of requests, and scaling a bit isnt that expensive. And the time you spend doing all of these workarounds is most likely not worth it in the long run :) Finally if you every scale to enterprice level, chances are you are going to be rewriting a lot of the code anyways.

What will really save money performance wise, is optiizing queries, adding index etc. The database (though very very fast) is often the bottleneck, if not used properly.

1 like

Please or to participate in this conversation.