Skip to content
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

Action in root.tsx not executed when pathless index route present #2498

Closed
styxlab opened this issue Mar 25, 2022 · 9 comments · Fixed by #4376
Closed

Action in root.tsx not executed when pathless index route present #2498

styxlab opened this issue Mar 25, 2022 · 9 comments · Fixed by #4376
Labels
bug Something isn't working

Comments

@styxlab
Copy link

styxlab commented Mar 25, 2022

What version of Remix are you using?

1.3.3

Steps to Reproduce

Install

git clone https://github.com/styxlab/remix-issue-root-action
cd remix-issue-root-action
yarn
yarn dev

Reproduce

Enter a text into the text field and press enter => Error: 405 Method Not Allowed

Route "routes/__layout" does not have an action, but you are trying to submit to it. To fix this, please add an `action` function to the route

Expected Behavior

No error. The action in root.tsx should be executed.

Actual Behavior

Error: 405 Method Not Allowed

@styxlab
Copy link
Author

styxlab commented Mar 25, 2022

Note:

  1. the action in root.tsx is triggered, when you remove the pathless layout route structure
  2. the console error message is misleading because actions in pathless routes are not allowed.

@kiliman
Copy link
Collaborator

kiliman commented Mar 25, 2022

Yeah, when Remix posts to a route, it looks for an action on that route. If you're on an index route, it will look at the parent layout for an action unless you include ?index in the form action. Looks like Remix isn't skipping pathless layouts as it looks for the "parent" layout route and just stops at the first parent.

@styxlab
Copy link
Author

styxlab commented Mar 26, 2022

Thanks for explaining what's going wrong here. From a DX point of view I would expect that the action is always triggered in the same location where I define the <Form> unless it is changed in the form action attribute.

@styxlab
Copy link
Author

styxlab commented Mar 26, 2022

Quote from the docs:

Also note that forms without an action prop (<Form method="post">) will automatically post to the same route within which they are rendered ...

Thus I hope this issue will be fixed.

@maranomynet
Copy link

maranomynet commented Jul 7, 2022

when Remix posts to a route, it looks for an action on that route. If you're on an index route, it will look at the parent layout for an action unless you include ?index in the form action.

This breaks the principle of least surprise.

IMO, matched routes should have their ActionFunction invoked regardless of whether the filename is index.tsx or something else.

If the index route doesn't export an ActionFunction, then look at the parent.

If both the index route AND the parent export an ActionFunction, prefer the one exported by the index route and log a warning in development mode.

@maranomynet
Copy link

Here this is mentioned in passing: https://remix.run/docs/en/v1/api/remix#form-action

@brophdawg11
Copy link
Contributor

I think this is the same bug as this issue over in react router. Pathless routes should be ignored when matching for actions, so /parent?index should hit the index route, and /parent should use the parent.jsx match, not a potential parent/__pathless.jsx file. Otherwise there's no way of differentiating between the parent route and it's child pathless routes.

@brophdawg11 brophdawg11 linked a pull request Oct 17, 2022 that will close this issue
2 tasks
@brophdawg11 brophdawg11 added bug Something isn't working and removed bug:unverified labels Oct 17, 2022
@github-actions
Copy link
Contributor

🤖 Hello there,

We just published version v0.0.0-nightly-9868b20-20221021 which involves this issue. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

@machour
Copy link
Collaborator

machour commented Jan 22, 2023

Should be fixed now that Remix sits on top of RR.

@machour machour closed this as completed Jan 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants