2
0

🔒 Expose minimum data to NEXT_DATA json

This commit is contained in:
Baptiste Arnaud
2023-08-14 16:55:47 +02:00
parent 401efa9d0c
commit de616ea649
2 changed files with 106 additions and 64 deletions

View File

@@ -3,23 +3,28 @@ import { BackgroundType, Typebot } from '@typebot.io/schemas'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import { SEO } from './Seo' import { SEO } from './Seo'
export type TypebotPageProps = { export type TypebotV3PageProps = {
url: string url: string
typebot: Pick<Typebot, 'settings' | 'theme' | 'name' | 'publicId'> name: string
publicId: string | null
isHideQueryParamsEnabled: boolean | null
background: Typebot['theme']['general']['background']
metadata: Typebot['settings']['metadata']
} }
export const TypebotPageV3 = ({ url, typebot }: TypebotPageProps) => { export const TypebotPageV3 = ({
publicId,
name,
url,
isHideQueryParamsEnabled,
metadata,
background,
}: TypebotV3PageProps) => {
const { asPath, push } = useRouter() const { asPath, push } = useRouter()
const background = typebot?.theme.general.background
const clearQueryParamsIfNecessary = () => { const clearQueryParamsIfNecessary = () => {
const hasQueryParams = asPath.includes('?') const hasQueryParams = asPath.includes('?')
if ( if (!hasQueryParams || !(isHideQueryParamsEnabled ?? true)) return
!hasQueryParams ||
!(typebot?.settings.general.isHideQueryParamsEnabled ?? true)
)
return
push(asPath.split('?')[0], undefined, { shallow: true }) push(asPath.split('?')[0], undefined, { shallow: true })
} }
@@ -36,15 +41,8 @@ export const TypebotPageV3 = ({ url, typebot }: TypebotPageProps) => {
: '#fff', : '#fff',
}} }}
> >
<SEO <SEO url={url} typebotName={name} metadata={metadata} />
url={url} <Standard typebot={publicId} onInit={clearQueryParamsIfNecessary} />
typebotName={typebot.name}
metadata={typebot.settings.metadata}
/>
<Standard
typebot={typebot.publicId}
onInit={clearQueryParamsIfNecessary}
/>
</div> </div>
) )
} }

View File

@@ -2,16 +2,10 @@ import { IncomingMessage } from 'http'
import { ErrorPage } from '@/components/ErrorPage' import { ErrorPage } from '@/components/ErrorPage'
import { NotFoundPage } from '@/components/NotFoundPage' import { NotFoundPage } from '@/components/NotFoundPage'
import { GetServerSideProps, GetServerSidePropsContext } from 'next' import { GetServerSideProps, GetServerSidePropsContext } from 'next'
import { import { env, getViewerUrl, isNotDefined } from '@typebot.io/lib'
env,
getViewerUrl,
isDefined,
isNotDefined,
omit,
} from '@typebot.io/lib'
import prisma from '../lib/prisma' import prisma from '../lib/prisma'
import { TypebotPageProps, TypebotPageV2 } from '@/components/TypebotPageV2' import { TypebotPageProps, TypebotPageV2 } from '@/components/TypebotPageV2'
import { TypebotPageV3 } from '@/components/TypebotPageV3' import { TypebotPageV3, TypebotV3PageProps } from '@/components/TypebotPageV3'
// Browsers that doesn't support ES modules and/or web components // Browsers that doesn't support ES modules and/or web components
const incompatibleBrowsers = [ const incompatibleBrowsers = [
@@ -67,14 +61,11 @@ export const getServerSideProps: GetServerSideProps = async (
const publishedTypebot = isMatchingViewerUrl const publishedTypebot = isMatchingViewerUrl
? await getTypebotFromPublicId(context.query.publicId?.toString()) ? await getTypebotFromPublicId(context.query.publicId?.toString())
: await getTypebotFromCustomDomain(customDomain) : await getTypebotFromCustomDomain(customDomain)
const headCode = publishedTypebot?.settings.metadata.customHeadCode
return { return {
props: { props: {
publishedTypebot, publishedTypebot,
incompatibleBrowser, incompatibleBrowser,
url: `https://${forwardedHost ?? host}${pathname}`, url: `https://${forwardedHost ?? host}${pathname}`,
customHeadCode:
isDefined(headCode) && headCode !== '' ? headCode : null,
}, },
} }
} catch (err) { } catch (err) {
@@ -88,12 +79,18 @@ export const getServerSideProps: GetServerSideProps = async (
} }
} }
const getTypebotFromPublicId = async ( const getTypebotFromPublicId = async (publicId?: string) => {
publicId?: string const publishedTypebot = (await prisma.publicTypebot.findFirst({
): Promise<TypebotPageProps['publishedTypebot'] | null> => {
const publishedTypebot = await prisma.publicTypebot.findFirst({
where: { typebot: { publicId: publicId ?? '' } }, where: { typebot: { publicId: publicId ?? '' } },
include: { select: {
variables: true,
settings: true,
theme: true,
version: true,
groups: true,
edges: true,
typebotId: true,
id: true,
typebot: { typebot: {
select: { select: {
name: true, name: true,
@@ -103,21 +100,39 @@ const getTypebotFromPublicId = async (
}, },
}, },
}, },
}) })) as TypebotPageProps['publishedTypebot'] | null
if (isNotDefined(publishedTypebot)) return null if (isNotDefined(publishedTypebot)) return null
return omit( return publishedTypebot.version
publishedTypebot, ? ({
'createdAt', name: publishedTypebot.typebot.name,
'updatedAt' publicId: publishedTypebot.typebot.publicId ?? null,
) as TypebotPageProps['publishedTypebot'] background: publishedTypebot.theme.general.background,
isHideQueryParamsEnabled:
publishedTypebot.settings.general.isHideQueryParamsEnabled ?? null,
metadata: publishedTypebot.settings.metadata,
} as Pick<
TypebotV3PageProps,
| 'name'
| 'publicId'
| 'background'
| 'isHideQueryParamsEnabled'
| 'metadata'
>)
: publishedTypebot
} }
const getTypebotFromCustomDomain = async ( const getTypebotFromCustomDomain = async (customDomain: string) => {
customDomain: string const publishedTypebot = (await prisma.publicTypebot.findFirst({
): Promise<TypebotPageProps['publishedTypebot'] | null> => {
const publishedTypebot = await prisma.publicTypebot.findFirst({
where: { typebot: { customDomain } }, where: { typebot: { customDomain } },
include: { select: {
variables: true,
settings: true,
theme: true,
version: true,
groups: true,
edges: true,
typebotId: true,
id: true,
typebot: { typebot: {
select: { select: {
name: true, name: true,
@@ -127,13 +142,25 @@ const getTypebotFromCustomDomain = async (
}, },
}, },
}, },
}) })) as TypebotPageProps['publishedTypebot'] | null
if (isNotDefined(publishedTypebot)) return null if (isNotDefined(publishedTypebot)) return null
return omit( return publishedTypebot.version
publishedTypebot, ? ({
'createdAt', name: publishedTypebot.typebot.name,
'updatedAt' publicId: publishedTypebot.typebot.publicId ?? null,
) as TypebotPageProps['publishedTypebot'] background: publishedTypebot.theme.general.background,
isHideQueryParamsEnabled:
publishedTypebot.settings.general.isHideQueryParamsEnabled ?? null,
metadata: publishedTypebot.settings.metadata,
} as Pick<
TypebotV3PageProps,
| 'name'
| 'publicId'
| 'background'
| 'isHideQueryParamsEnabled'
| 'metadata'
>)
: publishedTypebot
} }
const getHost = ( const getHost = (
@@ -147,7 +174,22 @@ const App = ({
publishedTypebot, publishedTypebot,
incompatibleBrowser, incompatibleBrowser,
...props ...props
}: TypebotPageProps & { incompatibleBrowser: string | null }) => { }: {
isIE: boolean
customHeadCode: string | null
url: string
publishedTypebot:
| TypebotPageProps['publishedTypebot']
| Pick<
TypebotV3PageProps,
| 'name'
| 'publicId'
| 'background'
| 'isHideQueryParamsEnabled'
| 'metadata'
>
incompatibleBrowser: string | null
}) => {
if (incompatibleBrowser) if (incompatibleBrowser)
return ( return (
<ErrorPage <ErrorPage
@@ -158,22 +200,24 @@ const App = ({
} }
/> />
) )
if (!publishedTypebot || publishedTypebot.typebot.isArchived) if (
!publishedTypebot ||
('typebot' in publishedTypebot && publishedTypebot.typebot.isArchived)
)
return <NotFoundPage /> return <NotFoundPage />
if (publishedTypebot.typebot.isClosed) if ('typebot' in publishedTypebot && publishedTypebot.typebot.isClosed)
return <ErrorPage error={new Error('This bot is now closed')} /> return <ErrorPage error={new Error('This bot is now closed')} />
return publishedTypebot.version ? ( return 'typebot' in publishedTypebot ? (
<TypebotPageV2 publishedTypebot={publishedTypebot} {...props} />
) : (
<TypebotPageV3 <TypebotPageV3
url={props.url} url={props.url}
typebot={{ name={publishedTypebot.name}
name: publishedTypebot.typebot.name, publicId={publishedTypebot.publicId}
publicId: publishedTypebot.typebot.publicId, isHideQueryParamsEnabled={publishedTypebot.isHideQueryParamsEnabled}
settings: publishedTypebot.settings, background={publishedTypebot.background}
theme: publishedTypebot.theme, metadata={publishedTypebot.metadata}
}}
/> />
) : (
<TypebotPageV2 publishedTypebot={publishedTypebot} {...props} />
) )
} }