Skip to content

Commit

Permalink
shs sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
SH20RAJ authored Mar 25, 2024
1 parent 1a6d555 commit 89afeef
Show file tree
Hide file tree
Showing 2 changed files with 267 additions and 0 deletions.
210 changes: 210 additions & 0 deletions src/app/sitemapsh/[id]/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
// import fetch from 'isomorphic-fetch'; // Import fetch
// import { getArticlesFromDevTo } from 'someFunctions'; // Replace with your actual function to fetch articles
function generateRandomNumber(min, max) {
// Math.random() generates a random number between 0 and 1
// We multiply it by (max - min + 1) to include the max value,
// then add min to ensure the number falls within the desired range.
return Math.floor(Math.random() * (max - min + 1)) + min;
}

// Usage example to generate a random number between 1 and 10000
const randomNumber = generateRandomNumber(1, 1000);
console.log(randomNumber);

// Mock function to fetch articles (replace this with your actual implementation)
let getArticlesFromDevTo = async (params) => {
console.log("params", params);
const response = await fetch(
`https://dev.to/api/articles/latest/?per_page=100000&page=${
params || generateRandomNumber(1, 1000).toString()
}`
);
const data = await response.json();
return data;
};

export async function GET(req) {
if (req.method === "GET") {
try {
// Fetch articles from Dev.to or your database
let articles = await getArticlesFromDevTo(0);
let articles2 = await getArticlesFromDevTo(328);
let articles3 = await getArticlesFromDevTo(39);
let articles4 = await getArticlesFromDevTo(342);
// let articles5 = await getArticlesFromDevTo(35);
// let articles6 = await getArticlesFromDevTo(122);
// let articles7 = await getArticlesFromDevTo(11);
// let articles8 = await getArticlesFromDevTo(312);
// let articles9 = await getArticlesFromDevTo(31);
// let articles10 = await getArticlesFromDevTo(21);
// let articles11 = await getArticlesFromDevTo();
// let articles12 = await getArticlesFromDevTo(2);
// let articles13 = await getArticlesFromDevTo();
// let articles14 = await getArticlesFromDevTo(2);
// let articles15 = await getArticlesFromDevTo();
// let articles16 = await getArticlesFromDevTo(2);
// let articles17 = await getArticlesFromDevTo();
// let articles18 = await getArticlesFromDevTo(2);
// let articles19 = await getArticlesFromDevTo();
// let articles20 = await getArticlesFromDevTo(2);
// let articles21 = await getArticlesFromDevTo();
// let articles22 = await getArticlesFromDevTo(2);

// Start building the XML
let xml = '<?xml version="1.0" encoding="UTF-8"?>';
xml += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

// Add each article URL to the sitemap
articles.forEach((article) => {
xml += "<url>";
xml += `<loc>https://sh20raj.com${article.path}</loc>`; // Modify URL structure as needed
xml += `<lastmod>${new Date(
article.published_at
).toISOString()}</lastmod>`; // Use published date as last modified
xml += "<changefreq>weekly</changefreq>"; // You can adjust the change frequency
xml += "<priority>0.8</priority>"; // Priority can be adjusted based on the importance of the page
xml += "</url>";
});
articles2.forEach((article) => {
xml += "<url>";
xml += `<loc>https://sh20raj.com${article.path}</loc>`; // Modify URL structure as needed
xml += `<lastmod>${new Date(
article.published_at
).toISOString()}</lastmod>`; // Use published date as last modified
xml += "<changefreq>weekly</changefreq>"; // You can adjust the change frequency
xml += "<priority>0.8</priority>"; // Priority can be adjusted based on the importance of the page
xml += "</url>";
});
articles3.forEach((article) => {
xml += "<url>";
xml += `<loc>https://sh20raj.com${article.path}</loc>`; // Modify URL structure as needed
xml += `<lastmod>${new Date(
article.published_at
).toISOString()}</lastmod>`; // Use published date as last modified
xml += "<changefreq>weekly</changefreq>"; // You can adjust the change frequency
xml += "<priority>0.8</priority>"; // Priority can be adjusted based on the importance of the page
xml += "</url>";
});
articles4.forEach((article) => {
xml += "<url>";
xml += `<loc>https://sh20raj.com${article.path}</loc>`; // Modify URL structure as needed
xml += `<lastmod>${new Date(
article.published_at
).toISOString()}</lastmod>`; // Use published date as last modified
xml += "<changefreq>weekly</changefreq>"; // You can adjust the change frequency
xml += "<priority>0.8</priority>"; // Priority can be adjusted based on the importance of the page
xml += "</url>";
});
// articles5.forEach((article) => {
// xml += "<url>";
// xml += `<loc>https://sh20raj.com${article.path}</loc>`; // Modify URL structure as needed
// xml += `<lastmod>${new Date(
// article.published_at
// ).toISOString()}</lastmod>`; // Use published date as last modified
// xml += "<changefreq>weekly</changefreq>"; // You can adjust the change frequency
// xml += "<priority>0.8</priority>"; // Priority can be adjusted based on the importance of the page
// xml += "</url>";
// });
// articles6.forEach((article) => {
// xml += "<url>";
// xml += `<loc>https://sh20raj.com${article.path}</loc>`; // Modify URL structure as needed
// xml += `<lastmod>${new Date(
// article.published_at
// ).toISOString()}</lastmod>`; // Use published date as last modified
// xml += "<changefreq>weekly</changefreq>"; // You can adjust the change frequency
// xml += "<priority>0.8</priority>"; // Priority can be adjusted based on the importance of the page
// xml += "</url>";
// });
// articles7.forEach((article) => {
// xml += "<url>";
// xml += `<loc>https://sh20raj.com${article.path}</loc>`; // Modify URL structure as needed
// xml += `<lastmod>${new Date(
// article.published_at
// ).toISOString()}</lastmod>`; // Use published date as last modified
// xml += "<changefreq>weekly</changefreq>"; // You can adjust the change frequency
// xml += "<priority>0.8</priority>"; // Priority can be adjusted based on the importance of the page
// xml += "</url>";
// });
// articles8.forEach((article) => {
// xml += "<url>";
// xml += `<loc>https://sh20raj.com${article.path}</loc>`; // Modify URL structure as needed
// xml += `<lastmod>${new Date(
// article.published_at
// ).toISOString()}</lastmod>`; // Use published date as last modified
// xml += "<changefreq>weekly</changefreq>"; // You can adjust the change frequency
// xml += "<priority>0.8</priority>"; // Priority can be adjusted based on the importance of the page
// xml += "</url>";
// });
// (await getArticlesFromDevTo(24)).forEach((article) => {
// xml += "<url>";
// xml += `<loc>https://sh20raj.com${article.path}</loc>`; // Modify URL structure as needed
// xml += `<lastmod>${new Date(
// article.published_at
// ).toISOString()}</lastmod>`; // Use published date as last modified
// xml += "<changefreq>weekly</changefreq>"; // You can adjust the change frequency
// xml += "<priority>0.8</priority>"; // Priority can be adjusted based on the importance of the page
// xml += "</url>";
// });

// (await getArticlesFromDevTo(32)).forEach((article) => {
// xml += "<url>";
// xml += `<loc>https://sh20raj.com${article.path}</loc>`; // Modify URL structure as needed
// xml += `<lastmod>${new Date(
// article.published_at
// ).toISOString()}</lastmod>`; // Use published date as last modified
// xml += "<changefreq>weekly</changefreq>"; // You can adjust the change frequency
// xml += "<priority>0.8</priority>"; // Priority can be adjusted based on the importance of the page
// xml += "</url>";
// });

// (await getArticlesFromDevTo(23)).forEach((article) => {
// xml += "<url>";
// xml += `<loc>https://sh20raj.com${article.path}</loc>`; // Modify URL structure as needed
// xml += `<lastmod>${new Date(
// article.published_at
// ).toISOString()}</lastmod>`; // Use published date as last modified
// xml += "<changefreq>weekly</changefreq>"; // You can adjust the change frequency
// xml += "<priority>0.8</priority>"; // Priority can be adjusted based on the importance of the page
// xml += "</url>";
// });

// (await getArticlesFromDevTo(324)).forEach((article) => {
// xml += "<url>";
// xml += `<loc>https://sh20raj.com${article.path}</loc>`; // Modify URL structure as needed
// xml += `<lastmod>${new Date(
// article.published_at
// ).toISOString()}</lastmod>`; // Use published date as last modified
// xml += "<changefreq>weekly</changefreq>"; // You can adjust the change frequency
// xml += "<priority>0.8</priority>"; // Priority can be adjusted based on the importance of the page
// xml += "</url>";
// });






xml += "</urlset>";

// Set the response headers and status
return new Response(xml, {
headers: {
"Content-Type": "application/xml",
},
status: 200,
});
} catch (error) {
console.error("Error generating sitemap:", error);
return new Response("Error generating sitemap", {
status: 500,
});
}
} else {
return new Response("Method Not Allowed", {
status: 405,
headers: {
Allow: ["GET"],
},
});
}
}
57 changes: 57 additions & 0 deletions src/app/sitemapsh/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { NextResponse } from "next/server";

// Mock function to fetch articles (replace this with your actual implementation)
let getArticlesFromDevTo = async () => {
const response = await fetch(
`https://dev.to/api/articles/latest/?per_page=100000&page=1`
);
const data = await response.json();
return data;
};

export async function GET(req) {
if (req.method === "GET") {
try {
// Fetch articles from Dev.to or your database
const articles = await getArticlesFromDevTo();

// Start building the XML
let xml = '<?xml version="1.0" encoding="UTF-8"?>';
xml +=
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';

// Add each article URL to the sitemap
articles.forEach((article) => {
xml += "<url>";
xml += `<loc>https://sh20raj.com${article.path}</loc>`; // Modify URL structure as needed
xml += `<lastmod>${new Date(article.published_at).toISOString()}</lastmod>`; // Use published date as last modified
xml += "<changefreq>weekly</changefreq>"; // You can adjust the change frequency
xml += "<priority>0.8</priority>"; // Priority can be adjusted based on the importance of the page
xml += "</url>";
});

xml += "</urlset>";

// Set the response headers and status
return new NextResponse(xml, {
headers: {
"Content-Type": "application/xml",
"X-Robots-Tag": "index, follow",
},
status: 200,
});
} catch (error) {
console.error("Error generating sitemap:", error);
return new NextResponse("Error generating sitemap", {
status: 500,
});
}
} else {
return new NextResponse("Method Not Allowed", {
status: 405,
headers: {
Allow: ["GET"],
},
});
}
}

0 comments on commit 89afeef

Please sign in to comment.