Merge pull request #52 from itzMiney/development
modified prerender script, dynamic metadata now lives in backend
This commit is contained in:
1 file changed
+7
-37
Regular → Executable
+7
-37
@@ -1,6 +1,5 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const axios = require('axios');
|
||||
|
||||
// Define metadata for routes
|
||||
const staticMeta = {
|
||||
@@ -32,6 +31,7 @@ const staticMeta = {
|
||||
url: 'https://itzminey.dev/socials',
|
||||
color: '#eb284c'
|
||||
},
|
||||
/*
|
||||
'/services': {
|
||||
title: 'Services | itzMiney',
|
||||
description: 'All the Services I offer are listed here!',
|
||||
@@ -39,6 +39,7 @@ const staticMeta = {
|
||||
url: 'https://itzminey.dev/services',
|
||||
color: '#eb284c'
|
||||
},
|
||||
*/
|
||||
'/blog': {
|
||||
title: 'Blog | itzMiney',
|
||||
description: 'Welcome to my blog! I\'ll post occasional updates here on the stuff I\'m currently up to.',
|
||||
@@ -67,6 +68,7 @@ const staticMeta = {
|
||||
url: 'https://itzminey.dev/404',
|
||||
color: '#eb284c',
|
||||
},
|
||||
/*
|
||||
'/tos': {
|
||||
title: 'Terms of Service | itzMiney',
|
||||
description: 'My Terms of Service for the Freelancing work I provide',
|
||||
@@ -74,6 +76,7 @@ const staticMeta = {
|
||||
url: 'https://itzminey.dev/tos',
|
||||
color: '#eb284c'
|
||||
},
|
||||
*/
|
||||
'/privacy': {
|
||||
title: 'Privacy Policy | itzMiney',
|
||||
description: 'My Privacy Policy for the data collected on this site',
|
||||
@@ -88,6 +91,7 @@ const staticMeta = {
|
||||
url: 'https://itzminey.dev/imprint',
|
||||
color: '#eb284c'
|
||||
},
|
||||
/*
|
||||
'/shortener': {
|
||||
title: 'Shortener | itzMiney',
|
||||
description: 'URL Shortener',
|
||||
@@ -95,6 +99,7 @@ const staticMeta = {
|
||||
url: 'https://itzminey.dev/shortener',
|
||||
color: '#eb284c'
|
||||
}
|
||||
*/
|
||||
};
|
||||
|
||||
// Output directory for pre-rendered metadata
|
||||
@@ -103,39 +108,6 @@ const outputDir = path.join(__dirname, '../dist/itzmineydev/meta');
|
||||
// Ensure the output directory exists
|
||||
fs.mkdirSync(outputDir, { recursive: true });
|
||||
|
||||
// Function to fetch dynamic metadata
|
||||
async function fetchDynamicMeta() {
|
||||
try {
|
||||
const response = await axios.get('https://itzminey.dev/api/articles'); // Replace with your API endpoint
|
||||
const articles = response.data;
|
||||
|
||||
return articles.reduce((acc, article) => {
|
||||
const createdAt = new Date(article.createdAt);
|
||||
const formattedCreatedAt = createdAt.toLocaleDateString('en-US', {
|
||||
month: 'short',
|
||||
day: '2-digit',
|
||||
year: 'numeric',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
hour12: false,
|
||||
});
|
||||
|
||||
const description = `By ${article.authorName} | Article from ${formattedCreatedAt}`;
|
||||
acc[`/blog/${article.slug}`] = {
|
||||
title: article.title,
|
||||
description,
|
||||
image: 'https://meta.itzminey.dev/assets/ogimg.png',
|
||||
url: `https://itzminey.dev/blog/${article.slug}`,
|
||||
color: '#eb284c',
|
||||
};
|
||||
return acc;
|
||||
}, {});
|
||||
} catch (error) {
|
||||
console.error('Error fetching dynamic metadata:', error);
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
function ensureFolderExists(folderPath) {
|
||||
if (!fs.existsSync(folderPath)) {
|
||||
fs.mkdirSync(folderPath, { recursive: true });
|
||||
@@ -144,9 +116,7 @@ function ensureFolderExists(folderPath) {
|
||||
|
||||
// Function to generate metadata files
|
||||
async function generateMetaFiles() {
|
||||
const dynamicMeta = await fetchDynamicMeta();
|
||||
const allMeta = { ...staticMeta, ...dynamicMeta };
|
||||
Object.entries(allMeta).forEach(([route, meta]) => {
|
||||
Object.entries(staticMeta).forEach(([route, meta]) => {
|
||||
const routePath = route === '/' ? 'index.html' : 'index.html';
|
||||
const folderPath = path.join(outputDir, route === '/' ? '' : route.replace(/\//g, '/'));
|
||||
const filePath = path.join(folderPath, routePath);
|
||||
|
||||
Reference in new issue
Block a user