Skip to content

Commit

Permalink
chore: update sitemap content fetched
Browse files Browse the repository at this point in the history
  • Loading branch information
timothejoubert committed Jun 10, 2024
1 parent 63926cf commit 694627a
Showing 1 changed file with 58 additions and 4 deletions.
62 changes: 58 additions & 4 deletions server/api/sitemap.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,61 @@
import type { SitemapUrlInput } from '@nuxtjs/sitemap/dist/runtime/types'
import type { HydraCollection, JsonLdObject, RoadizRequestParams, RoadizTranslation } from '@roadiz/types'
import { asSitemapUrl, defineSitemapEventHandler } from '#imports'
import { hydraCollectionFetch } from '~/utils/hydra-collection-fetch'
import { useApiUrl } from '~/composables/use-api-url'

export default defineSitemapEventHandler(async (_event) => {
const result: SitemapUrlInput[] = []
type ReachableEntity = JsonLdObject & { url?: string }

return result
const apiFetch = $fetch.create({
method: 'GET',
headers: {
'accept-encoding': 'gzip, deflate',
Accept: 'application/ld+json',
},
baseURL: useApiUrl(), // Auto imports within the server folder aren't supported
})

function fetchAllByLocale(path: string, _locale = 'fr', params: RoadizRequestParams = {}): Promise<ReachableEntity[]> {
return hydraCollectionFetch<ReachableEntity>(
path,
{
params: {
properties: ['url'],
_locale,
...params,
},
},
apiFetch,
)
}

function fetchResourcesByLocale(locale: string) {
const nodes = fetchAllByLocale('/nodes_sources', locale, {
'node.nodeType.reachable': true,
'node.visible': true,
noIndex: false,
})

// const today = new Date()
// today.setFullYear(today.getFullYear() - 2)
// const events = fetchAllByLocale('/events', locale, {
// 'sortingDateTime[after]': today.getFullYear() + '-' + today.getMonth() + '-' + today.getDate(),
// })
//
// const peoples = fetchAllByLocale('/people', locale, {
// 'exists[description]': true,
// 'order[familyName]': 'asc',
// })

return [nodes]
}

export default defineSitemapEventHandler(async () => {
const locales = await apiFetch<HydraCollection<RoadizTranslation>>('/translations', {
params: { available: true },
}).then((response) => response['hydra:member'].map(({ locale }) => locale))

const resourcesLocalized = locales.map((locale) => fetchResourcesByLocale(locale)).flat()
const resources = (await Promise.all(resourcesLocalized)).flat()

return resources.filter((r) => r?.url).map((resource) => asSitemapUrl(resource.url as string))
})

0 comments on commit 694627a

Please sign in to comment.