diff --git a/generator/konfig-next-app/src/middleware.ts b/generator/konfig-next-app/src/middleware.ts index 83a7fe226a..36c9d5d2d1 100644 --- a/generator/konfig-next-app/src/middleware.ts +++ b/generator/konfig-next-app/src/middleware.ts @@ -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) diff --git a/generator/konfig-next-app/src/pages/[org]/[portal]/index.tsx b/generator/konfig-next-app/src/pages/[org]/[portal]/index.tsx deleted file mode 100644 index 4531bb5559..0000000000 --- a/generator/konfig-next-app/src/pages/[org]/[portal]/index.tsx +++ /dev/null @@ -1,38 +0,0 @@ -import { GetStaticPaths, GetStaticProps } from 'next' -import Error from 'next/error' - -import { generateDemosDataFromGithub } from '@/utils/generate-demos-from-github' - -export const getStaticPaths: GetStaticPaths = async () => { - return { - paths: [], - fallback: 'blocking', - } -} - -export const getStaticProps: GetStaticProps = async (ctx) => { - const orgId = ctx.params?.org - if (orgId === undefined || Array.isArray(orgId)) { - return { - notFound: true, - } - } - const portalId = ctx.params?.portal - if (portalId === undefined || Array.isArray(portalId)) { - return { - notFound: true, - } - } - return { - redirect: { - destination: `/${orgId}/${portalId}/reference`, - permanent: false, - }, - } -} - -function Org() { - return -} - -export default Org diff --git a/generator/konfig-next-app/src/pages/sitemap.xml.ts b/generator/konfig-next-app/src/pages/sitemap.xml.ts new file mode 100644 index 0000000000..50832c4028 --- /dev/null +++ b/generator/konfig-next-app/src/pages/sitemap.xml.ts @@ -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 ` + + ${pages + .map( + (page) => ` + + ${`https://${host}${page.slug}`} + daily + 0.7 + + ` + ) + .join('')} + ` +} + +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 diff --git a/generator/konfig-next-app/src/utils/generate-props-for-sitemap-xml.ts b/generator/konfig-next-app/src/utils/generate-props-for-sitemap-xml.ts new file mode 100644 index 0000000000..3fd3738724 --- /dev/null +++ b/generator/konfig-next-app/src/utils/generate-props-for-sitemap-xml.ts @@ -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>['allMarkdown'] +} + +export async function generatePropsForSitemapXml({ + owner, + repo, +}: { + owner: string + repo: string +}): Promise { + 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, + } +}