Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 41 additions & 37 deletions src/app/sitemap.xml/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ export async function GET() {

const staticPages: SitemapUrl[] = [
{ loc: `${siteUrl}/`, changefreq: 'daily', priority: 1.0 },
{ loc: `${siteUrl}/about`, changefreq: 'monthly', priority: 0.8 },
{ loc: `${siteUrl}/works`, changefreq: 'weekly', priority: 0.9 },
{ 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: 'monthly', priority: 0.8 },
{ loc: `${siteUrl}/categories`, changefreq: 'monthly', priority: 0.8 },
{ loc: `${siteUrl}/gallery`, changefreq: 'weekly', priority: 0.7 },
{ loc: `${siteUrl}/news`, changefreq: 'weekly', priority: 0.8 },
{ loc: `${siteUrl}/reviews`, changefreq: 'weekly', priority: 0.7 },
{ loc: `${siteUrl}/order-delivery`, changefreq: 'monthly', priority: 0.8 },
{ 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]
Expand All @@ -36,24 +36,24 @@ export async function GET() {
])

if (Array.isArray(works)) {
works.forEach((w: WorkFromServer) => {
if (w && (w.slug || w._id)) {
works.forEach((work: WorkFromServer) => {
if (work && (work.slug || work._id)) {
urls.push({
loc: `${siteUrl}/works/${w.slug || w._id}`,
lastmod: w._modified ? new Date(w._modified * 1000).toISOString() : undefined,
changefreq: 'monthly',
priority: 0.7,
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((w: WorkFromServer) => {
if (w && (w.slug || w._id)) {
inStock.forEach((work: WorkFromServer) => {
if (work && (work.slug || work._id)) {
urls.push({
loc: `${siteUrl}/in-stock/${w.slug || w._id}`,
lastmod: w._modified ? new Date(w._modified * 1000).toISOString() : undefined,
loc: `${siteUrl}/in-stock/${work.slug || work._id}`,
lastmod: work._modified ? new Date(work._modified * 1000).toISOString() : undefined,
changefreq: 'daily',
priority: 0.8,
})
Expand All @@ -62,42 +62,46 @@ export async function GET() {
}

if (Array.isArray(news)) {
news.forEach((n: NewsFromServer) => {
if (n && (n.slug || n._id)) {
news.forEach((item: NewsFromServer) => {
if (item && (item.slug || item._id)) {
urls.push({
loc: `${siteUrl}/news/${n.slug || n._id}`,
lastmod: n._modified ? new Date(n._modified * 1000).toISOString() : undefined,
changefreq: 'monthly',
priority: 0.6,
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((c: CategoryFromServer) => {
if (c && (c.slug || c._id)) {
categories.forEach((category: CategoryFromServer) => {
if (category && (category.slug || category._id)) {
urls.push({
loc: `${siteUrl}/categories/${c.slug || c._id}`,
lastmod: c._modified ? new Date(c._modified * 1000).toISOString() : undefined,
changefreq: 'monthly',
priority: 0.7,
loc: `${siteUrl}/categories/${category.slug || category._id}`,
lastmod: category._modified ? new Date(category._modified * 1000).toISOString() : undefined,
changefreq: 'daily',
priority: 0.8,
})
}
})
}
} catch (e) {
console.error('Sitemap generation error:', e)
} catch (err) {
console.error('Sitemap generation error:', err)
}

const xml = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
>
${urls
.map(
(u) => ` <url>
<loc>${u.loc}</loc>${u.lastmod ? `\n <lastmod>${u.lastmod}</lastmod>` : ''}
<changefreq>${u.changefreq}</changefreq>
<priority>${u.priority}</priority>
(url) => `<url>
<loc>${url.loc}</loc>
${url.lastmod ? `\n <lastmod>${url.lastmod}</lastmod>` : ''}
<changefreq>${url.changefreq}</changefreq>
<priority>${url.priority}</priority>
</url>`,
)
.join('\n')}
Expand Down
16 changes: 2 additions & 14 deletions src/lib/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,14 @@ interface FetchOptions {
tags?: string[]
}

/**
* Базовый URL для API запросов
* @returns базовый URL
*/
function getBaseUrl(): string {
if (typeof window === 'undefined') {
return process.env.INTERNAL_URL || 'http://localhost:3000'
}

return window.location.origin
}

/**
* Построение URL с учетом опций запроса
* @param path - путь на сервере
* @param options - параметры запроса
* @returns полный URL
*/
function buildUrl(path: string, options: FetchOptions = {}): string {
const baseUrl = process.env.INTERNAL_URL || 'http://localhost:3000'
const params = new URLSearchParams()

if (options.locale) params.set('locale', options.locale)
Expand All @@ -47,7 +36,6 @@ function buildUrl(path: string, options: FetchOptions = {}): string {
if (options.field) params.set('field', options.field)

const queryString = params.toString()
const baseUrl = getBaseUrl()
const fullPath = `${baseUrl}/api${path}`

return queryString ? `${fullPath}?${queryString}` : fullPath
Expand Down Expand Up @@ -215,11 +203,11 @@ export async function fetchCollectionCount(
model: string,
options: Pick<FetchOptions, 'filter' | 'cache' | 'revalidate' | 'tags'> = {},
): Promise<number> {
const baseUrl = process.env.INTERNAL_URL || 'http://localhost:3000'
const params = new URLSearchParams()

if (options.filter) params.set('filter', JSON.stringify(options.filter))

const baseUrl = getBaseUrl()
const queryString = params.toString()
const url = `${baseUrl}/api/content/collection/${model}/count${queryString ? `?${queryString}` : ''}`

Expand Down