Skip to content

Commit 79c7df4

Browse files
committed
feat: update back button logic
Instead of saving back url in the param, save the back url info inside session.
1 parent 3542917 commit 79c7df4

4 files changed

Lines changed: 40 additions & 14 deletions

File tree

src/components/BackButton.astro

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,19 @@ import { SITE } from "@/config";
1919
)
2020
}
2121

22-
<script is:inline data-astro-rerun>
22+
<script>
2323
/* Update Search Praam */
2424
function updateGoBackUrl() {
25-
const backButton = document.querySelector("#back-button");
26-
const params = new URL(document.location.toString()).searchParams;
25+
const backButton: HTMLAnchorElement | null =
26+
document.querySelector("#back-button");
2727

28-
if (params.has("from")) {
29-
backButton.href = params.get("from");
28+
const backUrl = sessionStorage.getItem("backUrl");
29+
30+
if (backUrl && backButton) {
31+
backButton.href = backUrl;
3032
}
3133
}
34+
35+
document.addEventListener("astro:page-load", updateGoBackUrl);
3236
updateGoBackUrl();
3337
</script>

src/layouts/Main.astro

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
import Breadcrumb from "@/components/Breadcrumb.astro";
3+
import { SITE } from "@/config";
34
45
interface StringTitleProp {
56
pageTitle: string;
@@ -15,10 +16,16 @@ interface ArrayTitleProp {
1516
export type Props = StringTitleProp | ArrayTitleProp;
1617
1718
const { props } = Astro;
19+
20+
const backUrl = SITE.showBackButton ? Astro.url.pathname : "/";
1821
---
1922

2023
<Breadcrumb />
21-
<main id="main-content" class="mx-auto w-full max-w-3xl px-4 pb-4">
24+
<main
25+
data-backUrl={backUrl}
26+
id="main-content"
27+
class="mx-auto w-full max-w-3xl px-4 pb-4"
28+
>
2229
{
2330
"titleTransition" in props ? (
2431
<h1 class="text-2xl font-semibold sm:text-3xl">
@@ -34,3 +41,14 @@ const { props } = Astro;
3441
<p class="mt-2 mb-6 italic">{props.pageDesc}</p>
3542
<slot />
3643
</main>
44+
45+
<script>
46+
document.addEventListener("astro:page-load", () => {
47+
const mainContent: HTMLElement | null =
48+
document.querySelector("#main-content");
49+
const backUrl = mainContent?.dataset?.backurl;
50+
if (backUrl) {
51+
sessionStorage.setItem("backUrl", backUrl);
52+
}
53+
});
54+
</script>

src/pages/archives/index.astro

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ const months = [
2929
"November",
3030
"December",
3131
];
32-
33-
const backUrl = SITE.showBackButton ? `?from=${Astro.url.pathname}` : "/";
3432
---
3533

3634
<Layout title={`Archives | ${SITE.title}`}>
@@ -72,10 +70,7 @@ const backUrl = SITE.showBackButton ? `?from=${Astro.url.pathname}` : "/";
7270
)
7371
)
7472
.map(({ data, id }) => (
75-
<Card
76-
href={`/posts/${id}${backUrl}`}
77-
frontmatter={data}
78-
/>
73+
<Card href={`/posts/${id}`} frontmatter={data} />
7974
))}
8075
</ul>
8176
</div>
@@ -84,6 +79,5 @@ const backUrl = SITE.showBackButton ? `?from=${Astro.url.pathname}` : "/";
8479
))
8580
}
8681
</Main>
87-
8882
<Footer />
8983
</Layout>

src/pages/index.astro

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const recentPosts = sortedPosts.filter(({ data }) => !data.featured);
2222

2323
<Layout>
2424
<Header />
25-
<main id="main-content">
25+
<main id="main-content" data-layout="index">
2626
<section id="hero" class="pt-8 pb-6">
2727
<h1 class="my-4 inline-block text-4xl font-bold sm:my-8 sm:text-5xl">
2828
Mingalaba
@@ -119,3 +119,13 @@ const recentPosts = sortedPosts.filter(({ data }) => !data.featured);
119119
</main>
120120
<Footer />
121121
</Layout>
122+
123+
<script>
124+
document.addEventListener("astro:page-load", () => {
125+
const indexLayout = (document.querySelector("#main-content") as HTMLElement)
126+
?.dataset?.layout;
127+
if (indexLayout) {
128+
sessionStorage.setItem("backUrl", "/");
129+
}
130+
});
131+
</script>

0 commit comments

Comments
 (0)