1
1
/**
2
2
* 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
6
6
* - 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
8
8
* - 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
10
10
*
11
11
* Related Docs:
12
12
* - https://vercel.com/docs/functions/edge-middleware
@@ -19,7 +19,7 @@ import { next } from '@vercel/edge';
19
19
import { replaceAllMetaTags } from './app/utils/replace-meta-tag' ;
20
20
21
21
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
23
23
// RegExp syntax uses rules from pillarjs/path-to-regexp
24
24
matcher : [ '/concepts/:path*' , '/contests/:path*' ] ,
25
25
} ;
@@ -56,14 +56,14 @@ function getContestDetails(slug) {
56
56
}
57
57
58
58
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
60
60
const conceptsPathMatchResult = request . url . match ( / \/ c o n c e p t s \/ ( [ ^ / ? ] + ) / ) ;
61
61
const contestsPathMatchResult = request . url . match ( / \/ c o n t e s t s \/ ( [ ^ / ? ] + ) / ) ;
62
62
63
- // Skip the request if username or concept slug is missing
63
+ // Skip the request if concept or contest slug is missing
64
64
if ( ! conceptsPathMatchResult && ! contestsPathMatchResult ) {
65
65
// 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 ) ;
67
67
68
68
// Pass the request down the stack for processing and return
69
69
return next ( request ) ;
@@ -87,21 +87,17 @@ export default async function middleware(request) {
87
87
let conceptDescription = `View the ${ conceptTitle } concept on CodeCrafters` ;
88
88
89
89
// Get concept data from the backend
90
- let conceptData ;
91
-
92
90
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 ( ) ;
97
94
conceptTitle = conceptData . title ;
98
95
conceptDescription = conceptData . description_markdown ;
99
96
} catch ( e ) {
100
- console . error ( e ) ;
101
- console . log ( 'ignoring error for now' ) ;
97
+ console . error ( 'Failed to fetch concept data:' , e ) ;
102
98
}
103
99
104
- // Generate a proper OG Image URL for the concept
100
+ // Override OG tag values for the concept
105
101
pageImageUrl = `https://og.codecrafters.io/api/concept/${ conceptSlug } ` ;
106
102
pageTitle = conceptTitle ;
107
103
pageDescription = conceptDescription ;
@@ -111,6 +107,7 @@ export default async function middleware(request) {
111
107
// Fetch contest details from the hashmap
112
108
const contestDetails = getContestDetails ( contestSlug ) ;
113
109
110
+ // Override OG tag values for the contest
114
111
pageImageUrl = contestDetails . imageUrl ;
115
112
pageTitle = contestDetails . title ;
116
113
pageDescription = contestDetails . description ;
0 commit comments