diff --git a/.gitignore b/.gitignore index 4e6e72b1..7ae44ddd 100644 --- a/.gitignore +++ b/.gitignore @@ -9,9 +9,9 @@ /coverage # Sitemap file -sitemap.xml -sitemap-0.xml -robots.txt +public/sitemap.xml +public/sitemap-0.xml +public/robots.txt # Local Scraping /docsearch-scraper diff --git a/next-sitemap.config.js b/next-sitemap.config.js index 49071d07..cd541e85 100644 --- a/next-sitemap.config.js +++ b/next-sitemap.config.js @@ -1,33 +1,28 @@ /** @type {import('next').NextConfig} */ -// eslint-disable-next-line @typescript-eslint/no-var-requires -const fs = require('fs') const siteUrl = process.env.NEXT_PUBLIC_DOMAIN_URL module.exports = { + siteUrl, transform: async (config, path) => { - const noIndexRegex = /()/gm - const filepath = `./.next/server/pages/${path}.html` - - if (fs.existsSync(filepath)) { - try { - const data = await fs.promises.readFile(filepath, 'utf8') - - if (data.match(noIndexRegex)) return null - } catch (error) { - console.error('err', error) - } - } - return { loc: path, changefreq: config.changefreq, priority: config.priority, lastmod: config.autoLastmod ? new Date().toISOString() : undefined, - alternateRefs: config.alternateRefs ?? [], + alternateRefs: [ + { + href: `${siteUrl}/pt/${path}`, + hreflang: 'pt', + }, + { + href: `${siteUrl}/es/${path}`, + hreflang: 'es', + }, + ], } }, - siteUrl, - exclude: ['/404', '/500', '/docs/api-reference*', '/server-sitemap.xml'], + generateIndexSitemap: false, + exclude: ['/404', '/*/404', '/500', '/*/500', 'server-sitemap.xml'], generateRobotsTxt: true, robotsTxtOptions: { policies: [ @@ -37,6 +32,6 @@ module.exports = { }, { userAgent: '*', allow: '/' }, ], - additionalSitemaps: [`${siteUrl}server-sitemap.xml`], + additionalSitemaps: [`${siteUrl}/server-sitemap.xml`], }, } diff --git a/package.json b/package.json index 1e743415..be22b6c3 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ "next": "^13.0.5", "next-mdx-remote": "^4.1.0", "next-plugin-preval": "^1.2.4", - "next-sitemap": "^3.1.42", + "next-sitemap": "^4.2.3", "node-fetch": "^3.2.10", "oas": "^20.4.0", "octokit": "^2.0.7", diff --git a/src/pages/server-sitemap.xml/index.ts b/src/pages/server-sitemap.xml/index.ts new file mode 100644 index 00000000..ce91b58a --- /dev/null +++ b/src/pages/server-sitemap.xml/index.ts @@ -0,0 +1,64 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +import { GetServerSidePropsContext } from 'next' +import { ISitemapField, getServerSideSitemapLegacy } from 'next-sitemap' +import getNavigation from 'utils/getNavigation' + +const DOMAIN_URL = 'https://leafy-mooncake-7c2e5e.netlify.app' + +function getEndpoint(element: any, slugPrefix: string) { + let urls: ISitemapField[] = [] + if (element.children) { + const children = element.children.flatMap((e: any) => { + return getEndpoint(e, slugPrefix) + }) + urls = children + } + + if (element.type === 'markdown') { + const url: ISitemapField = { + loc: '', + } + url.loc = `${DOMAIN_URL}/${slugPrefix}/${element.slug}` + url.lastmod = new Date().toISOString() + url.alternateRefs = [ + { + href: `${DOMAIN_URL}/pt/${slugPrefix}/${element.slug}`, + hreflang: 'pt', + }, + { + href: `${DOMAIN_URL}/es/${slugPrefix}/${element.slug}`, + hreflang: 'es', + }, + ] + urls.push(url) + } + return urls +} + +export async function getServerSideProps(ctx: GetServerSidePropsContext) { + let urls: ISitemapField[] = [] + const documentations = await getNavigation() + + for (let i = 0; i < documentations.length; i++) { + const documentation = documentations[i] + for (let j = 0; j < documentation.categories.length; j++) { + const category = documentation.categories[j] + urls = urls.concat(getEndpoint(category, documentation.slugPrefix)) + } + } + + ctx.res.setHeader( + 'Cache-control', + 'public, s-maxage=10, stale-while-revalidate=59' + ) + + ctx.res.setHeader( + 'Netlify-CDN-Cache-Control', + 'public, s-maxage=10, stale-while-revalidate=59' + ) + + return await getServerSideSitemapLegacy(ctx, urls) +} + +// eslint-disable-next-line @typescript-eslint/no-empty-function +export default function SitemapIndex() {} diff --git a/yarn.lock b/yarn.lock index 2bbd9e0c..215c958f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1229,10 +1229,10 @@ dependencies: chalk "^4.0.0" -"@corex/deepmerge@^4.0.29": - version "4.0.29" - resolved "https://registry.npmjs.org/@corex/deepmerge/-/deepmerge-4.0.29.tgz" - integrity sha512-q/yVUnqckA8Do+EvAfpy7RLdumnBy9ZsducMUtZTvpdbJC7azEf1hGtnYYxm0QfphYxjwggv6XtH64prvS1W+A== +"@corex/deepmerge@^4.0.43": + version "4.0.43" + resolved "https://registry.yarnpkg.com/@corex/deepmerge/-/deepmerge-4.0.43.tgz#9bd42559ebb41cc5a7fb7cfeea5f231c20977dca" + integrity sha512-N8uEMrMPL0cu/bdboEWpQYb/0i2K5Qn8eCsxzOmxSggJbbQte7ljMRoXm917AbntqTGOzdTu+vP3KOOzoC70HQ== "@cspotcode/source-map-consumer@0.8.0": version "0.8.0" @@ -1831,6 +1831,11 @@ resolved "https://registry.npmjs.org/@next/env/-/env-13.0.5.tgz" integrity sha512-F3KLtiDrUslAZhTYTh8Zk5ZaavbYwLUn3NYPBnOjAXU8hWm0QVGVzKIOuURQ098ofRU4e9oglf3Sj9pFx5nI5w== +"@next/env@^13.4.3": + version "13.5.6" + resolved "https://registry.yarnpkg.com/@next/env/-/env-13.5.6.tgz#c1148e2e1aa166614f05161ee8f77ded467062bc" + integrity sha512-Yac/bV5sBGkkEXmAX5FWPS9Mmo2rthrOPRQQNfycJPkjUAUclomCPH7QFVCDQ4Mp2k2K1SSM6m0zrxYrOwtFQw== + "@next/eslint-plugin-next@13.0.6": version "13.0.6" resolved "https://registry.npmjs.org/@next/eslint-plugin-next/-/eslint-plugin-next-13.0.6.tgz" @@ -6360,6 +6365,17 @@ fast-glob@^3.2.11: merge2 "^1.3.0" micromatch "^4.0.4" +fast-glob@^3.2.12: + version "3.3.2" + resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.3.2.tgz#a904501e57cfdd2ffcded45e99a54fef55e46129" + integrity sha512-oX2ruAFQwf/Orj8m737Y5adxDQO0LAB7/S5MnxCdTNDd4p6BsyIVsv9JQsATbTSq8KHRpLwIHbVlUNatxd+1Ow== + dependencies: + "@nodelib/fs.stat" "^2.0.2" + "@nodelib/fs.walk" "^1.2.3" + glob-parent "^5.1.2" + merge2 "^1.3.0" + micromatch "^4.0.4" + fast-glob@^3.2.9: version "3.2.11" resolved "https://registry.npmjs.org/fast-glob/-/fast-glob-3.2.11.tgz" @@ -9340,12 +9356,12 @@ minimist@1.2.5, minimist@^1.2.0, minimist@^1.2.5: resolved "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz" integrity sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw== -minimist@^1.2.3, minimist@^1.2.7: +minimist@^1.2.3: version "1.2.7" resolved "https://registry.npmjs.org/minimist/-/minimist-1.2.7.tgz" integrity sha512-bzfL1YUZsP41gmu/qjrEk0Q6i2ix/cVeAhbCbqH9u3zYutS1cLg00qhrD0M2MVdCcx4Sc0UpP2eBWo9rotpq6g== -minimist@^1.2.6: +minimist@^1.2.6, minimist@^1.2.8: version "1.2.8" resolved "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz" integrity sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA== @@ -9472,13 +9488,15 @@ next-plugin-preval@^1.2.4: tsconfig-paths "^3.9.0" webpack "^5.56.0" -next-sitemap@^3.1.42: - version "3.1.43" - resolved "https://registry.npmjs.org/next-sitemap/-/next-sitemap-3.1.43.tgz" - integrity sha512-TshDAP69OjHwvszedQ/jBvOjzqy7Gm9NU3ZIP30X3e1qXsNVOVF5uzi9kQOI6v0uBG08QUOk9wKNSIk3zI/wjQ== +next-sitemap@^4.2.3: + version "4.2.3" + resolved "https://registry.yarnpkg.com/next-sitemap/-/next-sitemap-4.2.3.tgz#5db3f650351a51e84b9fd6b58c5af2f9257b5058" + integrity sha512-vjdCxeDuWDzldhCnyFCQipw5bfpl4HmZA7uoo3GAaYGjGgfL4Cxb1CiztPuWGmS+auYs7/8OekRS8C2cjdAsjQ== dependencies: - "@corex/deepmerge" "^4.0.29" - minimist "^1.2.7" + "@corex/deepmerge" "^4.0.43" + "@next/env" "^13.4.3" + fast-glob "^3.2.12" + minimist "^1.2.8" next-tick@1, next-tick@^1.1.0: version "1.1.0"