Skip to content

Commit

Permalink
sm
Browse files Browse the repository at this point in the history
  • Loading branch information
SH20RAJ authored Mar 25, 2024
1 parent 15da881 commit a1b03a9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
14 changes: 9 additions & 5 deletions src/app/sitemapsh/[id]/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ function generateRandomNumber(min, max) {


// Mock function to fetch articles (replace this with your actual implementation)
let getArticlesFromDevTo = async (params) => {
let getArticlesFromDevTo = async (params,limit) => {
const response = await fetch(
`https://dev.to/api/articles/latest/?per_page=500&page=${
`https://dev.to/api/articles/latest/?per_page=${limit}&page=${
params
}`
);
Expand All @@ -20,11 +20,15 @@ let getArticlesFromDevTo = async (params) => {
};

export async function GET(req, res) {
console.log(res);
const url = new URL(req.url)
if (req.method === "GET") {
try {
// Fetch articles from Dev.to or your database
let articles = await getArticlesFromDevTo(res.params.id);

let id = url.searchParams.has("limit") ? url.searchParams.get("limit") : 1000;

console.log("id",id);
let articles = await getArticlesFromDevTo(res.params.id, id);

// Start building the XML
let xml = '<?xml version="1.0" encoding="UTF-8"?>';
Expand All @@ -33,7 +37,7 @@ export async function GET(req, res) {
// 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 += `<loc>${url.origin + article.path}</loc>`; // Modify URL structure as needed
xml += `<lastmod>${new Date(
article.published_at
).toISOString()}</lastmod>`; // Use published date as last modified
Expand Down
6 changes: 4 additions & 2 deletions src/app/sitemapsh/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { NextResponse } from "next/server";


export async function GET(req) {
const url = new URL(req.url)

if (true) {
try {
// Fetch articles from Dev.to or your database
Expand All @@ -10,9 +12,9 @@ export async function GET(req) {
let xml = '<?xml version="1.0" encoding="UTF-8"?>';
xml += '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

for (let i = 0; i < 1000; i++) {
for (let i = 0; i < 1001; i++) {
xml += "<sitemap>";
xml += `<loc>https://sh20raj.com/sitemapsh/${i || Math.floor(Math.random()*1000)}</loc>`; // Modify URL structure as needed
xml += `<loc>${url.origin}/sitemapsh/${i || Math.floor(Math.random()*1000)}</loc>`; // Modify URL structure as needed
xml += "</sitemap>";

}
Expand Down

0 comments on commit a1b03a9

Please sign in to comment.