Skip to content

Commit

Permalink
Add new prerender-user-profile-page Vercel prerender function
Browse files Browse the repository at this point in the history
  • Loading branch information
VasylMarchuk committed Feb 12, 2025
1 parent 97f0e37 commit 50e2604
Show file tree
Hide file tree
Showing 7 changed files with 912 additions and 0 deletions.
2 changes: 2 additions & 0 deletions vercel-functions/prerender-user-profile-page.func/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules
/dist
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 vercel-functions/prerender-user-profile-page.func/index.js
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);
}
Loading

0 comments on commit 50e2604

Please sign in to comment.