Skip to content

Commit

Permalink
refactor(docs): move last pages over to app router (supabase#29293)
Browse files Browse the repository at this point in the history
The end of the Great Migration is here!!!

Moves the last pages over, deleting pages like the FAQ that we don't use
and that contain duplicated information anyway.

Dev secret auth page URL had to change as App Router doesn't like the
leading underscores in the path.

Also fixes the not-found recommendations to use the proper Next.js
not-found page so it will return a 404 as it should. (I was under the
erroneous impression that I couldn't get the pathname in not-found.tsx,
that is not true, so this works better.)
  • Loading branch information
charislam authored Sep 13, 2024
1 parent e81beac commit 5d2bdc9
Show file tree
Hide file tree
Showing 23 changed files with 191 additions and 385 deletions.
80 changes: 80 additions & 0 deletions apps/docs/app/api/ai/docs/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { SupabaseClient } from '@supabase/supabase-js'
import { ApplicationError, UserError, clippy } from 'ai-commands/edge'
import { NextRequest, NextResponse } from 'next/server'
import OpenAI from 'openai'

export const runtime = 'edge'
/* To avoid OpenAI errors, restrict to the Vercel Edge Function regions that
overlap with the OpenAI API regions.
Reference for Vercel regions: https://vercel.com/docs/edge-network/regions#region-list
Reference for OpenAI regions: https://help.openai.com/en/articles/5347006-openai-api-supported-countries-and-territories
*/
export const preferredRegion = [
'arn1',
'bom1',
'cdg1',
'cle1',
'cpt1',
'dub1',
'fra1',
'gru1',
'hnd1',
'iad1',
'icn1',
'kix1',
'lhr1',
'pdx1',
'sfo1',
'sin1',
'syd1',
]

const openAiKey = process.env.OPENAI_API_KEY
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL as string
const supabaseServiceKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY as string

export async function POST(req: NextRequest) {
if (!openAiKey || !supabaseUrl || !supabaseServiceKey) {
return NextResponse.json(
{ error: 'Missing environment variables for AI features.' },
{ status: 500 }
)
}

const openai = new OpenAI({ apiKey: openAiKey })
const supabaseClient = new SupabaseClient(supabaseUrl, supabaseServiceKey)

try {
const { messages } = (await req.json()) as {
messages: { content: string; role: 'user' | 'assistant' }[]
}

if (!messages) {
throw new UserError('Missing messages in request data')
}

const response = await clippy(openai, supabaseClient, messages)

// Proxy the streamed SSE response from OpenAI
return new NextResponse(response.body, {
headers: {
'Content-Type': 'text/event-stream',
},
})
} catch (error: unknown) {
console.error(error)
if (error instanceof UserError) {
return NextResponse.json({ error: error.message, data: error.data }, { status: 400 })
} else if (error instanceof ApplicationError) {
console.error(`${error.message}: ${JSON.stringify(error.data)}`)
} else {
console.error(error)
}

return NextResponse.json(
{ error: 'There was an error processing your request' },
{ status: 500 }
)
}
}
5 changes: 2 additions & 3 deletions apps/docs/app/api/crawlers/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { fromMarkdown } from 'mdast-util-from-markdown'
import { mdxFromMarkdown } from 'mdast-util-mdx'
import { toHast } from 'mdast-util-to-hast'
import { mdxjs } from 'micromark-extension-mdxjs'
import { redirect } from 'next/navigation'
import { notFound } from 'next/navigation'
import { visit } from 'unist-util-visit'

import { REFERENCES } from '~/content/navigation.references'
Expand All @@ -15,7 +15,6 @@ import {
import { getRefMarkdown } from '~/features/docs/Reference.mdx'
import type { MethodTypes } from '~/features/docs/Reference.typeSpec'
import type { AbbrevApiReferenceSection } from '~/features/docs/Reference.utils'
import { notFoundLink } from '~/features/recommendations/NotFound.utils'
import { BASE_PATH } from '~/lib/constants'

export async function GET(request: Request) {
Expand Down Expand Up @@ -52,7 +51,7 @@ export async function GET(request: Request) {
} catch {}

if (!section) {
redirect(notFoundLink(`${lib}/${slug}`))
notFound()
}

const html = htmlShell(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import { useRouter } from 'next/compat/router'
import { Button_Shadcn_ } from 'ui'
import { auth } from '~/lib/userAuth'
'use client'

export function getServerSideProps() {
if (process.env.NEXT_PUBLIC_DEV_AUTH_PAGE === 'true') {
return {
props: {},
}
}
import { useRouter } from 'next/navigation'

return {
notFound: true,
}
}
import { Button_Shadcn_ } from 'ui'

import { auth } from '~/lib/userAuth'

export default function DevOnlySecretAuth() {
export function DevSecretAuthForm() {
const router = useRouter()

function signInWithGitHub() {
Expand All @@ -37,7 +29,7 @@ export default function DevOnlySecretAuth() {
}

return (
<div className="p-10 flex items-center justify-center">
<div className="p-10 flex items-center justify-center max-w-lg mx-auto">
<section className="space-y-4">
<h1>Sign in</h1>
<p>
Expand All @@ -46,10 +38,10 @@ export default function DevOnlySecretAuth() {
the same domain.
</p>
<form className="flex flex-col gap-2 max-w-sm">
<Button_Shadcn_ type="button" onClick={signInWithGitHub}>
<Button_Shadcn_ type="button" variant="default" onClick={signInWithGitHub}>
Sign in with GitHub
</Button_Shadcn_>
<Button_Shadcn_ type="button" onClick={signOut}>
<Button_Shadcn_ type="button" variant="outline" onClick={signOut}>
Sign out
</Button_Shadcn_>
</form>
Expand Down
11 changes: 11 additions & 0 deletions apps/docs/app/dev-secret-auth/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { notFound } from 'next/navigation'

import { DevSecretAuthForm } from './AuthForm.client'

export default async function DevOnlySecretAuth() {
if (process.env.NEXT_PUBLIC_DEV_AUTH_PAGE !== 'true') {
throw notFound()
}

return <DevSecretAuthForm />
}
6 changes: 3 additions & 3 deletions apps/docs/app/guides/ai/python/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { type SerializeOptions } from 'next-mdx-remote/dist/types'
import { redirect } from 'next/navigation'
import { notFound } from 'next/navigation'
import { relative } from 'path'
import rehypeSlug from 'rehype-slug'

import { genGuideMeta } from '~/features/docs/GuidesMdx.utils'
import { GuideTemplate, newEditLink } from '~/features/docs/GuidesMdx.template'
import { fetchRevalidatePerDay } from '~/features/helpers.fetch'
import { notFoundLink } from '~/features/recommendations/NotFound.utils'
import { UrlTransformFunction, linkTransform } from '~/lib/mdx/plugins/rehypeLinkTransform'
import remarkMkDocsAdmonition from '~/lib/mdx/plugins/remarkAdmonition'
import { removeTitle } from '~/lib/mdx/plugins/remarkRemoveTitle'
Expand Down Expand Up @@ -73,7 +73,7 @@ const getContent = async ({ slug }: Params) => {
const page = pageMap.find(({ slug: validSlug }) => validSlug && validSlug === slug)

if (!page) {
redirect(notFoundLink(`api/python/${slug}`))
notFound()
}

const { remoteFile, meta } = page
Expand Down
5 changes: 2 additions & 3 deletions apps/docs/app/guides/cli/github-action/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { type SerializeOptions } from 'next-mdx-remote/dist/types'
import { redirect } from 'next/navigation'
import { notFound } from 'next/navigation'
import { relative } from 'node:path'
import rehypeSlug from 'rehype-slug'

import { genGuideMeta } from '~/features/docs/GuidesMdx.utils'
import { GuideTemplate, newEditLink } from '~/features/docs/GuidesMdx.template'
import { fetchRevalidatePerDay } from '~/features/helpers.fetch'
import { notFoundLink } from '~/features/recommendations/NotFound.utils'
import { UrlTransformFunction, linkTransform } from '~/lib/mdx/plugins/rehypeLinkTransform'
import remarkMkDocsAdmonition from '~/lib/mdx/plugins/remarkAdmonition'
import { removeTitle } from '~/lib/mdx/plugins/remarkRemoveTitle'
Expand Down Expand Up @@ -73,7 +72,7 @@ const getContent = async ({ slug }: Params) => {
const page = pageMap.find(({ slug: validSlug }) => validSlug && validSlug === slug)

if (!page) {
redirect(notFoundLink(`cli/github-action/${slug}`))
notFound()
}

const { remoteFile, meta } = page
Expand Down
5 changes: 2 additions & 3 deletions apps/docs/app/guides/graphql/[[...slug]]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { type SerializeOptions } from 'next-mdx-remote/dist/types'
import { redirect } from 'next/navigation'
import { notFound } from 'next/navigation'
import { isAbsolute, relative } from 'path'
import rehypeSlug from 'rehype-slug'

import { genGuideMeta } from '~/features/docs/GuidesMdx.utils'
import { GuideTemplate, newEditLink } from '~/features/docs/GuidesMdx.template'
import { fetchRevalidatePerDay } from '~/features/helpers.fetch'
import { notFoundLink } from '~/features/recommendations/NotFound.utils'
import { UrlTransformFunction, linkTransform } from '~/lib/mdx/plugins/rehypeLinkTransform'
import remarkMkDocsAdmonition from '~/lib/mdx/plugins/remarkAdmonition'
import { removeTitle } from '~/lib/mdx/plugins/remarkRemoveTitle'
Expand Down Expand Up @@ -127,7 +126,7 @@ const getContent = async ({ slug }: Params) => {
const page = pageMap.find((page) => page.slug === slug?.at(0))

if (!page) {
redirect(notFoundLink(`graphql/${slug ? slug.join('/') : ''}`))
notFound()
}

const { remoteFile, meta } = page
Expand Down
6 changes: 3 additions & 3 deletions apps/docs/app/guides/platform/terraform/[[...slug]]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import matter from 'gray-matter'
import { type SerializeOptions } from 'next-mdx-remote/dist/types'
import { redirect } from 'next/navigation'
import { notFound } from 'next/navigation'
import rehypeSlug from 'rehype-slug'

import { genGuideMeta } from '~/features/docs/GuidesMdx.utils'
import { GuideTemplate, newEditLink } from '~/features/docs/GuidesMdx.template'
import { fetchRevalidatePerDay } from '~/features/helpers.fetch'
import { notFoundLink } from '~/features/recommendations/NotFound.utils'
import { isValidGuideFrontmatter } from '~/lib/docs'
import { UrlTransformFunction, linkTransform } from '~/lib/mdx/plugins/rehypeLinkTransform'
import remarkMkDocsAdmonition from '~/lib/mdx/plugins/remarkAdmonition'
Expand Down Expand Up @@ -103,7 +103,7 @@ const getContent = async ({ slug }: Params) => {
const page = pageMap.find((page) => page.slug === requestedSlug)

if (!page) {
redirect(notFoundLink('platform/terraform' + (slug?.join('/') ?? '')))
notFound()
}

const { meta, remoteFile, useRoot } = page
Expand Down
50 changes: 50 additions & 0 deletions apps/docs/app/not-found.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { type Metadata } from 'next'
import Link from 'next/link'

import { Button } from 'ui'

import { Recommendations, SearchButton } from '~/features/recommendations/NotFound.client'
import { LayoutMainContent } from '~/layouts/DefaultLayout'
import { SidebarSkeleton } from '~/layouts/MainSkeleton'

export default function NotFound() {
return (
<SidebarSkeleton>
<LayoutMainContent>
<article className="prose max-w-[80ch]">
<h1>404: We couldn&apos;t find that page</h1>
<p>
Sorry, we couldn&apos;t find that page. It might be missing, or we had a temporary error
generating it.
</p>
<div className="flex flex-wrap gap-4 pt-4">
<SearchButton />
<Button type="default" size="small" className="p-4" asChild>
<Link href="/" className="no-underline">
Return to homepage
</Link>
</Button>
<Button type="text" size="small" asChild>
<Link
href="https://github.com/supabase/supabase/issues/new?assignees=&labels=documentation&projects=&template=2.Improve_docs.md"
target="_blank"
rel="noreferrer noopener"
className="no-underline"
>
Report missing page
</Link>
</Button>
</div>
<Recommendations />
</article>
</LayoutMainContent>
</SidebarSkeleton>
)
}

export const metadata: Metadata = {
title: 'Not found',
robots: {
index: false,
},
}
20 changes: 0 additions & 20 deletions apps/docs/app/not-found/layout.tsx

This file was deleted.

47 changes: 0 additions & 47 deletions apps/docs/app/not-found/page.tsx

This file was deleted.

Loading

0 comments on commit 5d2bdc9

Please sign in to comment.