Skip to content

Commit

Permalink
fix: wrong client side path and envs
Browse files Browse the repository at this point in the history
  • Loading branch information
amalcaraz committed Mar 4, 2024
1 parent 731244c commit 47ed750
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions pages/[[...slug]].js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -25,34 +18,38 @@ export default function Home({ story }) {
<title>{story2 ? story2.name : "My Site"}</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<StoryblokComponent blok={story2.content} />
<StoryblokComponent blok={story2.content} />
</div>
);
}

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,
};
}

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) {
Expand All @@ -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,
Expand Down

0 comments on commit 47ed750

Please sign in to comment.