Skip to content

Commit aaad48e

Browse files
committed
fix: use reliable memory tracking for back button to prevent history leakage on refreshes
1 parent ec37a59 commit aaad48e

3 files changed

Lines changed: 30 additions & 5 deletions

File tree

src/app/blog/page.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { getSortedPostsData } from "@/lib/blog";
22
import Link from "next/link";
3-
import { ExternalLink } from "lucide-react";
4-
import { BackButton } from "@/components/ui/back-button";
3+
import { ExternalLink, ArrowLeft } from "lucide-react";
54
import { Metadata } from "next";
65

76
export const metadata: Metadata = {
@@ -18,7 +17,13 @@ export default function BlogArchive() {
1817

1918
{/* Navigation */}
2019
<nav className="mb-12 flex items-center justify-between">
21-
<BackButton fallback="/" />
20+
<Link
21+
href="/"
22+
className="group flex items-center gap-2 text-sm font-semibold tracking-wide text-zinc-500 hover:text-blue-400 transition-colors uppercase"
23+
>
24+
<ArrowLeft size={16} className="transition-transform group-hover:-translate-x-1" />
25+
Back
26+
</Link>
2227
<Link
2328
href="/"
2429
className="text-sm font-semibold tracking-wide text-zinc-500 hover:text-blue-400 transition-colors uppercase"

src/app/layout.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const geistMono = Geist_Mono({
1313
});
1414

1515
import { portfolioData } from "@/data/portfolio";
16+
import { AppNavigationTracker } from "@/components/ui/back-button";
1617

1718
export const metadata: Metadata = {
1819
title: `${portfolioData.personalInfo.name} | ${portfolioData.personalInfo.tagline}`,
@@ -29,6 +30,7 @@ export default function RootLayout({
2930
<body
3031
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
3132
>
33+
<AppNavigationTracker />
3234
{children}
3335
</body>
3436
</html>

src/components/ui/back-button.tsx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
11
"use client";
22

3-
import { useRouter } from "next/navigation";
3+
import { useRouter, usePathname } from "next/navigation";
44
import { ArrowLeft } from "lucide-react";
5+
import { useEffect, useRef } from "react";
6+
7+
export function AppNavigationTracker() {
8+
const pathname = usePathname();
9+
const isFirstRender = useRef(true);
10+
11+
useEffect(() => {
12+
if (isFirstRender.current) {
13+
isFirstRender.current = false;
14+
(window as any).isInternalNavigation = false;
15+
} else {
16+
(window as any).isInternalNavigation = true;
17+
}
18+
}, [pathname]);
19+
20+
return null;
21+
}
22+
523
export function BackButton({ fallback = "/" }: { fallback?: string }) {
624
const router = useRouter();
725

826
const handleBack = () => {
9-
if (typeof window !== "undefined" && window.history.length > 2) {
27+
if (typeof window !== "undefined" && (window as any).isInternalNavigation) {
1028
router.back();
1129
} else {
1230
router.push(fallback);

0 commit comments

Comments
 (0)