From 02975eb15bb0887ffcf2b663941f54c9da3285b7 Mon Sep 17 00:00:00 2001 From: Sgal Cheung Date: Thu, 5 Sep 2024 20:19:54 +0800 Subject: [PATCH] Update [slug].astro MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Because SSR pages can’t use getStaticPaths(), they can’t receive props. https://docs.astro.build/en/guides/routing/#modifying-the-slug-example-for-ssr --- .../src/pages/posts/[slug].astro | 27 ++++++------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/apps/getting-started-astro/src/pages/posts/[slug].astro b/apps/getting-started-astro/src/pages/posts/[slug].astro index e186c3fe1..a65c0f423 100644 --- a/apps/getting-started-astro/src/pages/posts/[slug].astro +++ b/apps/getting-started-astro/src/pages/posts/[slug].astro @@ -1,26 +1,15 @@ --- -import MainLayout from '../../layouts/main.astro' +import MainLayout from "../../layouts/main.astro"; -import { XataClient } from '../../xata'; +import { XataClient } from "../../xata"; -export async function getStaticPaths() { - const xata = new XataClient({ - apiKey: import.meta.env.XATA_API_KEY, - branch: import.meta.env.XATA_BRANCH - }); - const posts = await xata.db.Posts.getAll(); +const { slug } = Astro.params; - const routes = posts.map(({ slug, title, description, pubDate }) => { - return { - params: { slug }, - props: { title, description, pubDate }, - }; - }); - - return routes; -} - -const post = Astro.props; +const xata = new XataClient({ + apiKey: import.meta.env.XATA_API_KEY, + branch: import.meta.env.XATA_BRANCH, +}); +const post = await xata.db.Posts.filter({ slug: slug }).getFirstOrThrow(); ---