From a1b03a93233496bf29008bf22023b492c47460fc Mon Sep 17 00:00:00 2001 From: Sh Raj Date: Mon, 25 Mar 2024 15:59:53 +0000 Subject: [PATCH] sm --- src/app/sitemapsh/[id]/route.js | 14 +++++++++----- src/app/sitemapsh/route.js | 6 ++++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/app/sitemapsh/[id]/route.js b/src/app/sitemapsh/[id]/route.js index 609227c..e43fd1f 100644 --- a/src/app/sitemapsh/[id]/route.js +++ b/src/app/sitemapsh/[id]/route.js @@ -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 }` ); @@ -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 = ''; @@ -33,7 +37,7 @@ export async function GET(req, res) { // Add each article URL to the sitemap articles.forEach((article) => { xml += ""; - xml += `https://sh20raj.com${article.path}`; // Modify URL structure as needed + xml += `${url.origin + article.path}`; // Modify URL structure as needed xml += `${new Date( article.published_at ).toISOString()}`; // Use published date as last modified diff --git a/src/app/sitemapsh/route.js b/src/app/sitemapsh/route.js index 566a938..9c2a7b9 100644 --- a/src/app/sitemapsh/route.js +++ b/src/app/sitemapsh/route.js @@ -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 @@ -10,9 +12,9 @@ export async function GET(req) { let xml = ''; xml += ''; - for (let i = 0; i < 1000; i++) { + for (let i = 0; i < 1001; i++) { xml += ""; - xml += `https://sh20raj.com/sitemapsh/${i || Math.floor(Math.random()*1000)}`; // Modify URL structure as needed + xml += `${url.origin}/sitemapsh/${i || Math.floor(Math.random()*1000)}`; // Modify URL structure as needed xml += ""; }