Untitled

mail@pastecode.io avatar
unknown
javascript
2 years ago
1.4 kB
2
Indexable
Never
import { globby } from 'globby'
import prettier from 'prettier'

import { writeFileSync } from 'fs'

async function generate() {
  ;['pl', 'en'].forEach(async lang => {
    const pages = await globby([
      `./dist/apps/appjet/.next/server/pages/${lang}/**/*.html`,
      `!./dist/apps/appjet/.next/server/pages/${lang}/404.html`,
      `!./dist/apps/appjet/.next/server/pages/${lang}/500.html`,
    ])

    const siteUrl = lang === 'pl' ? 'https://appjet.io/pl' : 'https://appjet.io/en'

    const sitemap = `
    <?xml version="1.0" encoding="UTF-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
        <url>
            <loc>${siteUrl}</loc>            
            <lastmod>${new Date().toISOString()}</lastmod>
        </url>
        ${pages
          .map(page => {
            const route = page
              .replace(`./dist/apps/appjet/.next/server/pages/${lang}`, '')
              .replace('.html', '')

            return `<url>
                  <loc>${siteUrl}${route}</loc>
                  <lastmod>${new Date().toISOString()}</lastmod>                  
              </url>
            `
          })
          .join('')}
    </urlset>
    `

    const formatted = prettier.format(sitemap, {
      parser: 'html',
    })

    writeFileSync(`apps/appjet/public/sitemap-${lang}.xml`, formatted)
  })
}

generate()