-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add SEO support with metadata, sitemap, and JSON-LD #22
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
Changes from all commits
28da4b4
d5a8f31
0a9e134
ea98c8d
326dc49
2834b44
29108ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import type { Metadata, ResolvingMetadata } from 'next' | ||
| import { notFound } from 'next/navigation' | ||
| import type { MDXContent } from 'mdx/types' | ||
| import { loadConfig } from '@/lib/config' | ||
|
|
@@ -16,6 +17,41 @@ interface PageData { | |
| toc: { title: string; url: string; depth: number }[] | ||
| } | ||
|
|
||
| export async function generateMetadata( | ||
| { params }: PageProps, | ||
| parent: ResolvingMetadata, | ||
| ): Promise<Metadata> { | ||
| const { slug } = await params | ||
| const page = source.getPage(slug) | ||
| if (!page) return {} | ||
| const config = loadConfig() | ||
| const data = page.data as PageData | ||
| const parentMetadata = await parent | ||
|
|
||
| const metadata: Metadata = { | ||
| title: data.title, | ||
| description: data.description, | ||
| } | ||
|
|
||
| if (config.url) { | ||
| const ogParams = new URLSearchParams({ title: data.title }) | ||
| if (data.description) ogParams.set('description', data.description) | ||
| metadata.openGraph = { | ||
| ...parentMetadata.openGraph, | ||
| title: data.title, | ||
| description: data.description, | ||
| images: [{ url: `/og?${ogParams.toString()}`, width: 1200, height: 630 }], | ||
| } | ||
|
Comment on lines
+36
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Verify current code does not set per-page openGraph.url and uses string concat for pageUrl.
rg -n --type=ts -C2 'openGraph\s*=' packages/chronicle/src/app/[[...slug]]/page.tsx
rg -n --type=ts -C2 'url:\s*pageUrl|const pageUrl = .*join' packages/chronicle/src/app/[[...slug]]/page.tsxRepository: raystack/chronicle Length of output: 615 🏁 Script executed: cat -n packages/chronicle/src/app/[[...slug]]/page.tsxRepository: raystack/chronicle Length of output: 3462 Use one normalized per-page absolute URL for both OpenGraph and JSON-LD. On line 39-44, 🔧 Proposed fix export async function generateMetadata(
{ params }: PageProps,
parent: ResolvingMetadata,
): Promise<Metadata> {
const { slug } = await params
const page = source.getPage(slug)
if (!page) return {}
const config = loadConfig()
const data = page.data as PageData
const parentMetadata = await parent
+ const pagePath = (slug ?? []).map(encodeURIComponent).join('/')
+ const pageUrl =
+ config.url
+ ? new URL(pagePath, config.url.endsWith('/') ? config.url : `${config.url}/`).toString()
+ : undefined
const metadata: Metadata = {
title: data.title,
description: data.description,
}
@@
if (config.url) {
const ogParams = new URLSearchParams({ title: data.title })
if (data.description) ogParams.set('description', data.description)
metadata.openGraph = {
...parentMetadata.openGraph,
title: data.title,
description: data.description,
+ ...(pageUrl && { url: pageUrl }),
images: [{ url: `/og?${ogParams.toString()}`, width: 1200, height: 630 }],
}
@@
export default async function DocsPage({ params }: PageProps) {
const { slug } = await params
const config = loadConfig()
@@
- const pageUrl = config.url ? `${config.url}/${(slug ?? []).join('/')}` : undefined
+ const pagePath = (slug ?? []).map(encodeURIComponent).join('/')
+ const pageUrl =
+ config.url
+ ? new URL(pagePath, config.url.endsWith('/') ? config.url : `${config.url}/`).toString()
+ : undefined🤖 Prompt for AI Agents |
||
| metadata.twitter = { | ||
| ...parentMetadata.twitter, | ||
| title: data.title, | ||
| description: data.description, | ||
| } | ||
| } | ||
|
|
||
| return metadata | ||
| } | ||
|
|
||
| export default async function DocsPage({ params }: PageProps) { | ||
| const { slug } = await params | ||
| const config = loadConfig() | ||
|
|
@@ -33,20 +69,33 @@ export default async function DocsPage({ params }: PageProps) { | |
|
|
||
| const tree = buildPageTree() | ||
|
|
||
| const pageUrl = config.url ? `${config.url}/${(slug ?? []).join('/')}` : undefined | ||
|
|
||
| return ( | ||
| <Page | ||
| page={{ | ||
| slug: slug ?? [], | ||
| frontmatter: { | ||
| title: data.title, | ||
| <> | ||
| <script type="application/ld+json"> | ||
| {JSON.stringify({ | ||
| '@context': 'https://schema.org', | ||
| '@type': 'Article', | ||
| headline: data.title, | ||
| description: data.description, | ||
| }, | ||
| content: <MDXBody components={mdxComponents} />, | ||
| toc: data.toc ?? [], | ||
| }} | ||
| config={config} | ||
| tree={tree} | ||
| /> | ||
| ...(pageUrl && { url: pageUrl }), | ||
| }, null, 2)} | ||
| </script> | ||
| <Page | ||
| page={{ | ||
| slug: slug ?? [], | ||
| frontmatter: { | ||
| title: data.title, | ||
| description: data.description, | ||
| }, | ||
| content: <MDXBody components={mdxComponents} />, | ||
| toc: data.toc ?? [], | ||
| }} | ||
| config={config} | ||
| tree={tree} | ||
| /> | ||
| </> | ||
| ) | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { ImageResponse } from 'next/og' | ||
| import type { NextRequest } from 'next/server' | ||
| import { loadConfig } from '@/lib/config' | ||
|
|
||
| export async function GET(request: NextRequest) { | ||
| const { searchParams } = request.nextUrl | ||
| const title = searchParams.get('title') ?? loadConfig().title | ||
| const description = searchParams.get('description') ?? '' | ||
| const siteName = loadConfig().title | ||
|
|
||
| return new ImageResponse( | ||
| ( | ||
| <div | ||
| style={{ | ||
| height: '100%', | ||
| width: '100%', | ||
| display: 'flex', | ||
| flexDirection: 'column', | ||
| justifyContent: 'center', | ||
| padding: '60px 80px', | ||
| backgroundColor: '#0a0a0a', | ||
| color: '#fafafa', | ||
| }} | ||
| > | ||
| <div | ||
| style={{ | ||
| fontSize: 24, | ||
| color: '#888', | ||
| marginBottom: 16, | ||
| }} | ||
| > | ||
| {siteName} | ||
| </div> | ||
| <div | ||
| style={{ | ||
| fontSize: 56, | ||
| fontWeight: 700, | ||
| lineHeight: 1.2, | ||
| marginBottom: 24, | ||
| }} | ||
| > | ||
| {title} | ||
| </div> | ||
| {description && ( | ||
| <div | ||
| style={{ | ||
| fontSize: 24, | ||
| color: '#999', | ||
| lineHeight: 1.4, | ||
| }} | ||
| > | ||
| {description} | ||
| </div> | ||
| )} | ||
| </div> | ||
| ), | ||
| { | ||
| width: 1200, | ||
| height: 630, | ||
| } | ||
| ) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import type { MetadataRoute } from 'next' | ||
| import { loadConfig } from '@/lib/config' | ||
|
|
||
| export default function robots(): MetadataRoute.Robots { | ||
| const config = loadConfig() | ||
| return { | ||
| rules: { userAgent: '*', allow: '/' }, | ||
| ...(config.url && { sitemap: `${config.url}/sitemap.xml` }), | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import type { MetadataRoute } from 'next' | ||
| import { loadConfig } from '@/lib/config' | ||
| import { source } from '@/lib/source' | ||
| import { loadApiSpecs } from '@/lib/openapi' | ||
| import { buildApiRoutes } from '@/lib/api-routes' | ||
|
|
||
| export default function sitemap(): MetadataRoute.Sitemap { | ||
| const config = loadConfig() | ||
| if (!config.url) return [] | ||
|
|
||
| const baseUrl = config.url.replace(/\/$/, '') | ||
|
|
||
| const docPages = source.getPages().map((page) => ({ | ||
| url: `${baseUrl}/${page.slugs.join('/')}`, | ||
| ...(page.data.lastModified && { lastModified: new Date(page.data.lastModified) }), | ||
| })) | ||
|
|
||
| const apiPages = config.api?.length | ||
| ? buildApiRoutes(loadApiSpecs(config.api)).map((route) => ({ | ||
| url: `${baseUrl}/apis/${route.slug.join('/')}`, | ||
| })) | ||
| : [] | ||
|
|
||
| return [ | ||
| { url: baseUrl }, | ||
| ...docPages, | ||
| ...apiPages, | ||
| ] | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.