#Rendering XML

6 messages · Page 1 of 1 (latest)

gloomy shoal
#

How can I create an /sitemap.xml route that returns XML instead of HTML? I tried using a server route but I can't use my composable there to fetch the sitemap from GraphQL.

sly thistle
#

what's in your composable that you can't use it in a server route or server plugin

gloomy shoal
#

Not much special, imho:

export const useStatamicSitemap = async (options: Options): Promise<Sitemap> => {
  const { $statamic } = useNuxtApp();

  if (!options.site) {
    options.site = $statamic.site.handle;
  }

  const { data } = await useAsyncQuery<{sitemap: Sitemap}>(sitemapQuery, options);
  const sitemap = data?.value?.sitemap;

  if (!sitemap) {
    throw new Error('Missing sitemap from Statamic.');
  }

  return sitemap;
};
sly thistle
#

any way to get the site handle without useNuxtApp and also use $fetch instead of useAsyncQuery ?

gloomy shoal
#

Not sure if I can. useAsyncQuery is the way to fetch graphql data.

sly thistle
#

useAsyncQuery is a wrapper around $fetch