Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow es modules to be imported in sitemap-generator to use fetch or axios inside sitemap-generator.js ? #10

Open
SSylvain1989 opened this issue May 5, 2022 · 0 comments

Comments

@SSylvain1989
Copy link

SSylvain1989 commented May 5, 2022

Hello ,

Your documentation say we can implement dynamic paths in our sitemap https://github.com/SergeyMyssak/nextjs-sitemap#with-dynamic-routes
but i can't manage to do so ,
I need to fetch all my url blog from sanity.io

See your example

const { configureSitemap } = require('@sergeymyssak/nextjs-sitemap');

async function fetchDynamicPaths() {
  return ['house', 'flower', 'table'];
}

async function getDynamicPaths() {
  const paths = await fetchDynamicPaths();

  return paths.map((item) => `/project/${item}`);
}

getDynamicPaths().then((paths) => {
  const Sitemap = configureSitemap({
    domains: [{ domain: 'example.com', defaultLocale: 'en' }],
    include: paths,
    exclude: ['/project/*'],
    excludeIndex: true,
    pagesConfig: {
      '/project/*': {
        priority: '0.5',
        changefreq: 'daily',
      },
    },
    trailingSlash: true,
    targetDirectory: __dirname + '/public',
    pagesDirectory: __dirname + '/src/pages',
  });

  Sitemap.generateSitemap();
});

But if i want to import fetch module i get an error

const fetch = require("node-fetch")

const { configureSitemap } = require("@sergeymyssak/nextjs-sitemap")
const fs = require("fs")

async function fetchDynamicPaths() {
  const query = encodeURIComponent(`{
    "posts": *[_type == "post"]  {
      ...,
      categories[]->
    }}
  `)
  const url = `https://eb29jfco.api.sanity.io/v1/data/query/production?query=${query}`
  const result = await fetch(url).then((res) => res.json())
  const paths = []
  result.result.posts.map((element) => {
    return paths.push({
      slug: `${element.slug.current}`,
    })
  })

  return {
    paths,
  }
}

async function getDynamicPaths() {
  const paths = await fetchDynamicPaths()
  return paths.paths.map((item) => `/blog/${item.slug}`)
}
const paths = getDynamicPaths()
console.log(paths)

async function writeSitemap() {
  // const dynamicPaths = uniquePaths
  const publicDirectory = `${__dirname}/public`
  const domain = "www.myblog.app"
  const Sitemap = configureSitemap({
    domains: [
      {
        domain,
        locales: ["fr"],
        defaultLocale: "en",
      },
    ],
    // include: dynamicPaths,
    exclude: ["/advisor", "/404", "/api/*", "blog/*"],
    targetDirectory: publicDirectory,
    pagesDirectory: `${__dirname}/pages`,
  })
  const result = await Sitemap.generateSitemap()
  let sitemaps = ""
  result.forEach((r) => {
    sitemaps += `Sitemap: https://${domain}/${r.name}\n`
  })
  const robotTxt = `# *
User-agent: *
Allow: /
# Host
Host: https://${domain}
# Sitemaps
${sitemaps}`
  fs.writeFileSync(`${publicDirectory}/robots.txt`, robotTxt)
}
writeSitemap()

error
image

@SSylvain1989 SSylvain1989 changed the title How can i use fetch or axios inside sitemap-generator.js ? Allow es modules to be imported in sitemap-generator to use fetch or axios inside sitemap-generator.js ? May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant