-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
- Loading branch information
There are no files selected for viewing
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,71 @@ | ||
import { GetServerSideProps } from 'next' | ||
import { GetStaticPaths, GetStaticProps } from 'next' | ||
import { createServerApp } from 'server/isr/server-app' | ||
import { KlevuCategoryPage } from '../../components/klevu/klevu' | ||
import { CategoryPage, CategoryPageProps } from 'components/category-page' | ||
import { getServerState } from 'react-instantsearch-hooks-server' | ||
import { renderToString } from 'react-dom/server' | ||
import { IntlProvider } from 'react-intl' | ||
import { | ||
ALGOLIA_API_SEARCH_KEY, | ||
ALGOLIA_APP_ID, | ||
ALGOLIA_INDEX_NAME, | ||
} from '../../utils/constants' | ||
|
||
export const getServerSideProps: GetServerSideProps<any> = async (context) => { | ||
const ALGOLIA_KEYS = [ | ||
ALGOLIA_APP_ID, | ||
ALGOLIA_API_SEARCH_KEY, | ||
ALGOLIA_INDEX_NAME, | ||
] | ||
|
||
export const getStaticPaths: GetStaticPaths = async (context) => { | ||
const { ssg } = await createServerApp({ context }) | ||
const categories = await ssg.commerce.getCategories.fetch() | ||
const paths = | ||
categories?.map((category) => ({ params: { slug: category.slug } })) || [] | ||
return { | ||
paths, | ||
fallback: 'blocking', | ||
} | ||
} | ||
|
||
export const getStaticProps: GetStaticProps<CategoryPageProps> = async ( | ||
context | ||
) => { | ||
const { ssg } = await createServerApp({ context }) | ||
const slug = `${context?.params?.slug?.toString()}` | ||
await ssg.commerce.getCategoryBy.prefetch({ slug }) | ||
|
||
const algoliaKeysMissing = ALGOLIA_KEYS.some((key) => !key) | ||
|
||
if (algoliaKeysMissing) { | ||
return { | ||
props: { | ||
trpcState: ssg.dehydrate(), | ||
algoliaKeysSet: !algoliaKeysMissing, | ||
query: slug, | ||
}, | ||
revalidate: 60, | ||
} | ||
} | ||
|
||
const intlConfig = await ssg.config.intl.fetch() | ||
const messages = intlConfig?.find((el) => el.locale === context.locale) | ||
|
||
const serverState = await getServerState( | ||
<IntlProvider locale={context.locale ?? ''} messages={messages?.keys ?? {}}> | ||
<CategoryPage query={slug} algoliaKeysSet={!algoliaKeysMissing} /> | ||
</IntlProvider>, | ||
{ renderToString } | ||
) | ||
|
||
return { | ||
props: { | ||
trpcState: ssg.dehydrate(), | ||
category: slug, | ||
algoliaKeysSet: !algoliaKeysMissing, | ||
query: slug, | ||
serverState, | ||
}, | ||
revalidate: 60, | ||
} | ||
} | ||
|
||
export default KlevuCategoryPage | ||
export default CategoryPage |
This file was deleted.