-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Form
post submission does not retain search parameters
#3133
Comments
consider this scenario I have a site with login functionality. There are routes "/" and "/settings" and to access these routes, the user should be authenticated. The site is hosted on xyz.com
|
@manV Yes, fixing this issue will solve your scenario. You can "fix" it now, by manually specifying your action. const location = useLocation()
return <Form action={`${location.pathname}${location.search}`}/> The actual fix is to essentially do this automatically if action is not specified, as that is standard browser behavior. |
I think problem lies in As of now code looks like this export function useFormAction(
action = ".",
// TODO: Remove method param in v2 as it's no longer needed and is a breaking change
method: FormMethod = "get"
): string {
let { id } = useRemixRouteContext();
let path = useResolvedPath(action);
let search = path.search;
let isIndexRoute = id.endsWith("/index");
if (action === "." && isIndexRoute) {
search = search ? search.replace(/^\?/, "?index&") : "?index";
}
return path.pathname + search;
} According to docs for
I think what that means is that only resolves only {hash: ""
pathname: "/login"
search: ""} even when the At commit a3ae536 the code was changed from export function useFormAction(action = "."): string {
let path = useResolvedPath(action);
let location = useLocation();
return path.pathname + location.search;
} to this export function useFormAction(action = "."): string {
let { id } = useRemixRouteContext();
let path = useResolvedPath(action);
let search = path.search;
let isIndexRoute = id.endsWith("/index");
if (isIndexRoute && action === ".") {
search = "?index";
}
return path.pathname + search;
} As you can see previously we have used only let search = path.search; to this will work let search = useLocation().search; it did, but now I am getting whole new error
Now I have no idea on what this error is or where it is originating from and therefore I have no idea on how to solve it. |
Did you, by any chance, modified the transpiled Remix code in Now, from what I can tell, the issue with the The solution would look like this: export function useFormAction(
action = ".",
// TODO: Remove method param in v2 as it's no longer needed and is a breaking change
method: FormMethod = "get"
): string {
let { id } = useRemixRouteContext();
let path = useResolvedPath(action);
let search = path.search;
let isIndexRoute = id.endsWith("/index");
+ let location = useLocation();
+
+ if (action === ".") {
+ search = location.search
+ }
if (action === "." && isIndexRoute) {
search = search ? search.replace(/^\?/, "?index&") : "?index";
}
return path.pathname + search;
} E.g.
|
@ionut-botizan yeah you are correct, I cloned the remix repo locally and changed the code in On your code, if we explicit set the if we assume the behaviour of So code should look like this /**
* Resolves a `<form action>` path relative to the current route.
*
* @see https://remix.run/api/remix#useformaction
*/
export function useFormAction(
action?: string,
// TODO: Remove method param in v2 as it's no longer needed and is a breaking change
method: FormMethod = "get"
): string {
let { id } = useRemixRouteContext();
let path = useResolvedPath(action ? action : ".");
let search = path.search;
let isIndexRoute = id.endsWith("/index");
let location = useLocation();
if (action === undefined) {
search = location.search;
}
if ((action === undefined || action === ".") && isIndexRoute) {
search = search ? search.replace(/^\?/, "?index&") : "?index";
}
return path.pathname + search;
} And we also have to change let FormImpl = React.forwardRef<HTMLFormElement, FormImplProps>(
(
{
reloadDocument = false,
replace = false,
method = "get",
action,
encType = "application/x-www-form-urlencoded",
fetchKey,
onSubmit,
...props
},
forwardedRef
) {...} |
That was from long ago 😒 |
Is there any workaround? I tried the one from @kiliman but then I get a |
If you're posting to an index route, then make sure you append |
This was good to me, haha. Any chance Remix could make this more ergonomic? : ) 🤞🏼 |
the latest version(1.6.8) already fixed the issue. |
I'm just closing this as stale, I'm pretty sure it was fixed but haven't had any confirmation from the team. If it's not we can re-open |
@TheRealFlyingCoder we're on 1.9.0 using a GET form submission, and the search/query params are still lost. Perhaps this is in conjunction with
package.json
|
@uhrohraggy a submission on a form using the |
It seems that in Remix v2, the `<Form>` component is able to POST to the correct _implicit_ route on its own. So we can remove our usages of `useLocation` now. We can stop watching remix-run/remix#3133 for now.
It's 2024, and this issue still exists. There should be an option within the Form to control whether existing search queries are removed or not. |
What version of Remix are you using?
1.4.3
Steps to Reproduce
Impement a route that includes search params in the url.
/my-route?param=1234
On the page include a post form
Native browser behaviour can be seen here thanks to @kiliman
https://codesandbox.io/s/form-action-params-8r86qw?file=/app/routes/test.tsx:732-987
Expected Behavior
Following the native browser behaviour, this should post back to
/my-route?param=1234
Note that a
method="get"
post would natively replace the search params by the nature of the submission methodActual Behavior
The form will post to the base path
/my-route
and trim off the search paramsThe text was updated successfully, but these errors were encountered: