SergeyMyssak / nextjs-sitemap

Generate dynamic sitemap.xml for Next.js projects following the example of Google!
122 stars 5 forks source link

Nextjs i18n defaultLocale not supported #2

Closed leo-cheron closed 3 years ago

leo-cheron commented 3 years ago

Nextjs supports default locale natively.

Using langs sitemap option with a nexjs defaultLocale will generate wrong path for the default locale, as it should not include locale information in the generated url.

hermanskurichin commented 3 years ago

As i understand next.js will handle default locale with sub-path as well. So it should work.

Perhaps only issue is it could be added as alternate link.

Also interconnected: https://github.com/vercel/next.js/discussions/18419 https://github.com/vercel/next.js/discussions/18834

leo-cheron commented 3 years ago

Default sub path locale is something most won't want. In that regard, nextjs-sitemap should also support default language without subpath.

MaksPapirovnyk commented 3 years ago

Dynamic pages with languages fast variant

Folders structure in your public, according to your langs public/xml/ca

Here you can write all your exclude pages

const excludePages = [
  '/api/*',
  '/accounts/[currency]/deposit',
];

You may need to group your pages data

const dynamicPagesTypes = {
  common: {
    parentPage: 'accounts',
    restPath: 'deposit',
  },
};

Here you can write your dynamic pages data for path, it is may be some request to API for get data

const dynamicData = {
  accounts: ['EUR', 'USD', 'CAD'],
};

Here we describe path, if you have two level nesting use restPath www.web.com/accounts/[CURRENCY]/deposit or www.web.com/accounts/[CURRENCY]/[TYPE]

const dynamicPagesTypes = {
  common: {
    parentPage: 'accounts',
    restPath: 'deposit',
  },
};

Default config for configureSitemap

const DEFAULT_GENERATOR_CONFIG = {
  excludeIndex: true,
  isSubdomain: false,
  exclude: excludePages,
  pagesDirectory: `${__dirname }/pages/`,
};

Generate path function with two level nesting

function generatePages(key = '') {
  const { parentPage, restPath } = dynamicPagesTypes[key];

  return dynamicData[parentPage].map((data) => (restPath ? `/${parentPage}/${data}/${restPath}` : `/${parentPage}/${data}`));
}

Here we get path data, if you group your path data you can add new type

async function getDynamicPaths() {
  const common = generatePages('common');

  return [...common];
}

Languages you need generate, according to folding Don’t forget create folders in public const languagesPath = ['/', '/en', '/de', '/ja', '/nz', '/ca'];

Run generation in loop of languagesPath

getDynamicPaths()
  .then((paths) => {
    for (const directory of languagesPath) {
      console.log(directory);
      const Sitemap = configureSitemap({
        include: paths,
        baseUrl: directory === '/' ? 'https://website.com' : `website.com${directory}`,
        targetDirectory: `${__dirname }/public/xml${directory}`,
        ...DEFAULT_GENERATOR_CONFIG,
      });
      Sitemap.generateSitemap();
    }
  });
thiras commented 3 years ago

+1 for support

fabien commented 3 years ago

@mrgnou @thiras see https://github.com/fabien/nextjs-sitemap for a fork with proper defaultLocale urls.

SergeyMyssak commented 3 years ago

Hello @mrgnou!

This functionality has been implemented in version 2.0.3. I'll close this issue, but if you have any questions feel free to ask