|
1 |
| -import type { SitemapUrlInput } from '@nuxtjs/sitemap/dist/runtime/types' |
| 1 | +import type { HydraCollection, JsonLdObject, RoadizRequestParams, RoadizTranslation } from '@roadiz/types' |
| 2 | +import { asSitemapUrl, defineSitemapEventHandler } from '#imports' |
| 3 | +import { hydraCollectionFetch } from '~/utils/hydra-collection-fetch' |
| 4 | +import { useApiUrl } from '~/composables/use-api-url' |
2 | 5 |
|
3 |
| -export default defineSitemapEventHandler(async (_event) => { |
4 |
| - const result: SitemapUrlInput[] = [] |
| 6 | +type ReachableEntity = JsonLdObject & { url?: string } |
5 | 7 |
|
6 |
| - return result |
| 8 | +const apiFetch = $fetch.create({ |
| 9 | + method: 'GET', |
| 10 | + headers: { |
| 11 | + 'accept-encoding': 'gzip, deflate', |
| 12 | + Accept: 'application/ld+json', |
| 13 | + }, |
| 14 | + baseURL: useApiUrl(), // Auto imports within the server folder aren't supported |
| 15 | +}) |
| 16 | + |
| 17 | +function fetchAllByLocale(path: string, _locale = 'fr', params: RoadizRequestParams = {}): Promise<ReachableEntity[]> { |
| 18 | + return hydraCollectionFetch<ReachableEntity>( |
| 19 | + path, |
| 20 | + { |
| 21 | + params: { |
| 22 | + properties: ['url'], |
| 23 | + _locale, |
| 24 | + ...params, |
| 25 | + }, |
| 26 | + }, |
| 27 | + apiFetch, |
| 28 | + ) |
| 29 | +} |
| 30 | + |
| 31 | +function fetchResourcesByLocale(locale: string) { |
| 32 | + const nodes = fetchAllByLocale('/nodes_sources', locale, { |
| 33 | + 'node.nodeType.reachable': true, |
| 34 | + 'node.visible': true, |
| 35 | + noIndex: false, |
| 36 | + }) |
| 37 | + |
| 38 | + // const today = new Date() |
| 39 | + // today.setFullYear(today.getFullYear() - 2) |
| 40 | + // const events = fetchAllByLocale('/events', locale, { |
| 41 | + // 'sortingDateTime[after]': today.getFullYear() + '-' + today.getMonth() + '-' + today.getDate(), |
| 42 | + // }) |
| 43 | + // |
| 44 | + // const peoples = fetchAllByLocale('/people', locale, { |
| 45 | + // 'exists[description]': true, |
| 46 | + // 'order[familyName]': 'asc', |
| 47 | + // }) |
| 48 | + |
| 49 | + return [nodes] |
| 50 | +} |
| 51 | + |
| 52 | +export default defineSitemapEventHandler(async () => { |
| 53 | + const locales = await apiFetch<HydraCollection<RoadizTranslation>>('/translations', { |
| 54 | + params: { available: true }, |
| 55 | + }).then((response) => response['hydra:member'].map(({ locale }) => locale)) |
| 56 | + |
| 57 | + const resourcesLocalized = locales.map((locale) => fetchResourcesByLocale(locale)).flat() |
| 58 | + const resources = (await Promise.all(resourcesLocalized)).flat() |
| 59 | + |
| 60 | + return resources.filter((r) => r?.url).map((resource) => asSitemapUrl(resource.url as string)) |
7 | 61 | })
|
0 commit comments