Advertisement
merhan_1

[slug].tsx

Jan 28th, 2025
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TypeScript 6.01 KB | Source Code | 0 0
  1. import type { BuilderContent as BuilderContentType } from '@builder.io/sdk';
  2. import {
  3.   BuilderContent as BuilderContentComponentWrapper,
  4.   Builder,
  5. } from '@builder.io/react/lite';
  6.  
  7. import { BuilderIoModelIDs } from '@/enums/common';
  8. import { fetchBottomNavigation } from '@/api/fetchBottomNavigation';
  9. import {
  10.   GlobalVariables,
  11.   NavigationDataModel,
  12.   ResourceDataModel,
  13. } from '@/global-types';
  14. import { INSIGHTS_PATH, SITE_MAP_PATH } from '@/constants/paths';
  15. import {
  16.   getResourceImage,
  17.   getResourceTitle,
  18.   getResourceDate,
  19.   getResourceAuthor,
  20.   getResourceDescription,
  21. } from '@/utils/resources';
  22. import { MetaData } from '@/components/meta';
  23. import { fetchGlobalVariables } from '@/api/fetchGlobalVariables';
  24. import { lazy, useEffect, useState } from 'react';
  25. import NotFoundPage from '@/pages/404';
  26. import { useRouter } from 'next/router';
  27. import { Loader } from '@/components/common/Revalidate/Loader/Loader';
  28. import { useUrlRedirectsProceed } from '@/hooks/useUrlRedirectsProceed/useUrlRedirectsProceed';
  29. import withRedirect from '@/components/hoc/withRedirect';
  30. import {
  31.   BuilderContent,
  32.   isPreviewing,
  33.   fetchEntries,
  34.   fetchOneEntry,
  35. } from '@builder.io/sdk-react';
  36. import { customComponents } from '../../../builder-registry';
  37.  
  38. const LazyBuilderComponent = lazy(() =>
  39.   import('@builder.io/sdk-react').then((module) => ({
  40.     default: module.Content,
  41.   }))
  42. );
  43.  
  44. Builder.isStatic = true;
  45.  
  46. export async function getStaticProps({
  47.   params,
  48. }: {
  49.   params: { resource: string; slug: string };
  50. }) {
  51.   const contentData =
  52.     ((await fetchOneEntry({
  53.       apiKey: process.env.NEXT_PUBLIC_BUILDER_API_KEY!,
  54.       model: BuilderIoModelIDs.Resources,
  55.       options: { enrich: true, cachebust: true },
  56.       query: {
  57.         'data.slug': params.slug,
  58.         'data.type': params.resource,
  59.       },
  60.     })) as unknown as ResourceDataModel) || undefined;
  61.  
  62.   const globalVars = (await fetchGlobalVariables()) as GlobalVariables;
  63.  
  64.   const commonSEOTitle = globalVars?.data?.seoSiteTitle;
  65.  
  66.   const title = getResourceTitle(contentData);
  67.   const description = getResourceDescription(contentData);
  68.   const date = getResourceDate(contentData);
  69.   const authors = await getResourceAuthor(contentData);
  70.   const { src, alt } = getResourceImage(contentData);
  71.  
  72.   const bottomNavigation = await fetchBottomNavigation();
  73.  
  74.   return {
  75.     props: {
  76.       model: BuilderIoModelIDs.Resources,
  77.       contentData: contentData,
  78.       bottomNavigation,
  79.       slug: params.slug,
  80.       resourceType: params.resource,
  81.       title: title,
  82.       description: description,
  83.       date: date,
  84.       authors: authors,
  85.       imageSrc: src,
  86.       imageAlt: alt,
  87.       commonSEOTitle,
  88.     },
  89.     revalidate: 300,
  90.   };
  91. }
  92.  
  93. export async function getStaticPaths() {
  94.   const resources = (await fetchEntries({
  95.     apiKey: process.env.NEXT_PUBLIC_BUILDER_API_KEY!,
  96.     model: BuilderIoModelIDs.Resources,
  97.     fields: 'data.slug,data.type',
  98.     options: { noTargeting: true },
  99.     limit: 100,
  100.   })) as { data: { slug: string; type: string } }[];
  101.  
  102.   return {
  103.     paths: resources.map(
  104.       (d) => `${INSIGHTS_PATH}/${d.data.type}/${d.data.slug}`
  105.     ),
  106.     fallback: true,
  107.   };
  108. }
  109.  
  110. function Page({
  111.   model,
  112.   contentData,
  113.   bottomNavigation,
  114.   slug,
  115.   resourceType,
  116.   title,
  117.   description,
  118.   date,
  119.   authors,
  120.   imageSrc,
  121.   imageAlt,
  122.   commonSEOTitle,
  123.   waitForRedirect,
  124. }: {
  125.   model: string;
  126.   contentData: BuilderContent | undefined;
  127.   bottomNavigation: NavigationDataModel | null;
  128.   slug: string;
  129.   title: string;
  130.   description: string;
  131.   date: string;
  132.   authors: string[];
  133.   resourceType: string;
  134.   imageSrc: string;
  135.   imageAlt: string;
  136.   commonSEOTitle: string;
  137.   waitForRedirect: boolean;
  138. }) {
  139.   const [rootUrl, setRootUrl] = useState<string>('');
  140.   const router = useRouter();
  141.   const canShowContent = contentData || isPreviewing(router.asPath);
  142.   useUrlRedirectsProceed(router, contentData);
  143.  
  144.   useEffect(() => {
  145.     if (window !== undefined) {
  146.       const _rootUrl = window.location.origin;
  147.       setRootUrl(_rootUrl);
  148.     }
  149.   }, []);
  150.  
  151.   if (router.isFallback || waitForRedirect) {
  152.     return (
  153.       <>
  154.         <MetaData
  155.           title={title}
  156.           commonSEOTitle={commonSEOTitle}
  157.           description={description}
  158.           siteMapUrl={`${rootUrl}${SITE_MAP_PATH}`}
  159.           postAuthors={authors}
  160.           url={`${rootUrl}${INSIGHTS_PATH}/${resourceType}/${slug}`}
  161.           imageSrc={imageSrc}
  162.           imageAlt={imageAlt}
  163.         />
  164.         <Loader />
  165.       </>
  166.     );
  167.   }
  168.  
  169.   if (!contentData && !canShowContent) {
  170.     return <NotFoundPage />;
  171.   }
  172.  
  173.   return (
  174.     <>
  175.       <MetaData
  176.         title={title}
  177.         commonSEOTitle={commonSEOTitle}
  178.         description={description}
  179.         siteMapUrl={`${rootUrl}${SITE_MAP_PATH}`}
  180.         postAuthors={authors}
  181.         url={`${rootUrl}${INSIGHTS_PATH}/${resourceType}/${slug}`}
  182.         imageSrc={imageSrc}
  183.         imageAlt={imageAlt}
  184.       />
  185.       <BuilderContentComponentWrapper
  186.         key={slug}
  187.         model={model}
  188.         content={contentData as BuilderContentType}
  189.         options={{ enrich: true }}
  190.       >
  191.         {(data) => {
  192.           return (
  193.             <LazyBuilderComponent
  194.               apiKey={process.env.NEXT_PUBLIC_BUILDER_API_KEY!}
  195.               customComponents={customComponents}
  196.               key={slug}
  197.               model={model}
  198.               content={contentData}
  199.               enrich
  200.               data={{
  201.                 bottomNavigation,
  202.                 resource: {
  203.                   type: data?.type,
  204.                   tags: data?.tags,
  205.                   date: date,
  206.                 },
  207.               }}
  208.               context={{
  209.                 bottomNavigation,
  210.                 resource: {
  211.                   type: data?.type,
  212.                   tags: data?.tags,
  213.                   date: data,
  214.                 },
  215.               }}
  216.             />
  217.           );
  218.         }}
  219.       </BuilderContentComponentWrapper>
  220.     </>
  221.   );
  222. }
  223.  
  224. export default withRedirect(Page);
  225.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement