@viscid wolf
import { NextResponse } from "next/server";
import path from "path";
import generateSitemap from "../../scripts/sitemap-generation";
export async function GET() {
// Generate your sitemap
await generateSitemap();
// Read the generated sitemap file
const sitemap = fs.readFileSync(
path.join(__dirname, "../../app/sitemap.ts"),
"utf8"
);
// Create a new NextResponse and set the appropriate headers
const response = new NextResponse(sitemap)
.withHeader("Content-Type", "text/xml")
.withHeader("Cache-control", "stale-while-revalidate, s-maxage=3600");
// Return the response
return response;
}
For context this is my generatesitemap:
require("dotenv").config({ path: ".env.local" });
const fs = require("fs");
const path = require("path");
const contentful = require("contentful");
const client = contentful.createClient({
space: process.env.NEXT_PUBLIC_CONTENTFUL_SPACE_ID,
accessToken: process.env.NEXT_PUBLIC_CONTENTFUL_ACCESS_TOKEN,
});
// Helper function to format dates
const formatDate = (date) => {
const d = new Date(date);
return ${d.getFullYear()}-${(d.getMonth() + 1) .toString() .padStart(2, "0")}-${d.getDate().toString().padStart(2, "0")};
};
async function generateSitemap() {
try {
// Static URLs for your site
const staticUrls = [
{
url: "/",
lastModified: new Date("2023-12-21"),
changeFrequency: "yearly",
priority: 1.0,
},
{
url: "/jasa-pengiriman-china-indonesia-laut",
lastModified: new Date("2023-12-21"),
changeFrequency: "yearly",
priority: 0.8,
},
{
url: "/jasa-pengiriman-china-indonesia-udara",
lastModified: new Date("2023-12-21"),
changeFrequency: "monthly",
priority: 0.8,
},
{
url: "/jasa-belanja-dari-china",
lastModified: new Date("2023-12-21"),
changeFrequency: "monthly",
priority: 0.8,
},
{
url: "/blog",
lastModified: new Date("2023-12-21"),
changeFrequency: "weekly",
priority: 0.8,
},
{
url: "/tracking",
lastModified: new Date("2023-12-21"),
changeFrequency: "monthly",
priority: 0.8,
},
];
// Fetch dynamic blog post URLs from Contentful
const response = await client.getEntries({
content_type: "blogPost",
select: "fields.slug,sys.updatedAt",
});
// Prepare sitemap array
let sitemapArray = staticUrls.map((url) => ({
url: `https://emporiumcargo.com${url.url}`,
lastModified: formatDate(url.lastModified),
changeFrequency: url.changeFrequency,
priority: url.priority,
}));
// Add dynamic URLs from Contentful
response.items.forEach((item) => {
sitemapArray.push({
url: `https://emporiumcargo.com/blog/${item.fields.slug}`,
lastModified: item.sys.updatedAt
? formatDate(item.sys.updatedAt)
: "2023-01-01",
changeFrequency: "weekly",
priority: 0.8,
});
});
// Generate TypeScript code
let tsCode = `import { MetadataRoute } from "next";\n\nexport default function sitemap(): MetadataRoute.Sitemap {\n return [\n`;
sitemapArray.forEach((item) => {
tsCode += ` {\n url: "${item.url}",\n lastModified: "${item.lastModified}",\n changeFrequency: "${item.changeFrequency}",\n priority: ${item.priority}\n },\n`;
});
tsCode += ` ];\n}\n`;
// Write to sitemap.ts file
const sitemapPath = path.join(__dirname, "../sitemap.ts");
fs.writeFileSync(sitemapPath, tsCode);
console.log("Sitemap TS file generated successfully.");
} catch (error) {
console.error("Error generating sitemap TS file:", error);
}
}
// generateSitemap();
module.exports = generateSitemap;
Can you update this code to make it proper?
Right now i am getting:
Property 'withHeader' does not exist on type