2022-05-23 14:51:11 -07:00
|
|
|
import { IncomingMessage } from 'http'
|
2022-02-04 19:00:08 +01:00
|
|
|
import { NotFoundPage } from 'layouts/NotFoundPage'
|
2022-01-06 09:40:56 +01:00
|
|
|
import { PublicTypebot } from 'models'
|
2021-12-23 16:31:56 +01:00
|
|
|
import { GetServerSideProps, GetServerSidePropsContext } from 'next'
|
2022-05-30 11:02:43 +02:00
|
|
|
import sanitizeHtml from 'sanitize-html'
|
2022-03-01 07:13:09 +01:00
|
|
|
import { isDefined, isNotDefined, omit } from 'utils'
|
2021-12-23 16:31:56 +01:00
|
|
|
import { TypebotPage, TypebotPageProps } from '../layouts/TypebotPage'
|
|
|
|
import prisma from '../libs/prisma'
|
|
|
|
|
|
|
|
export const getServerSideProps: GetServerSideProps = async (
|
|
|
|
context: GetServerSidePropsContext
|
|
|
|
) => {
|
2022-03-01 07:13:09 +01:00
|
|
|
let typebot: Omit<PublicTypebot, 'createdAt' | 'updatedAt'> | null
|
2021-12-23 16:31:56 +01:00
|
|
|
const isIE = /MSIE|Trident/.test(context.req.headers['user-agent'] ?? '')
|
|
|
|
const pathname = context.resolvedUrl.split('?')[0]
|
2022-05-23 14:51:11 -07:00
|
|
|
const { host, forwardedHost } = getHost(context.req)
|
2021-12-23 16:31:56 +01:00
|
|
|
try {
|
2022-05-23 14:51:11 -07:00
|
|
|
if (!host) return { props: {} }
|
2022-03-23 08:53:00 +01:00
|
|
|
const viewerUrls = (process.env.NEXT_PUBLIC_VIEWER_URL ?? '').split(',')
|
2022-05-23 14:51:11 -07:00
|
|
|
const isMatchingViewerUrl = viewerUrls.some(
|
|
|
|
(url) =>
|
|
|
|
host.split(':')[0].includes(url.split('//')[1].split(':')[0]) ||
|
|
|
|
(forwardedHost &&
|
|
|
|
forwardedHost
|
|
|
|
.split(':')[0]
|
|
|
|
.includes(url.split('//')[1].split(':')[0]))
|
2022-02-18 14:57:10 +01:00
|
|
|
)
|
2022-05-23 14:51:11 -07:00
|
|
|
const customDomain = `${forwardedHost ?? host}${
|
2022-05-23 14:16:43 -07:00
|
|
|
pathname === '/' ? '' : pathname
|
|
|
|
}`
|
2022-05-20 14:14:37 -07:00
|
|
|
typebot = isMatchingViewerUrl
|
2022-02-18 14:57:10 +01:00
|
|
|
? await getTypebotFromPublicId(context.query.publicId?.toString())
|
2022-05-20 14:14:37 -07:00
|
|
|
: await getTypebotFromCustomDomain(customDomain)
|
|
|
|
if (!typebot)
|
|
|
|
console.log(
|
|
|
|
isMatchingViewerUrl
|
|
|
|
? `Couldn't find publicId: ${context.query.publicId?.toString()}`
|
2022-05-20 15:37:39 -07:00
|
|
|
: `Couldn't find customDomain: ${customDomain}`
|
2022-05-20 14:14:37 -07:00
|
|
|
)
|
2022-05-30 11:02:43 +02:00
|
|
|
const headCode = typebot?.settings.metadata.customHeadCode
|
2021-12-23 16:31:56 +01:00
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
typebot,
|
|
|
|
isIE,
|
2022-05-23 14:51:11 -07:00
|
|
|
url: `https://${forwardedHost ?? host}${pathname}`,
|
2022-05-30 11:02:43 +02:00
|
|
|
customHeadCode:
|
|
|
|
isDefined(headCode) && headCode !== ''
|
|
|
|
? sanitizeHtml(headCode, { allowedTags: ['script', 'meta'] })
|
|
|
|
: null,
|
2021-12-23 16:31:56 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
isIE,
|
2022-05-23 14:51:11 -07:00
|
|
|
url: `https://${forwardedHost ?? host}${pathname}`,
|
2021-12-23 16:31:56 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-18 14:57:10 +01:00
|
|
|
const getTypebotFromPublicId = async (publicId?: string) => {
|
2022-02-10 16:23:21 +01:00
|
|
|
if (!publicId) return null
|
2021-12-23 16:31:56 +01:00
|
|
|
const typebot = await prisma.publicTypebot.findUnique({
|
|
|
|
where: { publicId },
|
|
|
|
})
|
2022-03-01 07:13:09 +01:00
|
|
|
if (isNotDefined(typebot)) return null
|
2022-03-09 15:12:00 +01:00
|
|
|
return omit(typebot as unknown as PublicTypebot, 'createdAt', 'updatedAt')
|
2021-12-23 16:31:56 +01:00
|
|
|
}
|
|
|
|
|
2022-02-18 14:57:10 +01:00
|
|
|
const getTypebotFromCustomDomain = async (customDomain: string) => {
|
2022-03-14 16:54:37 +01:00
|
|
|
const typebot = await prisma.publicTypebot.findFirst({
|
2022-05-02 07:24:24 -07:00
|
|
|
where: { customDomain },
|
2022-02-18 14:57:10 +01:00
|
|
|
})
|
2022-03-01 07:13:09 +01:00
|
|
|
if (isNotDefined(typebot)) return null
|
2022-03-09 15:12:00 +01:00
|
|
|
return omit(typebot as unknown as PublicTypebot, 'createdAt', 'updatedAt')
|
2022-02-18 14:57:10 +01:00
|
|
|
}
|
|
|
|
|
2022-05-23 14:51:11 -07:00
|
|
|
const getHost = (
|
|
|
|
req?: IncomingMessage
|
|
|
|
): { host?: string; forwardedHost?: string } => ({
|
|
|
|
host: req?.headers ? req.headers.host : window.location.host,
|
|
|
|
forwardedHost: req?.headers['x-forwarded-host'] as string | undefined,
|
|
|
|
})
|
|
|
|
|
2022-02-04 19:00:08 +01:00
|
|
|
const App = ({ typebot, ...props }: TypebotPageProps) =>
|
2022-03-01 07:13:09 +01:00
|
|
|
isDefined(typebot) ? (
|
|
|
|
<TypebotPage typebot={typebot} {...props} />
|
|
|
|
) : (
|
|
|
|
<NotFoundPage />
|
|
|
|
)
|
2022-02-04 19:00:08 +01:00
|
|
|
|
2021-12-23 16:31:56 +01:00
|
|
|
export default App
|