Skip to content

Commit

Permalink
Merge pull request #33 from erland-syafiq/committee-redirects
Browse files Browse the repository at this point in the history
Added redirect
  • Loading branch information
erland-syafiq authored Jun 6, 2024
2 parents 0a21db6 + 0a5f6d3 commit c0eee85
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
30 changes: 26 additions & 4 deletions site/app/committees/[slug]/page.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions site/middleware.js
Original file line number Diff line number Diff line change
@@ -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);
}

Expand Down

0 comments on commit c0eee85

Please sign in to comment.