Add sitemap.xml
This commit is contained in:
93
src/routes/sitemap.xml/+server.ts
Normal file
93
src/routes/sitemap.xml/+server.ts
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
import { error } from '@sveltejs/kit'
|
||||||
|
import type { RequestHandler } from './$types'
|
||||||
|
import { fetchAPI } from '$utils/api'
|
||||||
|
|
||||||
|
|
||||||
|
export const GET: RequestHandler = async ({ url, setHeaders }) => {
|
||||||
|
try {
|
||||||
|
const locations = []
|
||||||
|
const products = []
|
||||||
|
|
||||||
|
// Get dynamic data from API
|
||||||
|
const res = await fetchAPI(`query {
|
||||||
|
locations: location (filter: { status: { _eq: "published" }}) {
|
||||||
|
slug
|
||||||
|
country { slug }
|
||||||
|
}
|
||||||
|
products: product (filter: { status: { _eq: "published" }}) {
|
||||||
|
location { slug }
|
||||||
|
}
|
||||||
|
}`)
|
||||||
|
|
||||||
|
if (res) {
|
||||||
|
const { data } = res
|
||||||
|
locations.push(...data.locations)
|
||||||
|
products.push(...data.products)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Static pages
|
||||||
|
const pages = [
|
||||||
|
['/', 1.0, 'daily'],
|
||||||
|
['/photos', 1.0, 'daily'],
|
||||||
|
['/locations', 0.6, 'weekly'],
|
||||||
|
['/shop', 0.8, 'weekly'],
|
||||||
|
['/about', 0.6, 'weekly'],
|
||||||
|
['/terms', 0.6, 'weekly'],
|
||||||
|
['/subscribe', 0.6, 'weekly'],
|
||||||
|
['/credits', 0.6, 'monthly'],
|
||||||
|
]
|
||||||
|
|
||||||
|
// All pages
|
||||||
|
const allPages = [
|
||||||
|
// Static pages
|
||||||
|
...pages.map(([path, priority, frequency]) => ({
|
||||||
|
path,
|
||||||
|
priority,
|
||||||
|
frequency,
|
||||||
|
})),
|
||||||
|
|
||||||
|
// Locations
|
||||||
|
...locations.map(({ slug, country }) => ({
|
||||||
|
path: `/${country.slug}/${slug}`,
|
||||||
|
priority: 0.7,
|
||||||
|
frequency: 'monthly',
|
||||||
|
})),
|
||||||
|
|
||||||
|
// Products
|
||||||
|
...products.map(({ location: { slug }}) => ({
|
||||||
|
path: `/shop/poster-${slug}`,
|
||||||
|
priority: 0.7,
|
||||||
|
frequency: 'monthly',
|
||||||
|
})),
|
||||||
|
]
|
||||||
|
|
||||||
|
const sitemap = render(url.origin, allPages)
|
||||||
|
|
||||||
|
setHeaders({
|
||||||
|
'Content-Type': 'application/xml',
|
||||||
|
'Cache-Control': 'max-age=0, s-max-age=600',
|
||||||
|
})
|
||||||
|
|
||||||
|
return new Response(sitemap)
|
||||||
|
} catch (err) {
|
||||||
|
throw error(500, err.message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const render = (origin: string, pages: any[]) => {
|
||||||
|
return `<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<urlset
|
||||||
|
xmlns="https://www.sitemaps.org/schemas/sitemap/0.9"
|
||||||
|
xmlns:xhtml="https://www.w3.org/1999/xhtml"
|
||||||
|
xmlns:mobile="https://www.google.com/schemas/sitemap-mobile/1.0"
|
||||||
|
xmlns:news="https://www.google.com/schemas/sitemap-news/0.9"
|
||||||
|
xmlns:image="https://www.google.com/schemas/sitemap-image/1.1"
|
||||||
|
xmlns:video="https://www.google.com/schemas/sitemap-video/1.1"
|
||||||
|
>
|
||||||
|
${pages.map(({ path, priority, frequency }) => `<url>
|
||||||
|
<loc>${origin}${path}</loc>
|
||||||
|
<priority>${priority}</priority>
|
||||||
|
<changefreq>${frequency}</changefreq>
|
||||||
|
</url>`).join('')}
|
||||||
|
</urlset>`
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user