|
1 | 1 | <script> |
| 2 | + import { browser } from "$app/environment"; |
| 3 | + import { fetchAuthorsMetadata, formattedPubDate } from "$lib/utils"; |
2 | 4 | 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"; |
4 | 7 |
|
5 | 8 | export let data, title, pub_date, author; |
6 | 9 | export const form = data; |
7 | 10 |
|
8 | 11 | let authorsMetadata = []; |
| 12 | + let backToBlogUrl = "/blog"; |
9 | 13 |
|
10 | 14 | onMount(async () => { |
11 | 15 | const postAuthors = author || []; |
12 | 16 | 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 | + } |
13 | 59 | }); |
| 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 | + } |
14 | 77 | </script> |
15 | 78 |
|
16 | 79 | <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> |
18 | 89 | <h1 |
19 | 90 | class="text-2xl |
20 | 91 | md:text-4xl |
|
0 commit comments