From df0fccd8eee4499292e0eedafb1748f002ce2592 Mon Sep 17 00:00:00 2001 From: Yuriy Plotnikov Date: Mon, 18 May 2026 14:03:34 +0300 Subject: [PATCH] Fix sitemap --- package.json | 1 - src/app/sitemap.ts | 87 ++++++++++++++++++++++++++ src/app/sitemap.xml/route.ts | 116 ----------------------------------- 3 files changed, 87 insertions(+), 117 deletions(-) create mode 100644 src/app/sitemap.ts delete mode 100644 src/app/sitemap.xml/route.ts diff --git a/package.json b/package.json index ce60590d..5067071f 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,6 @@ "isomorphic-dompurify": "^2.28.0", "lightgallery": "^2.9.0", "next": "^15.5.4", - "next-sitemap": "^4.2.3", "react": "19.1.0", "react-dom": "19.1.0", "react-masonry-css": "^1.0.16", diff --git a/src/app/sitemap.ts b/src/app/sitemap.ts new file mode 100644 index 00000000..46504c49 --- /dev/null +++ b/src/app/sitemap.ts @@ -0,0 +1,87 @@ +import type { MetadataRoute } from 'next' +import { fetchCollection } from '@/lib/api-client' +import type { WorkFromServer, NewsFromServer, CategoryFromServer } from '@/types/types' + +export default async function sitemap(): Promise { + const siteUrl = process.env.SITE_URL || process.env.NEXT_PUBLIC_SITE_URL || '' + + const staticPages: MetadataRoute.Sitemap = [ + { url: `${siteUrl}/`, lastModified: new Date(), changeFrequency: 'daily', priority: 1.0 }, + { url: `${siteUrl}/about`, lastModified: new Date(), changeFrequency: 'daily', priority: 0.9 }, + { url: `${siteUrl}/works`, lastModified: new Date(), changeFrequency: 'daily', priority: 0.9 }, + { url: `${siteUrl}/in-stock`, lastModified: new Date(), changeFrequency: 'daily', priority: 0.9 }, + { url: `${siteUrl}/contacts`, lastModified: new Date(), changeFrequency: 'daily', priority: 0.9 }, + { url: `${siteUrl}/categories`, lastModified: new Date(), changeFrequency: 'daily', priority: 0.9 }, + { url: `${siteUrl}/gallery`, lastModified: new Date(), changeFrequency: 'daily', priority: 0.9 }, + { url: `${siteUrl}/news`, lastModified: new Date(), changeFrequency: 'daily', priority: 0.9 }, + { url: `${siteUrl}/reviews`, lastModified: new Date(), changeFrequency: 'daily', priority: 0.9 }, + { url: `${siteUrl}/order-delivery`, lastModified: new Date(), changeFrequency: 'daily', priority: 0.9 }, + ] + + const urls: MetadataRoute.Sitemap = [...staticPages] + + try { + const [works, news, categories, inStock] = await Promise.all([ + fetchCollection('works'), + fetchCollection('news'), + fetchCollection('category'), + fetchCollection('works', { filter: { in_stock: true } }), + ]) + + if (Array.isArray(works)) { + works.forEach((work: WorkFromServer) => { + if (work && (work.slug || work._id)) { + urls.push({ + url: `${siteUrl}/works/${work.slug || work._id}`, + lastModified: work._modified ? new Date(work._modified * 1000) : new Date(), + changeFrequency: 'daily', + priority: 0.8, + }) + } + }) + } + + if (Array.isArray(inStock)) { + inStock.forEach((work: WorkFromServer) => { + if (work && (work.slug || work._id)) { + urls.push({ + url: `${siteUrl}/in-stock/${work.slug || work._id}`, + lastModified: work._modified ? new Date(work._modified * 1000) : new Date(), + changeFrequency: 'daily', + priority: 0.8, + }) + } + }) + } + + if (Array.isArray(news)) { + news.forEach((item: NewsFromServer) => { + if (item && (item.slug || item._id)) { + urls.push({ + url: `${siteUrl}/news/${item.slug || item._id}`, + lastModified: item._modified ? new Date(item._modified * 1000) : new Date(), + changeFrequency: 'daily', + priority: 0.8, + }) + } + }) + } + + if (Array.isArray(categories)) { + categories.forEach((category: CategoryFromServer) => { + if (category && (category.slug || category._id)) { + urls.push({ + url: `${siteUrl}/categories/${category.slug || category._id}`, + lastModified: category._modified ? new Date(category._modified * 1000) : new Date(), + changeFrequency: 'daily', + priority: 0.8, + }) + } + }) + } + } catch (err) { + console.error('Sitemap generation error:', err) + } + + return urls +} \ No newline at end of file diff --git a/src/app/sitemap.xml/route.ts b/src/app/sitemap.xml/route.ts deleted file mode 100644 index 72d865b3..00000000 --- a/src/app/sitemap.xml/route.ts +++ /dev/null @@ -1,116 +0,0 @@ -import { NextResponse } from 'next/server' -import { fetchCollection } from '@/lib/api-client' -import type { WorkFromServer, NewsFromServer, CategoryFromServer } from '@/types/types' - -type SitemapUrl = { - loc: string - lastmod?: string - changefreq: string - priority: number -} - -export async function GET() { - const siteUrl = process.env.SITE_URL || process.env.NEXT_PUBLIC_SITE_URL - - const staticPages: SitemapUrl[] = [ - { loc: `${siteUrl}/`, changefreq: 'daily', priority: 1.0 }, - { loc: `${siteUrl}/about`, changefreq: 'daily', priority: 0.9 }, - { loc: `${siteUrl}/works`, changefreq: 'daily', priority: 0.9 }, - { loc: `${siteUrl}/in-stock`, changefreq: 'daily', priority: 0.9 }, - { loc: `${siteUrl}/contacts`, changefreq: 'daily', priority: 0.9 }, - { loc: `${siteUrl}/categories`, changefreq: 'daily', priority: 0.9 }, - { loc: `${siteUrl}/gallery`, changefreq: 'daily', priority: 0.9 }, - { loc: `${siteUrl}/news`, changefreq: 'daily', priority: 0.9 }, - { loc: `${siteUrl}/reviews`, changefreq: 'daily', priority: 0.9 }, - { loc: `${siteUrl}/order-delivery`, changefreq: 'daily', priority: 0.9 }, - ] - - const urls: SitemapUrl[] = [...staticPages] - - try { - const [works, news, categories, inStock] = await Promise.all([ - fetchCollection('works'), - fetchCollection('news'), - fetchCollection('category'), - fetchCollection('works', { filter: { in_stock: true } }), - ]) - - if (Array.isArray(works)) { - works.forEach((work: WorkFromServer) => { - if (work && (work.slug || work._id)) { - urls.push({ - loc: `${siteUrl}/works/${work.slug || work._id}`, - lastmod: work._modified ? new Date(work._modified * 1000).toISOString() : undefined, - changefreq: 'daily', - priority: 0.8, - }) - } - }) - } - - if (Array.isArray(inStock)) { - inStock.forEach((work: WorkFromServer) => { - if (work && (work.slug || work._id)) { - urls.push({ - loc: `${siteUrl}/in-stock/${work.slug || work._id}`, - lastmod: work._modified ? new Date(work._modified * 1000).toISOString() : undefined, - changefreq: 'daily', - priority: 0.8, - }) - } - }) - } - - if (Array.isArray(news)) { - news.forEach((item: NewsFromServer) => { - if (item && (item.slug || item._id)) { - urls.push({ - loc: `${siteUrl}/news/${item.slug || item._id}`, - lastmod: item._modified ? new Date(item._modified * 1000).toISOString() : undefined, - changefreq: 'daily', - priority: 0.8, - }) - } - }) - } - - if (Array.isArray(categories)) { - categories.forEach((category: CategoryFromServer) => { - if (category && (category.slug || category._id)) { - urls.push({ - loc: `${siteUrl}/categories/${category.slug || category._id}`, - lastmod: category._modified ? new Date(category._modified * 1000).toISOString() : undefined, - changefreq: 'daily', - priority: 0.8, - }) - } - }) - } - } catch (err) { - console.error('Sitemap generation error:', err) - } - - const xml = ` - - ${urls - .map( - (url) => ` - ${url.loc} - ${url.lastmod ? `\n ${url.lastmod}` : ''} - ${url.changefreq} - ${url.priority} - `, - ) - .join('\n')} - ` - - return new NextResponse(xml, { - headers: { - 'Content-Type': 'application/xml', - 'Cache-Control': 'public, max-age=3600, s-maxage=3600', - }, - }) -}