Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Refactor: prebuild sitemap generation instead of statically cached dynamic sitemap (#760)

Ben Fornefeld592543937a2d
diff --git a/apps/web/.gitignore b/apps/web/.gitignore
index 9f7fdf7cb..f69977093 100644
--- a/apps/web/.gitignore
+++ b/apps/web/.gitignore
@@ -15,6 +15,7 @@
# production
/build
+/public/sitemap.xml
# misc
.DS_Store
diff --git a/apps/web/prebuild.js b/apps/web/prebuild.js
index 6a7553b5d..838e414aa 100644
--- a/apps/web/prebuild.js
+++ b/apps/web/prebuild.js
@@ -1,5 +1,6 @@
const fs = require('fs')
const path = require('path')
+const fg = require('fast-glob')
const sdkRefRoutesFilePath = './src/components/Navigation/sdkRefRoutes.json'
@@ -239,3 +240,86 @@ fs.writeFileSync(
)
console.log('\n\nSDK reference routes generated successfully')
+
+// Generate sitemap
+
+async function generateSitemap() {
+ try {
+ console.log('Generating sitemap...')
+ console.log('Current working directory:', process.cwd())
+
+ if (!process.cwd().endsWith('/apps/web')) {
+ throw new Error('Not running from apps/web directory')
+ }
+
+ const pattern =
+ 'src/app/\\(docs\\)/docs/**/page.mdx'
+
+
+ let mdxFiles = []
+ let patternsWithResults = []
+
+ const files = await fg(pattern, {
+ cwd: process.cwd(),
+ absolute: true,
+ })
+
+ console.log(`Pattern: ${pattern}, Found: ${files.length} files`)
+
+ if (files.length > 0) {
+ mdxFiles.push(...files)
+ patternsWithResults.push(pattern)
+ }
+
+ console.log(`Found ${mdxFiles.length} total files using patterns: ${patternsWithResults.join(', ')}`)
+
+ if (mdxFiles.length === 0) {
+ throw new Error(`No page.mdx files found with any pattern. Pattern: ${pattern}`)
+ }
+
+ const docsPages = []
+
+ for (const filePath of mdxFiles) {
+ const docsMatch = filePath.match(/\/app\/\(docs\)\/docs\/(.*)\/page\.mdx$/) ||
+ filePath.match(/\/app\/\(docs\)\/docs\/page\.mdx$/)
+
+ if (!docsMatch) {
+ throw new Error(`Unexpected file path format: ${filePath}`)
+ }
+
+ const pathname = docsMatch[1] || ''
+ const normalizedPath = `/docs${pathname ? `/${pathname}` : ''}`
+ const url = `https://e2b.dev${normalizedPath}`
+
+ docsPages.push({
+ url,
+ priority: 0.8,
+ })
+ }
+
+ const filteredPages = docsPages.filter((entry) => !entry.url.includes('/docs/legacy'))
+
+ console.log(`Generated ${filteredPages.length} sitemap entries`)
+ const finalEntries = filteredPages.sort((a, b) => a.url.localeCompare(b.url))
+
+ const sitemapXml = `<?xml version="1.0" encoding="UTF-8"?>
+<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
+${finalEntries.map(entry => ` <url>
+ <loc>${entry.url}</loc>
+ <changefreq>weekly</changefreq>
+ <priority>${entry.priority}</priority>
+ </url>`).join('\n')}
+</urlset>`
+
+ const outputPath = path.join(process.cwd(), 'public', 'sitemap.xml')
+ fs.writeFileSync(outputPath, sitemapXml)
+
+ console.log(`✅ Sitemap generated successfully: ${outputPath}`)
+ console.log(`📊 Total entries: ${finalEntries.length}`)
+ } catch (error) {
+ console.error('Error generating sitemap:', error.message)
+ throw error
+ }
+}
+
+generateSitemap().catch(console.error)
diff --git a/apps/web/src/app/sitemap.ts b/apps/web/src/app/sitemap.ts
deleted file mode 100644
index bd6708c26..000000000
--- a/apps/web/src/app/sitemap.ts
+++ /dev/null
@@ -1,34 +0,0 @@
-import { MetadataRoute } from 'next'
-import path from 'path'
-import { getPageForSitemap } from '@/utils/sitemap'
-
-export const dynamic = 'force-static'
-
-// NOTE: Sitemap should not be split into multiple files.
-// This would break path validation in [app/layout.tsx]
-export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
- const docsDirectory = path.join(
- process.env.NODE_ENV === 'production'
- ? path.join('.', 'src', 'app', '(docs)', 'docs')
- : path.join(process.cwd(), 'src', 'app', '(docs)', 'docs')
- )
-
- const docsPages = getPageForSitemap(
- docsDirectory,
- 'https://e2b.dev/docs/',
- 0.8
- )
- // Filter out legacy docs pages
- .filter((entry) => !entry.url.includes('/docs/legacy'))
-
- // Deduplicate URLs, keeping the entry with highest priority
- const urlMap = new Map<string, MetadataRoute.Sitemap[0]>()
- for (const entry of docsPages) {
- const existing = urlMap.get(entry.url)
- if (!existing || (existing.priority || 0) < (entry.priority || 0)) {
- urlMap.set(entry.url, entry)
- }
- }
-
- return Array.from(urlMap.values()).sort((a, b) => a.url.localeCompare(b.url))
-}