diff --git a/pages/[[...slug]].js b/pages/[[...slug]].js index edae7e0..c00126e 100644 --- a/pages/[[...slug]].js +++ b/pages/[[...slug]].js @@ -5,17 +5,10 @@ import { useStoryblok, StoryblokComponent } from "@storyblok/react" -import { useRouter } from "next/router" -export default function Home({ story }) { +export default function SlugPage({ slug, version, story }) { const staticStory = useStoryblokState(story) - const router = useRouter() - - let slug = (router.pathname === "/" - ? "home" - : router.pathname.replace("/", "")) - - const dynamicStory = useStoryblok(slug, { version: process.env.VERSION }) + const dynamicStory = useStoryblok(slug, { version }) const story2 = dynamicStory?.name ? dynamicStory : staticStory @@ -25,7 +18,7 @@ export default function Home({ story }) { {story2 ? story2.name : "My Site"} - + ); } @@ -33,15 +26,19 @@ export default function Home({ story }) { export async function getStaticProps({ params }) { let slug = params.slug && params.slug.length ? params.slug.join("/") : "home"; + console.log('slug', slug) + + const version = process.env.VERSION + const storyblokApi = getStoryblokApi(); - let { data } = await storyblokApi.get(`cdn/stories/${slug}`, { - version: process.env.VERSION - }); - + let { data } = await storyblokApi.get(`cdn/stories/${slug}`, { version }); + return { props: { story: data ? data.story : false, key: data ? data.story.id : false, + slug, + version }, revalidate: 3600, }; @@ -49,10 +46,10 @@ export async function getStaticProps({ params }) { export async function getStaticPaths() { const storyblokApi = getStoryblokApi(); - let { data } = await storyblokApi.get("cdn/links/" ,{ + let { data } = await storyblokApi.get("cdn/links/", { version: process.env.VERSION }); - + let paths = []; Object.keys(data.links).forEach((linkKey) => { if (data.links[linkKey].is_folder) { @@ -61,15 +58,15 @@ export async function getStaticPaths() { if (data.links[linkKey].slug === "home") { paths.push({ params: { slug: [] } }); - return + return } - + const slug = data.links[linkKey].slug; let splittedSlug = slug.split("/"); - + paths.push({ params: { slug: splittedSlug } }); }); - + return { paths: paths, fallback: false,