modified prerender script, dynamic metadata now lives in backend
This commit is contained in:
1 parent
af529538ec
commit
6169298c5e
1 file changed
+7
-37
Regular → Executable
+7
-37
@@ -1,6 +1,5 @@
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const axios = require('axios');
|
|
||||||
|
|
||||||
// Define metadata for routes
|
// Define metadata for routes
|
||||||
const staticMeta = {
|
const staticMeta = {
|
||||||
@@ -32,6 +31,7 @@ const staticMeta = {
|
|||||||
url: 'https://itzminey.dev/socials',
|
url: 'https://itzminey.dev/socials',
|
||||||
color: '#eb284c'
|
color: '#eb284c'
|
||||||
},
|
},
|
||||||
|
/*
|
||||||
'/services': {
|
'/services': {
|
||||||
title: 'Services | itzMiney',
|
title: 'Services | itzMiney',
|
||||||
description: 'All the Services I offer are listed here!',
|
description: 'All the Services I offer are listed here!',
|
||||||
@@ -39,6 +39,7 @@ const staticMeta = {
|
|||||||
url: 'https://itzminey.dev/services',
|
url: 'https://itzminey.dev/services',
|
||||||
color: '#eb284c'
|
color: '#eb284c'
|
||||||
},
|
},
|
||||||
|
*/
|
||||||
'/blog': {
|
'/blog': {
|
||||||
title: 'Blog | itzMiney',
|
title: 'Blog | itzMiney',
|
||||||
description: 'Welcome to my blog! I\'ll post occasional updates here on the stuff I\'m currently up to.',
|
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',
|
url: 'https://itzminey.dev/404',
|
||||||
color: '#eb284c',
|
color: '#eb284c',
|
||||||
},
|
},
|
||||||
|
/*
|
||||||
'/tos': {
|
'/tos': {
|
||||||
title: 'Terms of Service | itzMiney',
|
title: 'Terms of Service | itzMiney',
|
||||||
description: 'My Terms of Service for the Freelancing work I provide',
|
description: 'My Terms of Service for the Freelancing work I provide',
|
||||||
@@ -74,6 +76,7 @@ const staticMeta = {
|
|||||||
url: 'https://itzminey.dev/tos',
|
url: 'https://itzminey.dev/tos',
|
||||||
color: '#eb284c'
|
color: '#eb284c'
|
||||||
},
|
},
|
||||||
|
*/
|
||||||
'/privacy': {
|
'/privacy': {
|
||||||
title: 'Privacy Policy | itzMiney',
|
title: 'Privacy Policy | itzMiney',
|
||||||
description: 'My Privacy Policy for the data collected on this site',
|
description: 'My Privacy Policy for the data collected on this site',
|
||||||
@@ -88,6 +91,7 @@ const staticMeta = {
|
|||||||
url: 'https://itzminey.dev/imprint',
|
url: 'https://itzminey.dev/imprint',
|
||||||
color: '#eb284c'
|
color: '#eb284c'
|
||||||
},
|
},
|
||||||
|
/*
|
||||||
'/shortener': {
|
'/shortener': {
|
||||||
title: 'Shortener | itzMiney',
|
title: 'Shortener | itzMiney',
|
||||||
description: 'URL Shortener',
|
description: 'URL Shortener',
|
||||||
@@ -95,6 +99,7 @@ const staticMeta = {
|
|||||||
url: 'https://itzminey.dev/shortener',
|
url: 'https://itzminey.dev/shortener',
|
||||||
color: '#eb284c'
|
color: '#eb284c'
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
};
|
};
|
||||||
|
|
||||||
// Output directory for pre-rendered metadata
|
// Output directory for pre-rendered metadata
|
||||||
@@ -103,39 +108,6 @@ const outputDir = path.join(__dirname, '../dist/itzmineydev/meta');
|
|||||||
// Ensure the output directory exists
|
// Ensure the output directory exists
|
||||||
fs.mkdirSync(outputDir, { recursive: true });
|
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) {
|
function ensureFolderExists(folderPath) {
|
||||||
if (!fs.existsSync(folderPath)) {
|
if (!fs.existsSync(folderPath)) {
|
||||||
fs.mkdirSync(folderPath, { recursive: true });
|
fs.mkdirSync(folderPath, { recursive: true });
|
||||||
@@ -144,9 +116,7 @@ function ensureFolderExists(folderPath) {
|
|||||||
|
|
||||||
// Function to generate metadata files
|
// Function to generate metadata files
|
||||||
async function generateMetaFiles() {
|
async function generateMetaFiles() {
|
||||||
const dynamicMeta = await fetchDynamicMeta();
|
Object.entries(staticMeta).forEach(([route, meta]) => {
|
||||||
const allMeta = { ...staticMeta, ...dynamicMeta };
|
|
||||||
Object.entries(allMeta).forEach(([route, meta]) => {
|
|
||||||
const routePath = route === '/' ? 'index.html' : 'index.html';
|
const routePath = route === '/' ? 'index.html' : 'index.html';
|
||||||
const folderPath = path.join(outputDir, route === '/' ? '' : route.replace(/\//g, '/'));
|
const folderPath = path.join(outputDir, route === '/' ? '' : route.replace(/\//g, '/'));
|
||||||
const filePath = path.join(folderPath, routePath);
|
const filePath = path.join(folderPath, routePath);
|
||||||
|
|||||||
Reference in new issue
Block a user