From c7ab58b44fae5e83fa937a396f28a7187b833b94 Mon Sep 17 00:00:00 2001 From: Dhenain Ambroise Date: Fri, 28 May 2021 11:10:11 +0200 Subject: [PATCH] Improve public layout by using a properly mocked Customer entity shared between SSG/SSR pages + improve doc --- src/layouts/public/mockedStaticDataset.ts | 25 +++++++++++++++++++++++ src/layouts/public/publicLayoutSSG.ts | 9 ++------ src/layouts/public/publicLayoutSSR.ts | 9 ++------ src/pages/[locale]/public/index.tsx | 17 +++++++-------- 4 files changed, 36 insertions(+), 24 deletions(-) create mode 100644 src/layouts/public/mockedStaticDataset.ts diff --git a/src/layouts/public/mockedStaticDataset.ts b/src/layouts/public/mockedStaticDataset.ts new file mode 100644 index 00000000..2ff77add --- /dev/null +++ b/src/layouts/public/mockedStaticDataset.ts @@ -0,0 +1,25 @@ +import { Customer } from '@/modules/core/data/types/Customer'; +import { GraphCMSDataset } from '@/modules/core/data/types/GraphCMSDataset'; + +const customerRef: string = process.env.NEXT_PUBLIC_CUSTOMER_REF; + +/** + * This mocked static dataset is used by the "public" layout to start the app with the minimalist amount of data. + * + * XXX The "Customer" entity represents a "Tenant" in a multi-tenancy system. It's basically the owner of a site. + * Each Customer has its own website, with its own data. + * + * Fields required by NRN to function properly by default (they can be hardcoded if you only have one Customer, or fetched from a DB if you have many) + * - 'ref': Identifier of the customer. Use by analytics, monitoring, etc. + * - 'availableLanguages': List of languages the website is available in, static pages will be generated for all listed languages. + * - '__typename': Must be "Customer". You can rename it if you wish to, but you'll need to adapt the code in various places. + * + * @see https://unlyed.github.io/next-right-now/concepts/tenancy.html#tenancy-st-mt-ht-and-mst + */ +export const mockedStaticDataset: GraphCMSDataset = { + customer: { + ref: customerRef, + availableLanguages: ['en'], // Necessary to generate the static pages and serve SSR pages, for those languages + __typename: 'Customer', // Necessary to find the customer object within the mocked dataset + } as Customer, // TS casting is necessary because we don't provide all properties +}; diff --git a/src/layouts/public/publicLayoutSSG.ts b/src/layouts/public/publicLayoutSSG.ts index 5d787be2..426a39ba 100644 --- a/src/layouts/public/publicLayoutSSG.ts +++ b/src/layouts/public/publicLayoutSSG.ts @@ -3,6 +3,7 @@ import { StaticPath } from '@/app/types/StaticPath'; import { StaticPathsOutput } from '@/app/types/StaticPathsOutput'; import { StaticPropsInput } from '@/app/types/StaticPropsInput'; import { SSGPageProps } from '@/layouts/core/types/SSGPageProps'; +import { mockedStaticDataset } from '@/layouts/public/mockedStaticDataset'; import { APOLLO_STATE_PROP_NAME } from '@/modules/core/apollo/apolloClient'; import { Customer } from '@/modules/core/data/types/Customer'; import { getLocizeTranslations } from '@/modules/core/i18n/getLocizeTranslations'; @@ -75,13 +76,7 @@ export const getPublicLayoutStaticProps: GetStaticProps = (props): JSX.Element => { pageName={AMPLITUDE_PAGES.TEMPLATE_SSG_PAGE} >

- This page was created using from "/layouts/public/pagePublicTemplateSSG.tsx".
- it is located at "/pages/[locale]/public/index.tsx", but you most likely want to move it to "/pages/[locale]/index.tsx".
+ This page was created using from "/layouts/public/pagePublicTemplateSSG.tsx", which uses a completely different layout.

- This page uses the "public" layout, which comes "naked" (no nav/footer) and doesn't fetch any data. (data are mocked, and minimalist) + This page uses the "public" + layout, which comes "naked" (no nav/footer) and doesn't fetch any data. (data are mocked, and minimalist)
- You can start customizing NRN here. Other pages use the "demo" layout, which comes with its own data fetching, components, and inherent complexity.
+ Other pages use the "demo" layout, which comes with its own data fetching, components, and inherent complexity.


- Usually, when I create a new project from NRN, I wipe the whole "/layouts/demo" folder, unless I want to keep it around for its documentation/examples. + If you want to use NRN as a boilerplate for your own project, we recommend using the public layout, because it's much simpler and doesn't rely on any particular data.
+ Usually, when I create a new project from NRN, I wipe the whole "/layouts/demo" folder, unless I want to keep it around for its documentation/examples.

- Customer data:
- + The customer data are visible in the browser logs.

);