-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: update sitemap content fetched
- Loading branch information
1 parent
63926cf
commit 694627a
Showing
1 changed file
with
58 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) |