|
|
import { useEffect } from "react"; |
|
|
import { GetStaticPaths, GetStaticProps } from "next"; |
|
|
import NotFound from "src/NotFound"; |
|
|
import Layout from "src/Layout"; |
|
|
import { |
|
|
RenderingType, |
|
|
SitecoreContext, |
|
|
ComponentPropsContext, |
|
|
handleEditorFastRefresh, |
|
|
EditingComponentPlaceholder, |
|
|
StaticPath, |
|
|
} from "@sitecore-jss/sitecore-jss-nextjs"; |
|
|
import { SitecorePageProps } from "lib/page-props"; |
|
|
import { sitecorePagePropsFactory } from "lib/page-props-factory"; |
|
|
|
|
|
import { |
|
|
componentFactory, |
|
|
editingComponentFactory, |
|
|
} from "temp/componentFactory"; |
|
|
import { sitemapFetcher } from "lib/sitemap-fetcher"; |
|
|
|
|
|
const SitecorePage = ({ |
|
|
notFound, |
|
|
componentProps, |
|
|
layoutData, |
|
|
}: SitecorePageProps): JSX.Element => { |
|
|
useEffect(() => { |
|
|
|
|
|
handleEditorFastRefresh(); |
|
|
}, []); |
|
|
|
|
|
if (notFound || !layoutData.sitecore.route) { |
|
|
|
|
|
return <NotFound />; |
|
|
} |
|
|
|
|
|
const isEditing = layoutData.sitecore.context.pageEditing; |
|
|
const isComponentRendering = |
|
|
layoutData.sitecore.context.renderingType === RenderingType.Component; |
|
|
|
|
|
return ( |
|
|
<ComponentPropsContext value={componentProps}> |
|
|
<SitecoreContext |
|
|
componentFactory={ |
|
|
isEditing ? editingComponentFactory : componentFactory |
|
|
} |
|
|
layoutData={layoutData} |
|
|
> |
|
|
{/* |
|
|
Sitecore Pages supports component rendering to avoid refreshing the entire page during component editing. |
|
|
If you are using Experience Editor only, this logic can be removed, Layout can be left. |
|
|
*/} |
|
|
{isComponentRendering ? ( |
|
|
<EditingComponentPlaceholder rendering={layoutData.sitecore.route} /> |
|
|
) : ( |
|
|
<Layout layoutData={layoutData} /> |
|
|
)} |
|
|
</SitecoreContext> |
|
|
</ComponentPropsContext> |
|
|
); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
export const getStaticPaths: GetStaticPaths = async (context) => { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let paths: StaticPath[] = []; |
|
|
let fallback: boolean | "blocking" = "blocking"; |
|
|
|
|
|
if ( |
|
|
process.env.NODE_ENV !== "development" && |
|
|
!process.env.DISABLE_SSG_FETCH |
|
|
) { |
|
|
try { |
|
|
|
|
|
paths = await sitemapFetcher.fetch(context); |
|
|
} catch (error) { |
|
|
console.log("Error occurred while fetching static paths"); |
|
|
console.log(error); |
|
|
} |
|
|
|
|
|
fallback = process.env.EXPORT_MODE ? false : fallback; |
|
|
} |
|
|
|
|
|
return { |
|
|
paths, |
|
|
fallback, |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const getStaticProps: GetStaticProps = async (context) => { |
|
|
const props = await sitecorePagePropsFactory.create(context); |
|
|
|
|
|
return { |
|
|
props, |
|
|
|
|
|
|
|
|
|
|
|
revalidate: 5, |
|
|
notFound: props.notFound, |
|
|
}; |
|
|
}; |
|
|
|
|
|
export default SitecorePage; |
|
|
|