From 0a5f6d38cb214a0428ff87137a24182ab69b3f91 Mon Sep 17 00:00:00 2001 From: Erland A Syafiq Date: Thu, 6 Jun 2024 19:58:27 -0400 Subject: [PATCH] Added redirect --- site/app/committees/[slug]/page.jsx | 30 +++++++++++++++++++++++++---- site/middleware.js | 5 ++--- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/site/app/committees/[slug]/page.jsx b/site/app/committees/[slug]/page.jsx index 769b78e..bc2cd48 100644 --- a/site/app/committees/[slug]/page.jsx +++ b/site/app/committees/[slug]/page.jsx @@ -3,7 +3,8 @@ import fetchStaticJSON from '@/app/utils/fetchStaticJSON'; import UserCard from '@/app/components/UserCard'; import './CommitteeDetails.css'; import FormattedParagraph from '@/app/components/FormattedParagraph'; -import { notFound } from 'next/navigation'; +import { notFound, permanentRedirect } from 'next/navigation'; +import { revalidateTag } from 'next/cache'; function findCommittee(committeeGroups, queryId) { @@ -18,6 +19,25 @@ function findCommittee(committeeGroups, queryId) { return null; } +function findCommitteeFromName(committeeGroups, queryId) { + console.log(queryId); + for (const group of committeeGroups) { + for (const committee of group.committees) { + const urlName = committee.committee_name + .replace(/[:(),<>]/g, "") // Remove specified characters + .replace(/ /g, "-") // Replace spaces with hyphens + .toLowerCase(); + console.log(urlName); + console.log(decodeURI(queryId)); + if (urlName == decodeURI(queryId)) { + return committee; + } + } + } + + return null; +} + export async function generateMetadata({ params }) { const committeeGroups = await fetchStaticJSON("/app/data/committees.json"); const id = params.slug; @@ -43,10 +63,12 @@ export default async function CommitteeDetailsPage({params}) { const committee = findCommittee(committeeGroups, id); if (!committee) { - return { - title: "Page not found", - description: "Page you're looking for doesn't exist. ." + // Attempts to find a matching url from the old ASP.NET url system + const alt_committee = findCommitteeFromName(committeeGroups, id); + if (!alt_committee) { + notFound(); } + permanentRedirect(`/committees/${alt_committee.id}`); } const isOneChair = committee.co_chair_name == null; diff --git a/site/middleware.js b/site/middleware.js index d0efa23..acbeea4 100644 --- a/site/middleware.js +++ b/site/middleware.js @@ -1,10 +1,9 @@ import { NextResponse } from "next/server"; export function middleware(request) { - - if (/[A-Z]/.test(request.nextUrl.pathName)) { + if (/[A-Z]/.test(request.nextUrl.pathname)) { const url = request.nextUrl.clone(); - url.pathname = pathname.toLowerCase(); + url.pathname = url.pathname.toLowerCase(); return NextResponse.redirect(url); }