Skip to content

Commit bfe9cb0

Browse files
authored
Improve blog tag handling and redirect behaviour (#4768)
## Description <!--- Please provide a summary of your changes. Make sure to include relevant motivation, context, and link related documents/conversations. --> #47 - Redirect when tag is missing or empty - Trim whitespace from `activeTag` - Redirect to `/blog` if no valid tag is present or URL contains an empty `?tag=` - Redirect when tag has no posts - Redirect users to `/blog` if an active tag is specified but no posts exist for it <!--- Use [linking keywords](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue) like `Resolves #<issue_number>` to automatically close issues. --> ## Checklist - [x] I have read and understood the [WATcloud Guidelines](https://cloud.watonomous.ca/docs/community-docs/watcloud/guidelines) - [x] I have performed a self-review of my code
1 parent 18382ab commit bfe9cb0

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

components/blog.tsx

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { useRouter } from 'next/router';
2323
import { MdxFile } from "nextra";
2424
import { Link } from "nextra-theme-docs";
2525
import { getPagesUnderRoute } from "nextra/context";
26-
import { useState } from "react";
26+
import { useState, useEffect } from "react";
2727
import { useForm } from "react-hook-form";
2828
import { z } from "zod";
2929
import Picture from "./picture";
@@ -47,21 +47,46 @@ export function BlogHeader() {
4747
export function BlogIndex() {
4848
const router = useRouter();
4949
const locale = router.locale || websiteConfig.default_locale;
50-
const activeTag = router.query.tag as string | undefined;
50+
const activeTag = (router.query.tag as string | undefined)?.trim();
5151
let tagCounts: Record<string, number> = {};
5252

5353
// Get all posts from route
5454
const allPosts = getPagesUnderRoute("/blog").filter((page) => {
5555
const frontMatter = (page as MdxFile).frontMatter || {};
5656
// Get tag counts for the tag bar
57-
if(frontMatter.tags && Array.isArray(frontMatter.tags)) {
57+
if (frontMatter.tags && Array.isArray(frontMatter.tags)) {
5858
frontMatter.tags.forEach((tag: string) => {
5959
tagCounts[tag] = (tagCounts[tag] || 0) + 1;
6060
});
6161
}
62-
if(frontMatter.hidden){return null}
62+
if (frontMatter.hidden) {return null}
6363
return frontMatter;
6464
});
65+
66+
// Redirect to main blog page if no tag specified or empty tag
67+
// (but only after router is ready and we've attempted to parse the tag)
68+
useEffect(() => {
69+
if (router.isReady && !activeTag &&
70+
(
71+
!router.query.tag ||
72+
(typeof router.query.tag === 'string' && router.query.tag.trim() === '') ||
73+
!router.asPath.includes('?tag=') ||
74+
router.asPath.includes('?tag=&') ||
75+
router.asPath.endsWith('?tag=')
76+
)
77+
) {
78+
router.push('/blog')
79+
} // eslint-disable-next-line react-hooks/exhaustive-deps
80+
}, [router.isReady, activeTag, router.query.tag, router.asPath])
81+
// excluding router object from deps to prevent infinite loop (it changes on every navigation)
82+
83+
// Redirect if tag has no posts
84+
useEffect(() => {
85+
if (router.isReady && activeTag && (!tagCounts[activeTag] || tagCounts[activeTag] === 0)) {
86+
router.push('/blog')
87+
} // eslint-disable-next-line react-hooks/exhaustive-deps
88+
}, [router.isReady, activeTag])
89+
// excluding router object from deps to prevent infinite loop (it changes on every navigation)
6590

6691
// Filter blogs by tag
6792
const filteredPosts = allPosts.filter((page) => {

0 commit comments

Comments
 (0)