feat: update back button logic

Instead of saving back url in the param, save the
back url info inside session.
This commit is contained in:
satnaing
2025-03-06 21:03:31 +07:00
committed by Sat Naing
parent 35429176e0
commit 79c7df45e5
4 changed files with 40 additions and 14 deletions
+9 -5
View File
@@ -19,15 +19,19 @@ import { SITE } from "@/config";
) )
} }
<script is:inline data-astro-rerun> <script>
/* Update Search Praam */ /* Update Search Praam */
function updateGoBackUrl() { function updateGoBackUrl() {
const backButton = document.querySelector("#back-button"); const backButton: HTMLAnchorElement | null =
const params = new URL(document.location.toString()).searchParams; document.querySelector("#back-button");
if (params.has("from")) { const backUrl = sessionStorage.getItem("backUrl");
backButton.href = params.get("from");
if (backUrl && backButton) {
backButton.href = backUrl;
} }
} }
document.addEventListener("astro:page-load", updateGoBackUrl);
updateGoBackUrl(); updateGoBackUrl();
</script> </script>
+19 -1
View File
@@ -1,5 +1,6 @@
--- ---
import Breadcrumb from "@/components/Breadcrumb.astro"; import Breadcrumb from "@/components/Breadcrumb.astro";
import { SITE } from "@/config";
interface StringTitleProp { interface StringTitleProp {
pageTitle: string; pageTitle: string;
@@ -15,10 +16,16 @@ interface ArrayTitleProp {
export type Props = StringTitleProp | ArrayTitleProp; export type Props = StringTitleProp | ArrayTitleProp;
const { props } = Astro; const { props } = Astro;
const backUrl = SITE.showBackButton ? Astro.url.pathname : "/";
--- ---
<Breadcrumb /> <Breadcrumb />
<main id="main-content" class="mx-auto w-full max-w-3xl px-4 pb-4"> <main
data-backUrl={backUrl}
id="main-content"
class="mx-auto w-full max-w-3xl px-4 pb-4"
>
{ {
"titleTransition" in props ? ( "titleTransition" in props ? (
<h1 class="text-2xl font-semibold sm:text-3xl"> <h1 class="text-2xl font-semibold sm:text-3xl">
@@ -34,3 +41,14 @@ const { props } = Astro;
<p class="mt-2 mb-6 italic">{props.pageDesc}</p> <p class="mt-2 mb-6 italic">{props.pageDesc}</p>
<slot /> <slot />
</main> </main>
<script>
document.addEventListener("astro:page-load", () => {
const mainContent: HTMLElement | null =
document.querySelector("#main-content");
const backUrl = mainContent?.dataset?.backurl;
if (backUrl) {
sessionStorage.setItem("backUrl", backUrl);
}
});
</script>
+1 -7
View File
@@ -29,8 +29,6 @@ const months = [
"November", "November",
"December", "December",
]; ];
const backUrl = SITE.showBackButton ? `?from=${Astro.url.pathname}` : "/";
--- ---
<Layout title={`Archives | ${SITE.title}`}> <Layout title={`Archives | ${SITE.title}`}>
@@ -72,10 +70,7 @@ const backUrl = SITE.showBackButton ? `?from=${Astro.url.pathname}` : "/";
) )
) )
.map(({ data, id }) => ( .map(({ data, id }) => (
<Card <Card href={`/posts/${id}`} frontmatter={data} />
href={`/posts/${id}${backUrl}`}
frontmatter={data}
/>
))} ))}
</ul> </ul>
</div> </div>
@@ -84,6 +79,5 @@ const backUrl = SITE.showBackButton ? `?from=${Astro.url.pathname}` : "/";
)) ))
} }
</Main> </Main>
<Footer /> <Footer />
</Layout> </Layout>
+11 -1
View File
@@ -22,7 +22,7 @@ const recentPosts = sortedPosts.filter(({ data }) => !data.featured);
<Layout> <Layout>
<Header /> <Header />
<main id="main-content"> <main id="main-content" data-layout="index">
<section id="hero" class="pt-8 pb-6"> <section id="hero" class="pt-8 pb-6">
<h1 class="my-4 inline-block text-4xl font-bold sm:my-8 sm:text-5xl"> <h1 class="my-4 inline-block text-4xl font-bold sm:my-8 sm:text-5xl">
Mingalaba Mingalaba
@@ -119,3 +119,13 @@ const recentPosts = sortedPosts.filter(({ data }) => !data.featured);
</main> </main>
<Footer /> <Footer />
</Layout> </Layout>
<script>
document.addEventListener("astro:page-load", () => {
const indexLayout = (document.querySelector("#main-content") as HTMLElement)
?.dataset?.layout;
if (indexLayout) {
sessionStorage.setItem("backUrl", "/");
}
});
</script>