Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

93
Views
Mapa del sitio de Next.js con parámetros de URL

Estoy tratando de crear un mapa del sitio para el proyecto en el que estoy trabajando y actualmente estoy usando next-sitemap para generar. El problema con este enfoque y prácticamente otra solución que encontré es que el mapa del sitio se genera para las páginas que se crean durante el tiempo de compilación.

Mi caso de uso también requiere agregar parámetros de URL en el mapa del sitio que no generen nuevas páginas durante el tiempo de compilación.

Este es también el enfoque más cercano que creo que debería funcionar, pero tendré que acceder a los datos JSON para cada página dinámica generada durante el tiempo de compilación.

 /pages/sitemap.xml.js const staticPages = fs .readdirSync("pages") .filter((staticPage) => { return ![ "_app.js", "_document.js", "_error.js", "sitemap.xml.js", ].includes(staticPage); }) .map((staticPagePath) => { return `${baseUrl}/${staticPagePath}`; }); const sitemap = `<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> ${staticPages .map((url) => { return ` <url> <loc>${url}</loc> <lastmod>${new Date().toISOString()}</lastmod> <changefreq>monthly</changefreq> <priority>1.0</priority> </url> `; }) .join("")} </urlset> `
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Aparentemente, Next.js guarda todo el JSON de cada página dinámica en .next/server/pages/dynamicPage y podemos usar node fs para leer los archivos y luego analizarlos. Esto resolvió el problema.

Entonces, el enfoque que funcionó para mi caso de uso:

 const staticPages = fs .readdirSync('.next/server/pages/scheme') .filter((file) => path.extname(file).toLowerCase() === '.json'); const schemeFiles = ['https://schemes.openbudgetsindia.org']; staticPages.forEach((staticPagePath) => { const schemeName = staticPagePath.split('.')[0]; schemeFiles.push( `https://schemes.openbudgetsindia.org/scheme/${schemeName}` ); const schemeObj = JSON.parse( fs.readFileSync(`.next/server/pages/scheme/${staticPagePath}`, 'utf8') ); const { indicators } = schemeObj.pageProps.scheme.metadata; indicators.forEach((indicator) => schemeFiles.push( `https://schemes.openbudgetsindia.org/scheme/${schemeName}?indicator=${indicator}` ) ); });

Después de eso, simplemente cree los datos XML :

 const sitemap = `<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> ${staticPages .map((url) => { return ` <url> <loc>${url}</loc> <lastmod>${new Date().toISOString()}</lastmod> <changefreq>monthly</changefreq> <priority>1.0</priority> </url> `; }) .join("")} </urlset> res.setHeader('Content-Type', 'text/xml'); res.write(sitemap); res.end();
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!