Skip to content

Commit 0b76d99

Browse files
committed
actually fix back button this time
1 parent 65f5231 commit 0b76d99

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
<script lang="ts">
2+
import { beforeNavigate } from "$app/navigation";
3+
4+
let lastPathname: string | null = null;
5+
6+
beforeNavigate(({ from, to }) => {
7+
if (from?.url && to?.url) {
8+
lastPathname = from.url.pathname;
9+
sessionStorage.setItem("lastPathname", lastPathname);
10+
}
11+
});
12+
</script>
13+
114
<div class="layout-container">
215
<slot />
316
</div>

src/routes/AppHeader.svelte

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@
2626
let notifications: NotificationGroup[] | undefined = undefined;
2727
2828
let pageTitle = getContext<Writable<string>>("pageTitle");
29+
30+
const handleBack = () => {
31+
const lastPathname = sessionStorage.getItem("lastPathname");
32+
const currentPathname = $page.url.pathname;
33+
34+
if (!lastPathname || lastPathname === currentPathname) {
35+
goto("/app/home");
36+
} else {
37+
goto(lastPathname);
38+
}
39+
};
2940
</script>
3041

3142
<header
@@ -35,12 +46,7 @@
3546
<div class="w-16">
3647
<!-- svelte-ignore a11y_consider_explicit_label -->
3748
<button
38-
on:click={canGoBack
39-
? () =>
40-
window.history.length > 1
41-
? window.history.back()
42-
: goto("/app/home")
43-
: undefined}
49+
on:click={canGoBack ? handleBack : undefined}
4450
class:opacity-0={!canGoBack}
4551
class="-m-4 p-4"
4652
>

0 commit comments

Comments
 (0)