-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
prerender-user-profile-page
Vercel prerender function
- Loading branch information
1 parent
97f0e37
commit 50e2604
Showing
7 changed files
with
912 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/node_modules | ||
/dist |
9 changes: 9 additions & 0 deletions
9
vercel-functions/prerender-user-profile-page.func/.vc-config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"handler": "index.js", | ||
"runtime": "nodejs22.x", | ||
"memory": 3009, | ||
"maxDuration": 15, | ||
"launcherType": "Nodejs", | ||
"shouldAddHelpers": true, | ||
"shouldAddSourcemapSupport": true | ||
} |
55 changes: 55 additions & 0 deletions
55
vercel-functions/prerender-user-profile-page.func/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import FastBoot from 'fastboot'; | ||
import { replaceAllMetaTags } from './replace-meta-tag.js'; | ||
|
||
export default async function (request, response) { | ||
const { username } = request.query; | ||
|
||
// Check if username query parameter is provided | ||
if (!username) { | ||
console.error('Missing "username" query parameter'); | ||
response.redirect('/404'); | ||
|
||
return; | ||
} | ||
|
||
// Initialize a FastBoot instance | ||
const app = new FastBoot({ | ||
distPath: 'dist', | ||
resilient: false, | ||
|
||
// Customize the sandbox globals | ||
buildSandboxGlobals(defaultGlobals) { | ||
return Object.assign({}, defaultGlobals, { | ||
AbortController, | ||
}); | ||
}, | ||
|
||
maxSandboxQueueSize: 1, | ||
}); | ||
|
||
// Visit the user profile page | ||
const result = await app.visit(`/users/${username}`); | ||
const statusCode = result._fastbootInfo.response.statusCode; | ||
|
||
// Redirect to 404 page if the status code is not 200 | ||
if (statusCode !== 200) { | ||
console.warn('Error parsing FastBoot response, statusCode was:', statusCode); | ||
response.redirect('/404'); | ||
|
||
return; | ||
} | ||
|
||
// Get the HTML content of the FastBoot response | ||
const html = await result['html'](); // Weird VSCode syntax highlighting issue if written as result.html() | ||
|
||
// Define meta tag values | ||
const pageTitle = `${username}'s CodeCrafters Profile`; | ||
const pageImageUrl = `https://og.codecrafters.io/api/user_profile/${username}`; // TODO: Read `metaTagUserProfilePictureBaseURL` from page config | ||
const pageDescription = `View ${username}'s profile on CodeCrafters`; | ||
|
||
// Replace meta tags in the HTML content | ||
const responseText = replaceAllMetaTags(html, pageTitle, pageDescription, pageImageUrl); | ||
|
||
// Send the modified HTML content as the response | ||
response.send(responseText); | ||
} |
Oops, something went wrong.