Skip to content

Commit

Permalink
Convert home to render the home CmsPage and add page/[…url] route for…
Browse files Browse the repository at this point in the history
… additional pages.
  • Loading branch information
paales committed Jan 16, 2025
1 parent 32bccbb commit d85b05f
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 109 deletions.
29 changes: 4 additions & 25 deletions examples/magento-open-source/pages/[...url].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import {
findParentBreadcrumbItem,
getCategoryStaticPaths,
} from '@graphcommerce/magento-category'
import type { CmsPageQuery } from '@graphcommerce/magento-cms'
import { CmsPageContent, CmsPageDocument } from '@graphcommerce/magento-cms'
import type {
FilterTypes,
ProductFiltersQuery,
Expand All @@ -28,12 +26,7 @@ import {
productListLink,
useProductList,
} from '@graphcommerce/magento-product'
import {
PageMeta,
redirectOrNotFound,
redirectTo,
StoreConfigDocument,
} from '@graphcommerce/magento-store'
import { redirectOrNotFound, redirectTo, StoreConfigDocument } from '@graphcommerce/magento-store'
import type { GetStaticProps } from '@graphcommerce/next-ui'
import { Container, LayoutHeader, LayoutTitle } from '@graphcommerce/next-ui'
import { i18n } from '@lingui/core'
Expand All @@ -50,8 +43,7 @@ import type { CategoryPageQuery } from '../graphql/CategoryPage.gql'
import { CategoryPageDocument } from '../graphql/CategoryPage.gql'
import { graphqlSharedClient, graphqlSsrClient } from '../lib/graphql/graphqlSsrClient'

export type CategoryProps = CmsPageQuery &
CategoryPageQuery &
export type CategoryProps = CategoryPageQuery &
ProductListQuery &
ProductFiltersQuery & { filterTypes?: FilterTypes; params?: ProductListParams }
export type CategoryRoute = { url: string[] }
Expand All @@ -60,7 +52,7 @@ type GetPageStaticPaths = GetStaticPaths<CategoryRoute>
type GetPageStaticProps = GetStaticProps<LayoutNavigationProps, CategoryProps, CategoryRoute>

function CategoryPage(props: CategoryProps) {
const { categories, cmsPage, ...rest } = props
const { categories, ...rest } = props
const productList = useProductList({
...rest,
category: categories?.items?.[0],
Expand All @@ -77,15 +69,6 @@ function CategoryPage(props: CategoryProps) {
{category?.name}
</LayoutTitle>
</LayoutHeader>
{cmsPage && (
<>
<PageMeta
title={cmsPage.meta_title || cmsPage.title || cmsPage.content_heading || 'Page'}
metaDescription={cmsPage.meta_description || undefined}
/>
<CmsPageContent cmsPage={cmsPage} />
</>
)}
{isCategory && isLanding && (
<>
<CategoryMeta params={params} {...category} />
Expand Down Expand Up @@ -176,7 +159,6 @@ export const getStaticProps: GetPageStaticProps = async (context) => {
const staticClient = graphqlSsrClient(context)

const categoryPage = staticClient.query({ query: CategoryPageDocument, variables: { url } })
const cmsPage = staticClient.query({ query: CmsPageDocument, variables: { url } })

const layout = staticClient.query({
query: LayoutDocument,
Expand Down Expand Up @@ -216,9 +198,7 @@ export const getStaticProps: GetPageStaticProps = async (context) => {
})
: undefined

const hasPage = (await cmsPage).data.cmsPage

if (!hasCategory && !hasPage) return redirectOrNotFound(staticClient, conf, params, locale)
if (!hasCategory) return redirectOrNotFound(staticClient, conf, params, locale)

if ((await products)?.errors) {
const totalPages = (await filters)?.data.filters?.page_info?.total_pages ?? 0
Expand All @@ -244,7 +224,6 @@ export const getStaticProps: GetPageStaticProps = async (context) => {
...(await products)?.data,
...(await filters)?.data,
...(await layout).data,
...(await cmsPage).data,
filterTypes: await filterTypes,
params: productListParams,
apolloState: await conf.then(() => client.cache.extract()),
Expand Down
98 changes: 24 additions & 74 deletions examples/magento-open-source/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,88 +1,33 @@
import type { PageOptions } from '@graphcommerce/framer-next-pages'
import { cacheFirst } from '@graphcommerce/graphql'
import {
ProductListDocument,
ProductListQuery,
ProductPageName,
} from '@graphcommerce/magento-product'
import type { CmsPageFragment } from '@graphcommerce/magento-cms'
import { CmsPageContent, CmsPageDocument } from '@graphcommerce/magento-cms'
import { StoreConfigDocument } from '@graphcommerce/magento-store'
import type { GetStaticProps } from '@graphcommerce/next-ui'
import {
breakpointVal,
HeroBanner,
LayoutHeader,
LayoutTitle,
MetaRobots,
PageMeta,
} from '@graphcommerce/next-ui'
import { Button, Container, Typography } from '@mui/material'
import { Container, isTypename, LayoutHeader, PageMeta } from '@graphcommerce/next-ui'
import { i18n } from '@lingui/core'
import type { LayoutNavigationProps } from '../components'
import { LayoutDocument, LayoutNavigation } from '../components'
import { graphqlSharedClient, graphqlSsrClient } from '../lib/graphql/graphqlSsrClient'

type Props = {}
type RouteProps = { url: string }
type GetPageStaticProps = GetStaticProps<LayoutNavigationProps, Props, RouteProps>
export type CmsPageProps = { cmsPage: CmsPageFragment | null }

function CmsPage(props: Props) {
const {} = props
type GetPageStaticProps = GetStaticProps<LayoutNavigationProps, CmsPageProps>

return (
<>
{/* <PageMeta
title={page?.metaTitle ?? page?.title ?? ''}
metaDescription={page?.metaDescription ?? ''}
metaRobots={page?.metaRobots.toLowerCase().split('_') as MetaRobots[] | undefined}
canonical='/'
/> */}
function CmsPage(props: CmsPageProps) {
const { cmsPage } = props

<LayoutHeader floatingMd hideMd={import.meta.graphCommerce.breadcrumbs}>
<LayoutTitle size='small' component='span'>
Home
</LayoutTitle>
</LayoutHeader>
if (!cmsPage) return <Container>Configure cmsPage home</Container>

<LayoutHeader floatingMd floatingSm />
return (
<>
<PageMeta
title={cmsPage.meta_title || cmsPage.title || i18n._(/* i18n */ 'Home')}
metaDescription={cmsPage.meta_description || undefined}
/>
<LayoutHeader floatingMd hideMd={import.meta.graphCommerce.breadcrumbs} floatingSm />

<HeroBanner
pageLinks={
<Button href='/men/art' variant='outlined' size='large' color='inherit'>
Shop Art
</Button>
}
videoSrc='https://media.graphassets.com/UNmtIZmWSgmnpUAWcAk0'
sx={(theme) => ({
'& .HeroBanner-copy': {
minHeight: { xs: 'min(70vh,600px)', md: 'min(70vh,1080px)' },
[theme.breakpoints.up('sm')]: {
padding: theme.spacings.xl,
justifyItems: 'start',
alignContent: 'center',
textAlign: 'left',
width: '50%',
},
},
})}
>
<Typography variant='overline' gutterBottom>
A journey through creativity
</Typography>
<Typography
variant='h1'
sx={(theme) => ({
textTransform: 'uppercase',
mt: 1,
mb: theme.spacings.sm,
...breakpointVal('fontSize', 36, 82, theme.breakpoints.values),
'& strong': {
WebkitTextFillColor: 'transparent',
WebkitTextStroke: '1.2px #fff',
},
})}
>
<strong>Discover</strong> beauty beyond boundaries.
</Typography>
</HeroBanner>
<CmsPageContent cmsPage={cmsPage} />
</>
)
}
Expand All @@ -95,19 +40,24 @@ export default CmsPage

export const getStaticProps: GetPageStaticProps = async (context) => {
const client = graphqlSharedClient(context)
const conf = client.query({ query: StoreConfigDocument })
const staticClient = graphqlSsrClient(context)

const conf = client.query({ query: StoreConfigDocument })
const url = (await conf).data.storeConfig?.cms_home_page ?? 'home'
const cmsPageQuery = staticClient.query({ query: CmsPageDocument, variables: { url } })
const layout = staticClient.query({
query: LayoutDocument,
fetchPolicy: cacheFirst(staticClient),
})
const cmsPage = (await cmsPageQuery).data.route

return {
const result = {
props: {
cmsPage: cmsPage && isTypename(cmsPage, ['CmsPage']) ? cmsPage : null,
...(await layout).data,
apolloState: await conf.then(() => client.cache.extract()),
},
revalidate: 60 * 20,
}
return result
}
98 changes: 98 additions & 0 deletions examples/magento-open-source/pages/page/[...url].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import type { PageOptions } from '@graphcommerce/framer-next-pages'
import { cacheFirst } from '@graphcommerce/graphql'
import { getCategoryStaticPaths } from '@graphcommerce/magento-category'
import type { CmsPageFragment, CmsPageQuery } from '@graphcommerce/magento-cms'
import { CmsPageContent, CmsPageDocument } from '@graphcommerce/magento-cms'
import { PageMeta, redirectOrNotFound, StoreConfigDocument } from '@graphcommerce/magento-store'
import {
Container,
isTypename,
LayoutHeader,
LayoutTitle,
type GetStaticProps,
} from '@graphcommerce/next-ui'
import type { GetStaticPaths } from 'next'
import type { LayoutNavigationProps } from '../../components'
import { LayoutDocument, LayoutNavigation } from '../../components'
import { graphqlSharedClient, graphqlSsrClient } from '../../lib/graphql/graphqlSsrClient'

export type CmsPageProps = { cmsPage: CmsPageFragment }
export type CmsRoute = { url: string[] }

type GetPageStaticPaths = GetStaticPaths<CmsRoute>
type GetPageStaticProps = GetStaticProps<LayoutNavigationProps, CmsPageProps, CmsRoute>

function CmsPage(props: CmsPageProps) {
const { cmsPage } = props

return (
<>
<PageMeta
title={cmsPage.meta_title || cmsPage.title || cmsPage.content_heading || 'Page'}
metaDescription={cmsPage.meta_description || undefined}
/>

<LayoutHeader floatingMd hideMd={import.meta.graphCommerce.breadcrumbs}>
<LayoutTitle size='small' component='span'>
{cmsPage.content_heading ?? cmsPage.title}
</LayoutTitle>
</LayoutHeader>

{cmsPage.content_heading && (
<Container maxWidth={false}>
<LayoutTitle variant='h1' gutterTop gutterBottom>
{cmsPage.content_heading}
</LayoutTitle>
</Container>
)}

<CmsPageContent cmsPage={cmsPage} />
</>
)
}

const pageOptions: PageOptions<LayoutNavigationProps> = {
Layout: LayoutNavigation,
}
CmsPage.pageOptions = pageOptions

export default CmsPage

export const getStaticPaths: GetPageStaticPaths = async ({ locales = [] }) => {
// Disable getStaticPaths while in development mode
if (process.env.NODE_ENV === 'development') return { paths: [], fallback: 'blocking' }

const path = (locale: string) => getCategoryStaticPaths(graphqlSsrClient({ locale }), locale)
const paths = (await Promise.all(locales.map(path))).flat(1)
return { paths, fallback: 'blocking' }
}

export const getStaticProps: GetPageStaticProps = async (context) => {
const { params, locale } = context
const url = params?.url?.join('/') ?? ''
if (!url) return { notFound: true }

const client = graphqlSharedClient(context)
const conf = client.query({ query: StoreConfigDocument })
const staticClient = graphqlSsrClient(context)

const cmsPageQuery = staticClient.query({ query: CmsPageDocument, variables: { url } })
const layout = staticClient.query({
query: LayoutDocument,
fetchPolicy: cacheFirst(staticClient),
})

const cmsPage = (await cmsPageQuery).data.route
if (!cmsPage || !isTypename(cmsPage, ['CmsPage']))
return redirectOrNotFound(staticClient, conf, params, locale)

const result = {
props: {
cmsPage,
...(await layout).data,
apolloState: await conf.then(() => client.cache.extract()),
},
revalidate: 60 * 20,
}
return result
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Container, Typography } from '@mui/material'
import { Container } from '@mui/material'
import type { CmsPageContentFragment } from './CmsPageContent.gql'

export type CmsPageContentProps = { cmsPage: CmsPageContentFragment }
Expand All @@ -7,11 +7,6 @@ export function CmsPageContent(props: CmsPageContentProps) {
const { cmsPage } = props
return (
<Container>
{cmsPage.content_heading && (
<Typography variant='h2' component='h1'>
{cmsPage.content_heading}
</Typography>
)}
{/* eslint-disable-next-line react/no-danger */}
{cmsPage.content && <div dangerouslySetInnerHTML={{ __html: cmsPage.content }} />}
</Container>
Expand Down
12 changes: 8 additions & 4 deletions packages/magento-cms/graphql/CmsPage.graphql
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
query CmsPage($url: String!) {
cmsPage(identifier: $url) {
identifier
...CmsPageMeta
...CmsPageContent
route(url: $url) {
__typename
relative_url
redirect_code
type
... on CmsPage {
...CmsPageFragment
}
}
}
5 changes: 5 additions & 0 deletions packages/magento-cms/graphql/CmsPageFragment.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fragment CmsPageFragment on CmsPage {
identifier
...CmsPageMeta
...CmsPageContent
}
1 change: 1 addition & 0 deletions packages/magento-cms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './components/CmsPageContent/CmsPageContent.gql'
export * from './components/CmsPageMeta/CmsPageMeta'
export * from './components/CmsPageMeta/CmsPageMeta.gql'
export * from './graphql/CmsPage.gql'
export * from './graphql/CmsPageFragment.gql'

0 comments on commit d85b05f

Please sign in to comment.