From 31e77746a36c0886368054b238acc04fd5b71716 Mon Sep 17 00:00:00 2001 From: Himank Dave <93311724+steadyfall@users.noreply.github.com> Date: Thu, 2 Oct 2025 16:49:06 -0400 Subject: [PATCH 1/2] fix(blog): redirect to main blog page when tag is missing or empty - Add `useEffect` to check `router.query.tag` and redirect to `/blog` if: - no tag is provided - tag is an empty string or only whitespace - URL contains `?tag=` with no value - Trim `activeTag` to avoid false positives from whitespace --- components/blog.tsx | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/components/blog.tsx b/components/blog.tsx index 959c2e2..dabb2fa 100644 --- a/components/blog.tsx +++ b/components/blog.tsx @@ -23,7 +23,7 @@ import { useRouter } from 'next/router'; import { MdxFile } from "nextra"; import { Link } from "nextra-theme-docs"; import { getPagesUnderRoute } from "nextra/context"; -import { useState } from "react"; +import { useState, useEffect } from "react"; import { useForm } from "react-hook-form"; import { z } from "zod"; import Picture from "./picture"; @@ -47,9 +47,25 @@ export function BlogHeader() { export function BlogIndex() { const router = useRouter(); const locale = router.locale || websiteConfig.default_locale; - const activeTag = router.query.tag as string | undefined; + const activeTag = (router.query.tag as string | undefined)?.trim(); let tagCounts: Record = {}; + // Redirect to main blog page if no tag specified or empty tag + // (but only after router is ready and we've attempted to parse the tag) + useEffect(() => { + if (router.isReady && !activeTag && + ( + !router.query.tag || + (typeof router.query.tag === 'string' && router.query.tag.trim() === '') || + !router.asPath.includes('?tag=') || + router.asPath.includes('?tag=&') || + router.asPath.endsWith('?tag=') + ) + ) { + router.push('/blog') + } // eslint-disable-next-line react-hooks/exhaustive-deps + }, [router.isReady, activeTag, router.query.tag, router.asPath]) + // Get all posts from route const allPosts = getPagesUnderRoute("/blog").filter((page) => { const frontMatter = (page as MdxFile).frontMatter || {}; From 861dbb3391a5201b1298d94a5fca37cf015c6f1c Mon Sep 17 00:00:00 2001 From: Himank Dave <93311724+steadyfall@users.noreply.github.com> Date: Thu, 2 Oct 2025 16:53:09 -0400 Subject: [PATCH 2/2] fix(blog): redirect to main page when tag has no posts - Use `useEffect` to redirect to `/blog` if active tag exists but has zero posts - Prevent users from landing on empty tag pages --- components/blog.tsx | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/components/blog.tsx b/components/blog.tsx index dabb2fa..057a892 100644 --- a/components/blog.tsx +++ b/components/blog.tsx @@ -50,6 +50,19 @@ export function BlogIndex() { const activeTag = (router.query.tag as string | undefined)?.trim(); let tagCounts: Record = {}; + // Get all posts from route + const allPosts = getPagesUnderRoute("/blog").filter((page) => { + const frontMatter = (page as MdxFile).frontMatter || {}; + // Get tag counts for the tag bar + if (frontMatter.tags && Array.isArray(frontMatter.tags)) { + frontMatter.tags.forEach((tag: string) => { + tagCounts[tag] = (tagCounts[tag] || 0) + 1; + }); + } + if (frontMatter.hidden) {return null} + return frontMatter; + }); + // Redirect to main blog page if no tag specified or empty tag // (but only after router is ready and we've attempted to parse the tag) useEffect(() => { @@ -66,18 +79,12 @@ export function BlogIndex() { } // eslint-disable-next-line react-hooks/exhaustive-deps }, [router.isReady, activeTag, router.query.tag, router.asPath]) - // Get all posts from route - const allPosts = getPagesUnderRoute("/blog").filter((page) => { - const frontMatter = (page as MdxFile).frontMatter || {}; - // Get tag counts for the tag bar - if(frontMatter.tags && Array.isArray(frontMatter.tags)) { - frontMatter.tags.forEach((tag: string) => { - tagCounts[tag] = (tagCounts[tag] || 0) + 1; - }); - } - if(frontMatter.hidden){return null} - return frontMatter; - }); + // Redirect if tag has no posts + useEffect(() => { + if (router.isReady && activeTag && (!tagCounts[activeTag] || tagCounts[activeTag] === 0)) { + router.push('/blog') + } // eslint-disable-next-line react-hooks/exhaustive-deps + }, [router.isReady, activeTag]) // Filter blogs by tag const filteredPosts = allPosts.filter((page) => {