Skip to content

Commit f39b072

Browse files
authored
Fix manually reroute. (#3467)
Fix reroute.
1 parent d226706 commit f39b072

File tree

3 files changed

+60
-58
lines changed

3 files changed

+60
-58
lines changed

src/frontend/src/hooks.ts

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1 @@
1-
import { Reroute } from "@sveltejs/kit";
2-
import { WEBAUTHN_IFRAME_PATH } from "$lib/legacy/flows/iframeWebAuthn";
3-
import { getAddDeviceAnchor } from "$lib/utils/addDeviceLink";
4-
import { nonNullish } from "@dfinity/utils";
5-
import { DISCOVERABLE_PASSKEY_FLOW } from "$lib/state/featureFlags";
6-
import { get } from "svelte/store";
7-
import { building } from "$app/environment";
8-
import { goto, replaceState } from "$app/navigation";
9-
10-
export const reroute: Reroute = ({ url }) => {
11-
if (nonNullish(getAddDeviceAnchor(url))) {
12-
return "/register/device";
13-
}
14-
if (url.hash === WEBAUTHN_IFRAME_PATH) {
15-
return "/iframe/webauthn";
16-
}
17-
if (url.pathname.startsWith("/vc-flow")) {
18-
return "/vc-flow/index";
19-
}
20-
if (url.hash === "#authorize") {
21-
return get(DISCOVERABLE_PASSKEY_FLOW)
22-
? "/legacy-authorize-protocol"
23-
: "/legacy/authorize";
24-
}
25-
// Load direct authorization page instead if openid param is present
26-
if (url.pathname === "/authorize" && url.searchParams.has("openid")) {
27-
return `/direct-authorize-protocol`;
28-
}
29-
if (url.pathname === "/") {
30-
return get(DISCOVERABLE_PASSKEY_FLOW) || building ? "/" : "/legacy";
31-
}
32-
};
33-
34-
// SSG routes don't reroute by default anymore, so this method is used in
35-
// these routes to manually add the rerouting back as early as possible,
36-
// before hydration has even started (so intentionally outside onMount).
37-
export const manuallyReroute = async () => {
38-
if (!building) {
39-
const next = await reroute({
40-
url: new URL(window.location.href),
41-
fetch: window.fetch,
42-
});
43-
if (nonNullish(next)) {
44-
// Capture current URL
45-
const currentURL = new URL(window.location.href);
46-
// Cast to string since `nonNullish` doesn't exclude `void` type
47-
const nextURL = new URL(next as string, window.location.origin);
48-
// Copy over the current query params
49-
nextURL.search = currentURL.search;
50-
// Reroute to destination
51-
await goto(nextURL, { replaceState: true });
52-
// After rerouting, change the URL back to what the user expects to see
53-
replaceState(currentURL, {});
54-
}
55-
}
56-
};
1+
export { reroute } from "$lib/utils/reroute";
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import type { Reroute } from "@sveltejs/kit";
2+
import { WEBAUTHN_IFRAME_PATH } from "$lib/legacy/flows/iframeWebAuthn";
3+
import { getAddDeviceAnchor } from "$lib/utils/addDeviceLink";
4+
import { nonNullish } from "@dfinity/utils";
5+
import { DISCOVERABLE_PASSKEY_FLOW } from "$lib/state/featureFlags";
6+
import { get } from "svelte/store";
7+
import { building } from "$app/environment";
8+
import { goto, replaceState } from "$app/navigation";
9+
10+
export const reroute: Reroute = ({ url }) => {
11+
if (nonNullish(getAddDeviceAnchor(url))) {
12+
return "/register/device";
13+
}
14+
if (url.hash === WEBAUTHN_IFRAME_PATH) {
15+
return "/iframe/webauthn";
16+
}
17+
if (url.pathname.startsWith("/vc-flow")) {
18+
return "/vc-flow/index";
19+
}
20+
if (url.hash === "#authorize") {
21+
return get(DISCOVERABLE_PASSKEY_FLOW)
22+
? "/legacy-authorize-protocol"
23+
: "/legacy/authorize";
24+
}
25+
// Load direct authorization page instead if openid param is present
26+
if (url.pathname === "/authorize" && url.searchParams.has("openid")) {
27+
return `/direct-authorize-protocol`;
28+
}
29+
if (url.pathname === "/") {
30+
return get(DISCOVERABLE_PASSKEY_FLOW) || building ? "/" : "/legacy";
31+
}
32+
};
33+
34+
// SSG routes don't reroute by default anymore, so this method is used
35+
// there to manually add the rerouting back after the page has loaded.
36+
export const manuallyReroute = async () => {
37+
if (!building) {
38+
const next = await reroute({
39+
url: new URL(window.location.href),
40+
fetch: window.fetch,
41+
});
42+
if (nonNullish(next)) {
43+
// Capture current URL
44+
const currentURL = new URL(window.location.href);
45+
// Cast to string since `nonNullish` doesn't exclude `void` type
46+
const nextURL = new URL(next as string, window.location.origin);
47+
// Copy over the current query params
48+
nextURL.search = currentURL.search;
49+
// Reroute to destination
50+
await goto(nextURL, { replaceState: true });
51+
// After rerouting, change the URL back to what the user expects to see
52+
replaceState(currentURL, {});
53+
}
54+
}
55+
};

src/frontend/src/routes/(new-styling)/+page.svelte

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
II_DEVELOPER_DOCS_URL,
1313
} from "$lib/config";
1414
import LandingHeader from "$lib/components/layout/LandingHeader.svelte";
15-
import { manuallyReroute } from "../../hooks";
15+
import { manuallyReroute } from "$lib/utils/reroute";
1616
import { localeOptions, localeStore, t } from "$lib/stores/locale.store";
1717
import { Trans } from "$lib/components/locale";
1818
import {
@@ -26,7 +26,9 @@
2626
import Select from "$lib/components/ui/Select.svelte";
2727
2828
// Add rerouting back on this SSG route
29-
manuallyReroute();
29+
$effect(() => {
30+
manuallyReroute();
31+
});
3032
3133
let triggerAnimation =
3234
$state<(opts: FlairAnimationOptions) => Promise<void>>();

0 commit comments

Comments
 (0)