42 lines
1.5 KiB
TypeScript
42 lines
1.5 KiB
TypeScript
import { MetadataRoute } from 'next';
|
|
import { createAdminClient } from '@/lib/appwrite.server';
|
|
import { APPWRITE_ALMEIRA_CONFIG } from '@/lib/appwrite';
|
|
import { Query } from 'node-appwrite';
|
|
|
|
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
|
|
const baseUrl = process.env.NEXT_PUBLIC_SITE_URL || 'https://almeira.com.co';
|
|
|
|
// Static routes
|
|
const routes = ['', '/clasificados', '/eventos', '/tv', '/radio'].map((route) => ({
|
|
url: `${baseUrl}${route}`,
|
|
lastModified: new Date(),
|
|
changeFrequency: 'daily' as const,
|
|
priority: 1,
|
|
}));
|
|
|
|
// Dynamic posts
|
|
let postRoutes: MetadataRoute.Sitemap = [];
|
|
try {
|
|
const client = await createAdminClient();
|
|
const dbId = APPWRITE_ALMEIRA_CONFIG.databaseId;
|
|
|
|
// Fetch all published posts
|
|
const postsRes = await client.databases.listDocuments(dbId, 'posts', [
|
|
Query.equal('estado', ['Publicado', 'publicado']),
|
|
Query.limit(1000), // Get up to 1000 posts. For more, pagination is needed.
|
|
Query.orderDesc('fecha_publicacion')
|
|
]);
|
|
|
|
postRoutes = postsRes.documents.map((post) => ({
|
|
url: `${baseUrl}/${post.slug || post.$id}`,
|
|
lastModified: new Date(post.$updatedAt || post.fecha_publicacion || post.$createdAt),
|
|
changeFrequency: 'weekly' as const,
|
|
priority: 0.7,
|
|
}));
|
|
} catch (e) {
|
|
console.error('Error generating sitemap for posts:', e);
|
|
}
|
|
|
|
return [...routes, ...postRoutes];
|
|
}
|