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

feat: add replace option to loader redirect function #11100

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
11 changes: 11 additions & 0 deletions .changeset/five-bottles-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"@remix-run/router": minor
---

`redirect` now accepts a `replace` option in the same way as `navigate`.

```ts
loader: () => {
return redirect("/", { replace: true });
};
```
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
- bhbs
- bilalk711
- bobziroll
- Brendonovich
- BrianT1414
- brockross
- brookslybrand
Expand Down
9 changes: 7 additions & 2 deletions packages/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1796,7 +1796,9 @@ export function createRouter(init: RouterInit): Router {
revalidatingFetchers[redirect.idx - matchesToLoad.length].key;
fetchRedirectIds.add(fetcherKey);
}
await startRedirectNavigation(state, redirect.result, { replace });
await startRedirectNavigation(state, redirect.result, {
replace: replace || redirect.result.replace,
});
return { shortCircuited: true };
}

Expand Down Expand Up @@ -2118,7 +2120,9 @@ export function createRouter(init: RouterInit): Router {
revalidatingFetchers[redirect.idx - matchesToLoad.length].key;
fetchRedirectIds.add(fetcherKey);
}
return startRedirectNavigation(state, redirect.result);
return startRedirectNavigation(state, redirect.result, {
replace: redirect.result.replace,
});
}

// Process and commit output from loaders
Expand Down Expand Up @@ -4066,6 +4070,7 @@ async function callLoaderOrAction(
type: ResultType.redirect,
status,
location,
replace: result.headers.get("X-Remix-Redirect-Replace") !== null,
revalidate: result.headers.get("X-Remix-Revalidate") !== null,
reloadDocument: result.headers.get("X-Remix-Reload-Document") !== null,
};
Expand Down
6 changes: 5 additions & 1 deletion packages/router/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export interface RedirectResult {
location: string;
revalidate: boolean;
reloadDocument?: boolean;
replace?: boolean;
}

/**
Expand Down Expand Up @@ -1543,7 +1544,7 @@ export const defer: DeferFunction = (data, init = {}) => {

export type RedirectFunction = (
url: string,
init?: number | ResponseInit
init?: number | (ResponseInit & { replace?: boolean })
) => Response;

/**
Expand All @@ -1561,6 +1562,9 @@ export const redirect: RedirectFunction = (url, init = 302) => {
let headers = new Headers(responseInit.headers);
headers.set("Location", url);

if (typeof init === "object" && init.replace)
headers.set("X-Remix-Redirect-Replace", "true");

return new Response(null, {
...responseInit,
headers,
Expand Down