Support replacing route on redirect (#3072)

* Support replacing route on redirect

Support next/router `.replace` interface to change page without creating a
history entry.

* test_event: include test cases for new "replace" kwarg
This commit is contained in:
Masen Furer 2024-05-16 13:21:40 -07:00 committed by GitHub
parent 89352ac10e
commit bc6f0f70cb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 47 additions and 14 deletions

View File

@ -126,8 +126,13 @@ export const applyDelta = (state, delta) => {
export const applyEvent = async (event, socket) => { export const applyEvent = async (event, socket) => {
// Handle special events // Handle special events
if (event.name == "_redirect") { if (event.name == "_redirect") {
if (event.payload.external) window.open(event.payload.path, "_blank"); if (event.payload.external) {
else Router.push(event.payload.path); window.open(event.payload.path, "_blank");
} else if (event.payload.replace) {
Router.replace(event.payload.path);
} else {
Router.push(event.payload.path);
}
return false; return false;
} }

View File

@ -467,18 +467,27 @@ def server_side(name: str, sig: inspect.Signature, **kwargs) -> EventSpec:
) )
def redirect(path: str | Var[str], external: Optional[bool] = False) -> EventSpec: def redirect(
path: str | Var[str],
external: Optional[bool] = False,
replace: Optional[bool] = False,
) -> EventSpec:
"""Redirect to a new path. """Redirect to a new path.
Args: Args:
path: The path to redirect to. path: The path to redirect to.
external: Whether to open in new tab or not. external: Whether to open in new tab or not.
replace: If True, the current page will not create a new history entry.
Returns: Returns:
An event to redirect to the path. An event to redirect to the path.
""" """
return server_side( return server_side(
"_redirect", get_fn_signature(redirect), path=path, external=external "_redirect",
get_fn_signature(redirect),
path=path,
external=external,
replace=replace,
) )

View File

@ -158,12 +158,29 @@ def test_fix_events(arg1, arg2):
@pytest.mark.parametrize( @pytest.mark.parametrize(
"input,output", "input,output",
[ [
(("/path", None), 'Event("_redirect", {path:`/path`,external:false})'),
(("/path", True), 'Event("_redirect", {path:`/path`,external:true})'),
(("/path", False), 'Event("_redirect", {path:`/path`,external:false})'),
( (
(Var.create_safe("path"), None), ("/path", None, None),
'Event("_redirect", {path:path,external:false})', 'Event("_redirect", {path:`/path`,external:false,replace:false})',
),
(
("/path", True, None),
'Event("_redirect", {path:`/path`,external:true,replace:false})',
),
(
("/path", False, None),
'Event("_redirect", {path:`/path`,external:false,replace:false})',
),
(
(Var.create_safe("path"), None, None),
'Event("_redirect", {path:path,external:false,replace:false})',
),
(
("/path", None, True),
'Event("_redirect", {path:`/path`,external:false,replace:true})',
),
(
("/path", True, True),
'Event("_redirect", {path:`/path`,external:true,replace:true})',
), ),
], ],
) )
@ -174,11 +191,13 @@ def test_event_redirect(input, output):
input: The input for running the test. input: The input for running the test.
output: The expected output to validate the test. output: The expected output to validate the test.
""" """
path, external = input path, external, replace = input
if external is None: kwargs = {}
spec = event.redirect(path) if external is not None:
else: kwargs["external"] = external
spec = event.redirect(path, external=external) if replace is not None:
kwargs["replace"] = replace
spec = event.redirect(path, **kwargs)
assert isinstance(spec, EventSpec) assert isinstance(spec, EventSpec)
assert spec.handler.fn.__qualname__ == "_redirect" assert spec.handler.fn.__qualname__ == "_redirect"