Skip to content

Commit 1cada1d

Browse files
committed
Add UI element to go back to current blog's page from inside a post
1 parent 6b0e028 commit 1cada1d

File tree

2 files changed

+75
-4
lines changed

2 files changed

+75
-4
lines changed

src/lib/blocks/Blog.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
>
8080
<a
8181
class="post-link"
82-
href="{base}/blog/{post.path}"
82+
href="{base}/blog/{post.path}?from={pageNum === 1 ? 'blog' : `blog/${pageNum}`}"
8383
title={post.meta.title}
8484
>
8585
{post.meta.title}
@@ -112,7 +112,7 @@
112112
</p>
113113
<a
114114
class="block text-right mt-4"
115-
href="{base}/blog/{post.path}"
115+
href="{base}/blog/{post.path}?from={pageNum === 1 ? 'blog' : `blog/${pageNum}`}"
116116
title={post.meta.title}
117117
>
118118
{$_("config.blog.readMore")}&hellip;

src/lib/blocks/Post.svelte

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,91 @@
11
<script>
2+
import { browser } from "$app/environment";
3+
import { fetchAuthorsMetadata, formattedPubDate } from "$lib/utils";
24
import { onMount } from "svelte";
3-
import { formattedPubDate, fetchAuthorsMetadata } from "$lib/utils";
5+
import { Icon } from "svelte-icons-pack";
6+
import { BsChevronLeft } from "svelte-icons-pack/bs";
47
58
export let data, title, pub_date, author;
69
export const form = data;
710
811
let authorsMetadata = [];
12+
let backToBlogUrl = "/blog";
913
1014
onMount(async () => {
1115
const postAuthors = author || [];
1216
authorsMetadata = await fetchAuthorsMetadata(postAuthors);
17+
18+
// Determine the correct "back to blog" URL
19+
if (browser) {
20+
// Method 1: Check URL parameters first (most reliable)
21+
const urlParams = new URLSearchParams(window.location.search);
22+
const fromPage = urlParams.get('from');
23+
24+
if (fromPage) {
25+
// Validate that it's a valid blog page
26+
if (fromPage === 'blog' || /^blog\/\d+$/.test(fromPage)) {
27+
backToBlogUrl = `/${fromPage}`;
28+
}
29+
} else {
30+
// Method 2: Check referrer
31+
const referrer = document.referrer;
32+
33+
if (referrer) {
34+
try {
35+
const referrerUrl = new URL(referrer);
36+
const referrerPath = referrerUrl.pathname;
37+
38+
// Check if referrer is from a blog page
39+
if (referrerPath.startsWith('/blog')) {
40+
// If it's a paginated blog page (e.g., /blog/2, /blog/3)
41+
const blogPageMatch = referrerPath.match(/^\/blog\/(\d+)$/);
42+
if (blogPageMatch) {
43+
const pageNum = parseInt(blogPageMatch[1]);
44+
if (pageNum > 1) {
45+
backToBlogUrl = `/blog/${pageNum}`;
46+
} else {
47+
backToBlogUrl = "/blog";
48+
}
49+
} else if (referrerPath === "/blog") {
50+
backToBlogUrl = "/blog";
51+
}
52+
}
53+
} catch (error) {
54+
console.warn("Error parsing referrer URL:", error);
55+
}
56+
}
57+
}
58+
}
1359
});
60+
61+
// Function to handle back button click
62+
function handleBackClick(event) {
63+
if (browser) {
64+
// If we have a specific back URL, use it
65+
if (backToBlogUrl !== "/blog") {
66+
// Let the normal link behavior work
67+
return;
68+
}
69+
70+
// Otherwise, try to go back in history
71+
if (window.history.length > 1) {
72+
window.history.back();
73+
event.preventDefault();
74+
}
75+
}
76+
}
1477
</script>
1578

1679
<article class="container">
17-
<div class="my-20 xl:mt-32 xl:mb-20">
80+
<div class="my-20 xl:mt-32 xl:mb-20 mx-auto max-w-6xl">
81+
<a
82+
href={backToBlogUrl}
83+
on:click={handleBackClick}
84+
class="flex items-center justify-center gap-1 button py-1 text-red-berry-900 dark:text-spring-wood-400"
85+
>
86+
<Icon src={BsChevronLeft} />
87+
Back to blog
88+
</a>
1889
<h1
1990
class="text-2xl
2091
md:text-4xl

0 commit comments

Comments
 (0)