Skip to content

Commit

Permalink
fix: storyblok build paths (#1140)
Browse files Browse the repository at this point in the history
  • Loading branch information
annarhughes authored Sep 16, 2024
1 parent 6df6559 commit 4371b46
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 14 deletions.
4 changes: 2 additions & 2 deletions pages/[slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export async function getStaticProps({ locale, preview = false, params }: GetSta

export async function getStaticPaths({ locales }: GetStaticPathsContext) {
const storyblokApi = getStoryblokApi();
let { data } = await storyblokApi.get('cdn/links/', { published: true });
let { data } = await storyblokApi.get('cdn/links/', { version: 'published' });

const excludePaths: string[] = [
'home',
Expand All @@ -50,7 +50,7 @@ export async function getStaticPaths({ locales }: GetStaticPathsContext) {

let paths: any = [];
Object.keys(data.links).forEach((linkKey) => {
if (data.links[linkKey].is_folder) {
if (data.links[linkKey].is_folder || !data.links[linkKey].published) {
return;
}

Expand Down
11 changes: 10 additions & 1 deletion pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ThemeProvider } from '@mui/material/styles';
// Import the functions you need from the SDKs you need
import { Analytics } from '@vercel/analytics/react';
import { NextComponentType } from 'next';
import { NextIntlClientProvider } from 'next-intl';
import { IntlError, NextIntlClientProvider } from 'next-intl';
import type { AppProps } from 'next/app';
import Head from 'next/head';
import { useRouter } from 'next/router';
Expand Down Expand Up @@ -57,12 +57,21 @@ function MyApp(props: MyAppProps) {
// Get top level directory of path e.g pathname /courses/course_name has pathHead courses
const pathHead = router.pathname.split('/')[1]; // E.g. courses | therapy | partner-admin

function onIntlError(error: IntlError) {
if (error.code === 'MISSING_MESSAGE') {
console.error(`${error.message} Page: ${router.asPath}`);
} else {
console.error(error);
}
}

return (
<ErrorBoundary>
<NextIntlClientProvider
messages={pageProps.messages}
locale={router.locale}
timeZone="Europe/London"
onError={onIntlError}
>
<Head>
<meta name="viewport" content="initial-scale=1, width=device-width" />
Expand Down
4 changes: 2 additions & 2 deletions pages/courses/[slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export async function getStaticProps({ locale, preview = false, params }: GetSta

export async function getStaticPaths({ locales }: GetStaticPathsContext) {
let sbParams: ISbStoriesParams = {
published: true,
version: 'published',
starts_with: 'courses/',
filter_query: {
component: {
Expand All @@ -66,7 +66,7 @@ export async function getStaticPaths({ locales }: GetStaticPathsContext) {
let paths: any = [];

courses.forEach((course: Partial<ISbStoryData>) => {
if (!course.slug) return;
if (!course.slug || !course.published) return;

if (!course.is_startpage || isAlternativelyHandledCourse(course.slug)) {
return;
Expand Down
9 changes: 7 additions & 2 deletions pages/courses/[slug]/[sessionSlug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export async function getStaticProps({ locale, preview = false, params }: GetSta

export async function getStaticPaths({ locales }: GetStaticPathsContext) {
let sbParams: ISbStoriesParams = {
published: true,
version: 'published',
starts_with: 'courses/',
};

Expand All @@ -88,7 +88,12 @@ export async function getStaticPaths({ locales }: GetStaticPathsContext) {
const slug = session.slug;
if (!slug) return;

if (session.is_startpage || session.is_folder || isAlternativelyHandledSession(slug)) {
if (
session.is_startpage ||
session.is_folder ||
isAlternativelyHandledSession(slug) ||
!session.published
) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export async function getStaticProps({ locale, preview = false, params }: GetSta

export async function getStaticPaths({ locales }: GetStaticPathsContext) {
let sbParams: ISbStoriesParams = {
published: true,
version: 'published',
starts_with: 'courses/image-based-abuse-and-rebuilding-ourselves/',
};

Expand All @@ -88,7 +88,7 @@ export async function getStaticPaths({ locales }: GetStaticPathsContext) {
const slug = session.slug;
if (!slug) return;

if (session.is_startpage || session.is_folder) {
if (session.is_startpage || session.is_folder || !session.published) {
return;
}

Expand Down
5 changes: 3 additions & 2 deletions pages/welcome/[partnerName].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export async function getStaticProps({ locale, preview = false, params }: GetSta
props: {
...storyblokProps,
messages: {
...require(`../../messages/courses/${locale}.json`),
...require(`../../messages/shared/${locale}.json`),
...require(`../../messages/navigation/${locale}.json`),
...require(`../../messages/welcome/${locale}.json`),
Expand All @@ -50,7 +51,7 @@ export async function getStaticProps({ locale, preview = false, params }: GetSta

export async function getStaticPaths({ locales }: GetStaticPathsContext) {
let sbParams: ISbStoriesParams = {
published: true,
version: 'published',
starts_with: 'welcome/',
};

Expand All @@ -60,7 +61,7 @@ export async function getStaticPaths({ locales }: GetStaticPathsContext) {
let paths: any = [];

data.forEach((story: Partial<ISbStoryData>) => {
if (!story.slug) return;
if (!story.slug || !story.published) return;

// get array for slug because of catch all
let splittedSlug = story.slug.split('/');
Expand Down
4 changes: 1 addition & 3 deletions utils/getStoryblokPageProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export const getStoryblokPageProps = async (
if (!slug) {
return {
story: null,
key: false,
preview,
locale: locale || null,
error: 'No slug provided',
Expand All @@ -27,11 +26,10 @@ export const getStoryblokPageProps = async (
let { data } = await storyblokApi.get(`cdn/stories/${slug}`, sbParams);
return {
story: data ? data.story : null,
key: data ? data.story.id : false,
preview,
locale: locale || null,
};
} catch (error) {
console.log('Error getting storyblok data for page', error);
console.log('Error getting storyblok data for page', slug, sbParams, error);
}
};

0 comments on commit 4371b46

Please sign in to comment.