From 72c3e32b516c04562c07988372f0c0bbdaa9dd10 Mon Sep 17 00:00:00 2001 From: Damian Ricobelli Date: Sun, 1 Dec 2024 00:46:48 -0300 Subject: [PATCH] docs: fix og files --- apps/docs/app/og/[...slug]/route.tsx | 4 +- apps/docs/utils/metadata.ts | 110 +-------------------------- 2 files changed, 2 insertions(+), 112 deletions(-) diff --git a/apps/docs/app/og/[...slug]/route.tsx b/apps/docs/app/og/[...slug]/route.tsx index fdf4791..cd49e1b 100644 --- a/apps/docs/app/og/[...slug]/route.tsx +++ b/apps/docs/app/og/[...slug]/route.tsx @@ -120,8 +120,6 @@ export const GET = metadataImage.createAPI((page): ImageResponseType => { ); }); -export function generateStaticParams(): { - slug: string[]; -}[] { +export function generateStaticParams() { return metadataImage.generateParams(); } diff --git a/apps/docs/utils/metadata.ts b/apps/docs/utils/metadata.ts index 0600ced..34ec340 100644 --- a/apps/docs/utils/metadata.ts +++ b/apps/docs/utils/metadata.ts @@ -1,115 +1,7 @@ import { source } from "@/app/source"; -import type { InferPageType, LoaderConfig, LoaderOutput } from "fumadocs-core/source"; -import { notFound } from "next/navigation"; -import type { NextRequest } from "next/server"; +import { createMetadataImage } from "fumadocs-core/server"; import type { Metadata } from "next/types"; -interface ImageMeta { - alt: string; - url: string; - width: number; - height: number; -} - -interface RouteOptions { - params: { - slug: string[]; - lang?: string; - }; -} - -export function createMetadataImage>(options: { - source: S; - - /** - * the route of your OG image generator. - * - * @example '/docs-og' - * @defaultValue '/docs-og' - */ - imageRoute?: string; - - /** - * The filename of generated OG Image - * - * @defaultValue 'image.png' - */ - filename?: string; -}): { - getImageMeta: (slugs: string[]) => ImageMeta; - - /** - * Add image meta tags to metadata - */ - withImage: (slugs: string[], metadata?: Metadata) => Metadata; - - /** - * Generate static params for OG Image Generator - */ - generateParams: () => { - slug: string[]; - lang?: string; - }[]; - - /** - * create route handler for OG Image Generator - */ - createAPI: ( - handler: (page: InferPageType, request: NextRequest, options: RouteOptions) => Response | Promise, - ) => (request: NextRequest, options: RouteOptions) => Response | Promise; -} { - const { filename = "image.png", imageRoute = "/docs-og" } = options; - - function getImageMeta(slugs: string[]): ImageMeta { - return { - alt: "Banner", - url: `/${[...imageRoute.split("/"), ...slugs, filename].filter((v) => v.length > 0).join("/")}`, - width: 1200, - height: 630, - }; - } - - return { - getImageMeta, - withImage(slugs, data) { - const imageData = getImageMeta(slugs); - - return { - ...data, - openGraph: { - images: imageData, - ...data?.openGraph, - }, - twitter: { - images: imageData, - card: "summary_large_image", - ...data?.twitter, - }, - }; - }, - generateParams() { - return options.source.generateParams().map((params) => ({ - ...params, - slug: [...params.slug, filename], - })); - }, - createAPI(handler) { - return (req, args) => { - const params = args.params; - const page = options.source.getPage( - params.slug.slice(0, -1), //remove filename - params.lang, - ); - if (!page) { - notFound(); - } - - return handler(page as InferPageType, req, args); - }; - }, - }; -} - export function createMetadata(override: Metadata): Metadata { return { ...override,