New Shopify Certification now available: Liquid Storefronts for Theme Developers

I can't add the sitemap.xml file with 'getProductOperation' and 'getProductsQuery'

burrakkozcaan
Visitor
1 0 0

I am using the next-sitemap package
api/pages/server-sitemap.ts looks like this:

import { NextApiRequest, NextApiResponse } from 'next';
import { getServerSideSitemap, ISitemapField } from 'next-sitemap';
import getAllProductsOperation from '../../../packages/shopify/dist/api/operations/get-all-products';
import getAllProductsQuery from "../../../packages/shopify/dist/utils/queries/get-all-products-query";

const generateSitemap = async (req: NextApiRequest, res: NextApiResponse) => {
  try {
    const getAllProducts = getAllProductsOperation({
      commerce: {
        locale: 'tr-TR',
        shopId: 'yukselensepets',
      },
    });

    const { products } = await getAllProducts({
      query: getAllProductsQuery,
      variables: {},
      config: {},
    });

    const productURLs: ISitemapField[] = products.map((product: { slug: string; updatedAt: Date }) => ({
      loc: `https://www.example.com/product/${product.slug}`,
      lastmod: product.updatedAt.toISOString(),
      changefreq: 'daily',
      priority: '0.7',
    }));

    const sitemapContent = await getServerSideSitemap(productURLs);

    res.setHeader('Content-Type', 'text/xml');
    res.write(sitemapContent);

    res.end();
  } catch (error) {
    console.error('Sitemap oluşturma hatası:', error);
    res.status(500).end();
  }
};

export default generateSitemap;

and next-server.config.js in my root folder:

const siteUrl = "https://www.example.com/";

module.exports = {
  siteUrl,
  generateRobotsTxt: true,
  robotsTxtOptions: {
    policies: [
      { userAgent: "*", allow: "/" },
    ],
    additionalSitemaps: [
      `${siteUrl}/sitemap.xml`,
      `${siteUrl}/server-sitemap.xml`,
    ],
  },

};

How can I create it? can you help me please?
 i can't output..

Replies 0 (0)