Skip to content

Commit

Permalink
Server content.
Browse files Browse the repository at this point in the history
  • Loading branch information
sherakama committed Oct 10, 2023
1 parent f293c9a commit 6d74af5
Showing 1 changed file with 49 additions and 62 deletions.
111 changes: 49 additions & 62 deletions app/(storyblok)/[...slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,86 +1,73 @@
// import { Metadata, ResolvingMetadata } from 'next';
// import {
// getStoryblokApi, ISbStoriesParams, StoryblokClient, StoryblokStory,
// } from '@storyblok/react/rsc';
// import { resolveRelations } from '@/utilities/resolveRelations';
// import { getPageMetadata } from '@/utilities/getPageMetadata';
// import { notFound } from 'next/navigation';
type PathsType = {
slug: string[];
};
import { Metadata } from 'next';
import { getStoryblokApi, ISbStoriesParams, StoryblokClient } from '@storyblok/react/rsc';
import StoryblokStory from '@storyblok/react/story';
import { resolveRelations } from '@/utilities/resolveRelations';
import { getPageMetadata } from '@/utilities/getPageMetadata';

type Props = {
params: { id: string, slug: string[] }
searchParams: { [key: string]: string | string[] | undefined }
}

// Make sure to not export the below functions otherwise there will be a typescript error
// https://github.com/vercel/next.js/discussions/48724
// const getStoryData = async (params: { slug: string[] }) => {
// const activeEnv = process.env.NODE_ENV || 'development';
// let slug: string = params.slug ? params.slug.join('/') : '';
// let sbParams: ISbStoriesParams = {
// version: activeEnv === 'development' ? 'draft' : 'published',
// cv: activeEnv === 'development' ? Date.now() : undefined,
// resolve_relations: resolveRelations,
// };
// const storyblokApi: StoryblokClient = getStoryblokApi();
const activeEnv = process.env.NODE_ENV || 'development';

// try {
// const story = await storyblokApi.get(`cdn/stories/${slug}`, sbParams);
// return story;
// } catch (error) {
// notFound();
// }
// type PathsType = {
// slug: string[];
// };

const Page = async ({ params }: Props) => {
// const { data } = await getStoryData(params);
// const bridgeOptions = { resolveRelations };
let slug: string = params.slug ? params.slug.join('/') : '';

return (
<>
<h1>{params.slug}</h1>
<p>Aaaaaah yeah. Server content.</p>
</>
);
type ParamsType = {
slug: string[];
};

/**
* Generate metadata for the inside pages.
*/
// export async function generateMetadata({ params }: Props, parent: ResolvingMetadata): Promise<Metadata> {
// const { data } = await getStoryData(params);
// const blok = data.story.content;
// let slug: string = params.slug ? params.slug.join('/') : '';
// const meta = getPageMetadata({ blok, slug });
// return meta;
// }

/**
* Generate static paths for the inside pages.
*/
// export async function generateStaticParams(): Promise<any[]> {
// const activeEnv = process.env.NODE_ENV || 'development';
// async function generateStaticParams() {
// const storyblokApi: StoryblokClient = getStoryblokApi();
// let sbParams: ISbStoriesParams = {
// version: activeEnv === 'development' ? 'draft' : 'published',
// cv: activeEnv === 'development' ? Date.now() : undefined,
// };

// const { data: { links } } = await storyblokApi.get('cdn/links', sbParams);
// let paths: PathsType[] = [];

// Object.keys(links).forEach((linkKey) => {
// if (links[linkKey].is_folder || links[linkKey].slug === 'home') {
// return;
// }
// const slug:string = links[linkKey].slug;
// const slug: string = links[linkKey].slug;
// let splittedSlug = slug.split('/');
// paths.push({ slug: splittedSlug });
// });

// // Return a list of paths and no fallback.
// return paths;
// };

export default Page;
// Make sure to not export the below functions otherwise there will be a typescript error
// https://github.com/vercel/next.js/discussions/48724
async function getStoryData(params: { slug: string[] }) {
let slug: string = params.slug ? params.slug.join('/') : '';
let sbParams: ISbStoriesParams = {
version: activeEnv === 'development' ? 'draft' : 'published',
cv: activeEnv === 'development' ? Date.now() : undefined,
resolve_relations: resolveRelations,
};

const storyblokApi: StoryblokClient = getStoryblokApi();
const story = await storyblokApi?.get(`cdn/stories/${slug}`, sbParams);

return story;
};

export async function generateMetadata({ params }: { params: ParamsType }): Promise<Metadata> {
const { data } = await getStoryData(params);
const blok = data.story.content;

let slug: string = params.slug ? params.slug.join('/') : '';
const meta = getPageMetadata({ blok, slug });

return meta;
}

export default async function Page({ params }: { params: ParamsType }) {
const { data } = await getStoryData(params);
const bridgeOptions = { resolveRelations };
let slug: string = params.slug ? params.slug.join('/') : '';

return (
<StoryblokStory story={data.story} bridgeOptions={bridgeOptions} slug={slug} />
);
};

0 comments on commit 6d74af5

Please sign in to comment.