Skip to content

[ENG-1968] sitemap.xml for docs #733

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions generator/konfig-next-app/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ export async function middleware(request: NextRequest) {

const url = request.nextUrl
const path = url.pathname
console.log('url', request.url)
console.log('nextUrl', request.nextUrl)
console.log('path', path)
const rewrittenUrl = new URL(`/${host}${path}`, request.url)
console.log('rewriting to', rewrittenUrl.href)
return NextResponse.rewrite(rewrittenUrl)
Expand Down
38 changes: 0 additions & 38 deletions generator/konfig-next-app/src/pages/[org]/[portal]/index.tsx

This file was deleted.

70 changes: 70 additions & 0 deletions generator/konfig-next-app/src/pages/sitemap.xml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// pages/sitemap.xml.ts

import { domainToRepoMappings } from '@/utils/domain-to-repo-mappings'
import { generateOwnerAndRepoFromDomain } from '@/utils/generate-owner-and-repo-from-domain'
import { generatePropsForSitemapXml } from '@/utils/generate-props-for-sitemap-xml'
import { GetServerSideProps } from 'next'
import { NextResponse } from 'next/server'

interface PageData {
slug: string
}

function generateSitemapXml(host: string, pages: PageData[]): string {
return `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${pages
.map(
(page) => `
<url>
<loc>${`https://${host}${page.slug}`}</loc>
<changefreq>daily</changefreq>
<priority>0.7</priority>
</url>
`
)
.join('')}
</urlset>`
}

const SiteMap: React.FC = () => {
// getServerSideProps will do the heavy lifting
return null
}

export const getServerSideProps: GetServerSideProps = async ({ res, req }) => {
if (!res) {
return {
props: {},
}
}
const host = req?.headers?.host
if (host === undefined)
return {
notFound: true,
}
if (domainToRepoMappings[host] === undefined) {
return {
notFound: true,
}
}

const { owner, repo } = generateOwnerAndRepoFromDomain(host)
const { slugs } = await generatePropsForSitemapXml({ owner, repo })

// Fetch your dynamic data
const pages: PageData[] = slugs.map((slug) => ({ slug: slug.id }))

// Generate the XML sitemap with the pages data
const sitemap = generateSitemapXml(host, pages)

res.setHeader('Content-Type', 'text/xml')
res.write(sitemap)
res.end()

return {
props: {},
}
}

export default SiteMap
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import path from 'path'
import { githubGetRepository } from './github-get-repository'
import { createOctokitInstance } from './octokit'
import { computeDocumentProps } from './compute-document-props'
import { githubGetKonfigYamlsSafe } from './github-get-konfig-yamls-safe'

export type SitemapXmlProps = {
slugs: Awaited<ReturnType<typeof computeDocumentProps>>['allMarkdown']
}

export async function generatePropsForSitemapXml({
owner,
repo,
}: {
owner: string
repo: string
}): Promise<SitemapXmlProps> {
const octokit = await createOctokitInstance({ owner, repo })

// get default branch of repo
const { data: repoData } = await githubGetRepository({
owner,
repo,
octokit,
})
const defaultBranch = repoData.default_branch

console.debug('defaultBranch', defaultBranch)

// time the next two lines
const start = Date.now()
const konfigYamls = await githubGetKonfigYamlsSafe({
owner,
repo,
octokit,
defaultBranch,
})
console.log(`githubGetKonfigYamls took ${Date.now() - start}ms`)

// TODO: handle multiple konfig.yaml
const konfigYaml = konfigYamls[0]

const documentationConfig = konfigYaml?.content.portal?.documentation

if (konfigYaml?.content.portal === undefined)
throw Error("Couldn't find portal configuration")

const { allMarkdown } = await computeDocumentProps({
documentationConfig,
owner,
repo,
octokit,
konfigYamlDir: path.dirname(konfigYaml.info.path),
})

return {
slugs: allMarkdown,
}
}
Loading