|
| 1 | +/** |
| 2 | + * Generate SiteMap for SEO Purpose |
| 3 | + * We choose to write the script instead of using next-sitemap library because |
| 4 | + * next-sitemap library doesn't handle Next.js dynamic path well and it's easier |
| 5 | + * to maintain the small script on our own. |
| 6 | + * src: https://leerob.io/blog/nextjs-sitemap-robots |
| 7 | + */ |
| 8 | + |
| 9 | +import dotenv from 'dotenv-safe'; |
| 10 | +import { globby } from 'globby'; |
| 11 | +import prettier from 'prettier'; |
| 12 | +import { writeFileSync } from 'fs'; |
| 13 | +import path from 'path'; |
| 14 | + |
| 15 | +import { getContentPaths } from '../src/utils/getContentPaths'; |
| 16 | +import { getPageFromSlug } from '../src/utils/getPageFromSlug'; |
| 17 | +import { getPagesManifest } from '../src/utils/getPagesManifest'; |
| 18 | +import { META_INFO } from '../src/data/meta'; |
| 19 | +import { FRAMEWORKS } from '../src/data/frameworks'; |
| 20 | + |
| 21 | +dotenv.config(); |
| 22 | + |
| 23 | +async function generateSitemap() { |
| 24 | + const manifest = await getPagesManifest( |
| 25 | + getContentPaths, |
| 26 | + getPageFromSlug, |
| 27 | + META_INFO |
| 28 | + ); |
| 29 | + |
| 30 | + console.log('🗺 ▶️ SiteMap generating...'); |
| 31 | + const prettierConfig = await prettier.resolveConfig('./.prettierrc.js'); |
| 32 | + const pagesWithParam = await globby([ |
| 33 | + 'src/pages/**/index.page.tsx', |
| 34 | + 'src/pages/**/index.page.mdx', |
| 35 | + '!src/pages/_*.tsx', |
| 36 | + '!src/pages/404.page.tsx', |
| 37 | + ]); |
| 38 | + |
| 39 | + const pages = pagesWithParam |
| 40 | + .slice(1) |
| 41 | + .flatMap((p) => { |
| 42 | + p = p |
| 43 | + .replace('src/pages', '') |
| 44 | + .replace('.page.mdx', '') |
| 45 | + .replace('.page.tsx', '') |
| 46 | + .replace('/index', ''); |
| 47 | + |
| 48 | + return FRAMEWORKS.map((framework) => { |
| 49 | + const filepath = p.replace('[platform]', framework); |
| 50 | + const supportedFrameworks = |
| 51 | + manifest[p].frontmatter.supportedFrameworks === 'all' |
| 52 | + ? FRAMEWORKS |
| 53 | + : manifest[p].frontmatter.supportedFrameworks.split('|'); |
| 54 | + if (supportedFrameworks.includes(framework)) { |
| 55 | + return filepath; |
| 56 | + } else { |
| 57 | + console.log( |
| 58 | + `ⓧ ${filepath} does not support ${framework}. Skipping adding to sitemap.` |
| 59 | + ); |
| 60 | + return ''; |
| 61 | + } |
| 62 | + }); |
| 63 | + }) |
| 64 | + .filter((el) => el); |
| 65 | + |
| 66 | + const sitemap = ` |
| 67 | + <?xml version="1.0" encoding="UTF-8"?> |
| 68 | + <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"> |
| 69 | + <url> |
| 70 | + <loc>https://ui.docs.amplify.aws</loc> |
| 71 | + <changefreq>weekly</changefreq> |
| 72 | + <priority>0.5</priority> |
| 73 | + <lastmod>2022-05-19T16:24:03.254Z</lastmod> |
| 74 | + </url> |
| 75 | + ${pages |
| 76 | + .map((path) => { |
| 77 | + const route = path === '/index' ? '' : path; |
| 78 | +
|
| 79 | + /** |
| 80 | + * The priority of this URL relative to other URLs on your site. |
| 81 | + * Valid values range from 0.0 to 1.0. This value does not affect |
| 82 | + * how your pages are compared to pages on other sites—it only lets |
| 83 | + * the search engines know which pages you deem most important for |
| 84 | + * the crawlers. |
| 85 | + * Source: https://www.sitemaps.org/protocol.html#prioritydef |
| 86 | + */ |
| 87 | + const prioritize = (path) => { |
| 88 | + const defaultPriority = 0.5; |
| 89 | + const isGetStarted = path.includes('getting-started') ? 0.1 : 0; |
| 90 | + const isReact = path.includes('react') ? 0.1 : 0; |
| 91 | +
|
| 92 | + return defaultPriority + isGetStarted + isReact; |
| 93 | + }; |
| 94 | + const priority = prioritize(route); |
| 95 | +
|
| 96 | + return ` |
| 97 | + <url> |
| 98 | + <loc>${'https://ui.docs.amplify.aws'}${route}</loc> |
| 99 | + <changefreq>weekly</changefreq> |
| 100 | + <priority>${priority}</priority> |
| 101 | + <lastmod>${new Date().toISOString()}</lastmod> |
| 102 | + </url> |
| 103 | + `; |
| 104 | + }) |
| 105 | + .join('')} |
| 106 | + </urlset> |
| 107 | + `; |
| 108 | + |
| 109 | + const formatted = prettier.format(sitemap, { |
| 110 | + ...prettierConfig, |
| 111 | + parser: 'html', |
| 112 | + }); |
| 113 | + |
| 114 | + // eslint-disable-next-line no-sync |
| 115 | + writeFileSync(path.resolve(__dirname, '../public/sitemap.xml'), formatted); |
| 116 | + console.log('🗺 ✅ SiteMap generated.'); |
| 117 | +} |
| 118 | + |
| 119 | +function generateRobotsTxt() { |
| 120 | + const isProd = |
| 121 | + process.env.SITE_URL && |
| 122 | + process.env.SITE_URL.startsWith('https://ui.docs.amplify.aws'); |
| 123 | + console.log( |
| 124 | + `🤖▶️ robots.txt generating for ${ |
| 125 | + isProd |
| 126 | + ? 'Prod. Googlebot is allowed.' |
| 127 | + : 'non-Prod. Googlebot is disallowed.' |
| 128 | + }...` |
| 129 | + ); |
| 130 | + const disallowTxt = `# * |
| 131 | +User-agent: Googlebot |
| 132 | +Disallow: / |
| 133 | +`; |
| 134 | + |
| 135 | + const txt = `${isProd ? '' : disallowTxt} |
| 136 | +User-agent: * |
| 137 | +Allow: / |
| 138 | +
|
| 139 | +# Host |
| 140 | +Host: ui.docs.amplify.aws |
| 141 | +
|
| 142 | +# Sitemaps |
| 143 | +Sitemap: ${'https://ui.docs.amplify.aws'} |
| 144 | +`; |
| 145 | + writeFileSync(path.resolve(__dirname, '../public/robots.txt'), txt); |
| 146 | + console.log('🤖✅ robots.txt generated.'); |
| 147 | +} |
| 148 | + |
| 149 | +generateSitemap(); |
| 150 | +generateRobotsTxt(); |
0 commit comments