Skip to content

Commit 0c1d719

Browse files
committed
Cleaner middleware
1 parent a42ba77 commit 0c1d719

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

middleware.js

+15-18
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
22
* This is a Vercel Middleware, which:
3-
* - is triggered for all `/users/:username` routes
4-
* - extracts `username` from the URL
5-
* - generates a proper Profile OG Image URL
3+
* - is triggered for concept or contest routes
4+
* - extracts concept or contest slug from the URL
5+
* - determines a proper OG Image URL and other meta tags
66
* - reads the contents of `dist/_empty.html`
7-
* - replaces OG meta tags with user-profile specific ones: <meta property="og:image" content="...">
7+
* - replaces OG meta tags with correct ones
88
* - serves the result as an HTML response
9-
* - passes request down the stack and returns if unable to extract `username`
9+
* - passes request down the stack and returns if unable to extract parameters
1010
*
1111
* Related Docs:
1212
* - https://vercel.com/docs/functions/edge-middleware
@@ -19,7 +19,7 @@ import { next } from '@vercel/edge';
1919
import { replaceAllMetaTags } from './app/utils/replace-meta-tag';
2020

2121
export const config = {
22-
// Limit the middleware to run only for user profile and concept routes
22+
// Limit the middleware to run only for concept and contest routes
2323
// RegExp syntax uses rules from pillarjs/path-to-regexp
2424
matcher: ['/concepts/:path*', '/contests/:path*'],
2525
};
@@ -56,14 +56,14 @@ function getContestDetails(slug) {
5656
}
5757

5858
export default async function middleware(request) {
59-
// Parse the users or concepts path match result from the request URL
59+
// Parse the concept or contest path match result from the request URL
6060
const conceptsPathMatchResult = request.url.match(/\/concepts\/([^/?]+)/);
6161
const contestsPathMatchResult = request.url.match(/\/contests\/([^/?]+)/);
6262

63-
// Skip the request if username or concept slug is missing
63+
// Skip the request if concept or contest slug is missing
6464
if (!conceptsPathMatchResult && !contestsPathMatchResult) {
6565
// Log an error to the console
66-
console.error('Unable to parse username or concept slug from the URL:', request.url);
66+
console.error('Unable to parse concept or contest slug from the URL:', request.url);
6767

6868
// Pass the request down the stack for processing and return
6969
return next(request);
@@ -87,21 +87,17 @@ export default async function middleware(request) {
8787
let conceptDescription = `View the ${conceptTitle} concept on CodeCrafters`;
8888

8989
// Get concept data from the backend
90-
let conceptData;
91-
9290
try {
93-
conceptData = await fetch(`https://backend.codecrafters.io/services/dynamic_og_images/concept_data?id_or_slug=${conceptSlug}`).then((res) =>
94-
res.json(),
95-
);
96-
91+
const conceptData = await (
92+
await fetch(`https://backend.codecrafters.io/services/dynamic_og_images/concept_data?id_or_slug=${conceptSlug}`)
93+
).json();
9794
conceptTitle = conceptData.title;
9895
conceptDescription = conceptData.description_markdown;
9996
} catch (e) {
100-
console.error(e);
101-
console.log('ignoring error for now');
97+
console.error('Failed to fetch concept data:', e);
10298
}
10399

104-
// Generate a proper OG Image URL for the concept
100+
// Override OG tag values for the concept
105101
pageImageUrl = `https://og.codecrafters.io/api/concept/${conceptSlug}`;
106102
pageTitle = conceptTitle;
107103
pageDescription = conceptDescription;
@@ -111,6 +107,7 @@ export default async function middleware(request) {
111107
// Fetch contest details from the hashmap
112108
const contestDetails = getContestDetails(contestSlug);
113109

110+
// Override OG tag values for the contest
114111
pageImageUrl = contestDetails.imageUrl;
115112
pageTitle = contestDetails.title;
116113
pageDescription = contestDetails.description;

0 commit comments

Comments
 (0)