2
0

fix: 🚑️ Custom domain lookup

This commit is contained in:
Baptiste Arnaud
2022-03-14 16:54:37 +01:00
parent 8954730508
commit 666f0c36bf

View File

@ -11,14 +11,16 @@ export const getServerSideProps: GetServerSideProps = async (
let typebot: Omit<PublicTypebot, 'createdAt' | 'updatedAt'> | null let typebot: Omit<PublicTypebot, 'createdAt' | 'updatedAt'> | null
const isIE = /MSIE|Trident/.test(context.req.headers['user-agent'] ?? '') const isIE = /MSIE|Trident/.test(context.req.headers['user-agent'] ?? '')
const pathname = context.resolvedUrl.split('?')[0] const pathname = context.resolvedUrl.split('?')[0]
console.log('pathname:', pathname)
try { try {
if (!context.req.headers.host) return { props: {} } if (!context.req.headers.host) return { props: {} }
console.log('context.req.headers.host:', context.req.headers.host)
typebot = context.req.headers.host.includes( typebot = context.req.headers.host.includes(
(process.env.NEXT_PUBLIC_VIEWER_URL ?? '').split('//')[1] (process.env.NEXT_PUBLIC_VIEWER_URL ?? '').split('//')[1]
) )
? await getTypebotFromPublicId(context.query.publicId?.toString()) ? await getTypebotFromPublicId(context.query.publicId?.toString())
: await getTypebotFromCustomDomain( : await getTypebotFromCustomDomain(
`${context.req.headers.host}${pathname}` `${context.req.headers.host}${pathname === '/' ? '' : pathname}`
) )
return { return {
props: { props: {
@ -48,8 +50,8 @@ const getTypebotFromPublicId = async (publicId?: string) => {
} }
const getTypebotFromCustomDomain = async (customDomain: string) => { const getTypebotFromCustomDomain = async (customDomain: string) => {
const typebot = await prisma.publicTypebot.findUnique({ const typebot = await prisma.publicTypebot.findFirst({
where: { customDomain }, where: { customDomain: { contains: customDomain } },
}) })
if (isNotDefined(typebot)) return null if (isNotDefined(typebot)) return null
return omit(typebot as unknown as PublicTypebot, 'createdAt', 'updatedAt') return omit(typebot as unknown as PublicTypebot, 'createdAt', 'updatedAt')