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

fix(remix-react): preserve ?index for fetcher get submissions #4238

Merged
merged 3 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sour-roses-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/react": patch
---

fix: preserve ?index for fetcher get submissions (#4238)
90 changes: 90 additions & 0 deletions integration/fetcher-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ test.describe("useFetcher", () => {

let CHEESESTEAK = "CHEESESTEAK";
let LUNCH = "LUNCH";
let PARENT_LAYOUT_LOADER = "parent layout loader";
let PARENT_LAYOUT_ACTION = "parent layout action";
let PARENT_INDEX_LOADER = "parent index loader";
let PARENT_INDEX_ACTION = "parent index action";

test.beforeAll(async () => {
fixture = await createFixture({
Expand Down Expand Up @@ -80,6 +84,65 @@ test.describe("useFetcher", () => {
);
}
`,

"app/routes/parent.jsx": js`
import { Outlet } from "@remix-run/react";

export function action() {
return "${PARENT_LAYOUT_ACTION}";
};

export function loader() {
return "${PARENT_LAYOUT_LOADER}";
};

export default function Parent() {
return <Outlet />;
}
`,

"app/routes/parent/index.jsx": js`
import { useFetcher } from "@remix-run/react";

export function action() {
return "${PARENT_INDEX_ACTION}";
};

export function loader() {
return "${PARENT_INDEX_LOADER}";
};

export default function ParentIndex() {
let fetcher = useFetcher();

return (
<>
<pre>{fetcher.data}</pre>
<button id="load-parent" onClick={() => fetcher.load('/parent')}>
Load parent
</button>
<button id="load-index" onClick={() => fetcher.load('/parent?index')}>
Load index
</button>
<button id="submit-empty" onClick={() => fetcher.submit({})}>
Submit empty
</button>
<button id="submit-parent-get" onClick={() => fetcher.submit({}, { method: 'get', action: '/parent' })}>
Submit parent
</button>
<button id="submit-index-get" onClick={() => fetcher.submit({}, { method: 'get', action: '/parent?index' })}>
Submit index
</button>
<button id="submit-parent-post" onClick={() => fetcher.submit({}, { method: 'post', action: '/parent' })}>
Submit parent
</button>
<button id="submit-index-post" onClick={() => fetcher.submit({}, { method: 'post', action: '/parent?index' })}>
Submit index
</button>
</>
);
}
`,
},
});

Expand Down Expand Up @@ -142,4 +205,31 @@ test.describe("useFetcher", () => {
await app.clickElement("#fetcher-submit");
expect(await app.getHtml("pre")).toMatch(CHEESESTEAK);
});

test("fetchers handle ?index param correctly", async ({ page }) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/parent");

await app.clickElement("#load-parent");
expect(await app.getHtml("pre")).toMatch(PARENT_LAYOUT_LOADER);

await app.clickElement("#load-index");
expect(await app.getHtml("pre")).toMatch(PARENT_INDEX_LOADER);

// fetcher.submit({}) defaults to GET for the current Route
await app.clickElement("#submit-empty");
expect(await app.getHtml("pre")).toMatch(PARENT_INDEX_LOADER);

await app.clickElement("#submit-parent-get");
expect(await app.getHtml("pre")).toMatch(PARENT_LAYOUT_LOADER);

await app.clickElement("#submit-index-get");
expect(await app.getHtml("pre")).toMatch(PARENT_INDEX_LOADER);

await app.clickElement("#submit-parent-post");
expect(await app.getHtml("pre")).toMatch(PARENT_LAYOUT_ACTION);

await app.clickElement("#submit-index-post");
expect(await app.getHtml("pre")).toMatch(PARENT_INDEX_ACTION);
});
});
10 changes: 10 additions & 0 deletions packages/remix-react/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,16 @@ export function useSubmitImpl(key?: string): SubmitFunction {
throw new Error(`Cannot submit binary form data using GET`);
}
}

// Preserve any incoming ?index param for fetcher GET submissions
let isIndexAction = new URLSearchParams(url.search)
.getAll("index")
.some((v) => v === "");
if (key != null && isIndexAction) {
hasParams = true;
params.append("index", "");
}

url.search = hasParams ? `?${params.toString()}` : "";
}

Expand Down