inertiajs / inertia

Inertia.js lets you quickly build modern single-page React, Vue and Svelte apps using classic server-side routing and controllers.
https://inertiajs.com
MIT License
6.02k stars 405 forks source link

[REQUEST] Router: make replace url optional #1861

Closed didix16 closed 2 months ago

didix16 commented 2 months ago

Discussed in https://github.com/inertiajs/inertia/discussions/1860

Originally posted by **didix16** April 18, 2024 Hello there! I'm using router.post() to send FormData (from '/' endpoint) to a POST endpoint Controller which does not have GET method, only POST. So when I use router.post(/post-only-endpoint), after sending the form, the browser URL changes to that URL (which I don't want to) and then if I have to send again the form and lets say that the $request->validate() aborts, then a pop up shows saying that I cannot do a GET request to that endpoint: (because now I'm sending the request from /post-only-endpoint) ![imagen](https://github.com/inertiajs/inertia/assets/10291092/3eb3551c-83dd-4c68-8ac6-c0cd1494bf9b) I know I can use axios or fetch but I want the integration that offers Inertia to update the component properties and error settings on every router.visit action So I propose to add a variable in router.visit() method, called **followUrl** (by default to true) which if you set it to false, on setPage method https://github.com/inertiajs/inertia/blob/ca7e7cbcd7f66bd37bc36741b5d3fcb9979db020/packages/core/src/router.ts#L457 allows to call replaceState/pushState like this: ```js replace ? this.replaceState({...page, url: followUrl ? page.url : window.location.href}) : this.pushState({...page, url: followUrl ? page.url : window.location.href}) ``` And thus, keep the url browser untouched. Sorry for my bad english.I hope the message is understood. Thanks in advance!
RobertBoes commented 2 months ago

You should ideally use a redirect or something. Displaying a view on a POST url would indeed cause these issues. Inertia is a router after all, not an API client.

driesvints commented 2 months ago

Thanks @RobertBoes.

Also @didix16 please don't re-open discussions as issues.