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

Add support for X-Remix-Reload-Document header #6842

Merged
merged 4 commits into from
Aug 22, 2023
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
2 changes: 1 addition & 1 deletion .changeset/wicked-points-unite.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
"@remix-run/server-runtime": minor
---

Re-export new `redirectDocument` method from React Router
Re-export new `redirectDocument` method from React Router ([#7040](https://github.com/remix-run/remix/pull/7040), [#6842](https://github.com/remix-run/remix/pull/6842))
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -573,4 +573,5 @@
- defjosiah
- AlemTuzlak
- ledniy
- robbtraister
- TrySound
49 changes: 49 additions & 0 deletions integration/redirects-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,41 @@ test.describe("redirects", () => {
return redirect("https://remix.run/");
}
`,

"app/routes/redirect-document.tsx": js`
import * as React from "react";
import { Outlet } from "@remix-run/react";

export default function Component() {
let [count, setCount] = React.useState(0);
let countText = 'Count:' + count;
return (
<>
<button onClick={() => setCount(count+1)}>{countText}</button>
<Outlet />
</>
);
}
`,

"app/routes/redirect-document._index.tsx": js`
import { Link } from "@remix-run/react";

export default function Component() {
return <Link to="/redirect-document/a">Link</Link>
}
`,

"app/routes/redirect-document.a.tsx": js`
import { redirectDocument } from "@remix-run/node";
export const loader = () => redirectDocument("/redirect-document/b");
`,

"app/routes/redirect-document.b.tsx": js`
export default function Component() {
return <h1>Hello B!</h1>
}
`,
},
});

Expand Down Expand Up @@ -230,4 +265,18 @@ test.describe("redirects", () => {
await page.waitForSelector(`#app:has-text("Page 2")`);
await page.waitForSelector(`#count:has-text("3")`);
});

test("supports hard redirects within the app via reloadDocument", async ({
page,
}) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/redirect-document", true);
expect(await app.getHtml("button")).toMatch("Count:0");
await app.clickElement("button");
expect(await app.getHtml("button")).toMatch("Count:1");
await app.waitForNetworkAfter(() => app.clickLink("/redirect-document/a"));
await page.waitForSelector(`h1`);
// Hard reload resets client side react state
expect(await app.getHtml("button")).toMatch("Count:0");
});
});
4 changes: 4 additions & 0 deletions packages/remix-react/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -258,5 +258,9 @@ function getRedirect(response: Response): Response {
if (revalidate) {
headers["X-Remix-Revalidate"] = revalidate;
}
let reloadDocument = response.headers.get("X-Remix-Reload-Document");
if (reloadDocument) {
headers["X-Remix-Reload-Document"] = reloadDocument;
}
Comment on lines 258 to +264
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brophdawg11 do you think it's worth refactoring this into an array + for-loop (or .forEach or .reduce)?

something like:

let HEADERS_TO_PRESERVE = ["X-Remix-Revalidate", "X-Remix-Reload-Document"];

...

  for (let headerName of HEADERS_TO_PRESERVE) {
    let headerValue = response.headers.get(headerName);
    if (headerValue) {
      headers[headerName] = headerValue;
    }
  }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nah not yet for the 2nd header - something something kent c dodds avoid hasty abstractions :)

return redirect(url, { status, headers });
}
Loading