2
0

fix(viewer): 💚 Attempt to fix LP rewrites

This commit is contained in:
Baptiste Arnaud
2022-02-10 16:23:21 +01:00
parent 92515efcc3
commit 19f4fdb83a
11 changed files with 65 additions and 73 deletions

View File

@ -7,7 +7,7 @@ import prisma from '../libs/prisma'
export const getServerSideProps: GetServerSideProps = async (
context: GetServerSidePropsContext
) => {
let typebot: PublicTypebot | undefined
let typebot: PublicTypebot | null
const isIE = /MSIE|Trident/.test(context.req.headers['user-agent'] ?? '')
const pathname = context.resolvedUrl.split('?')[0]
try {
@ -33,12 +33,12 @@ export const getServerSideProps: GetServerSideProps = async (
const getTypebotFromPublicId = async (
publicId?: string
): Promise<PublicTypebot | undefined> => {
if (!publicId) return
): Promise<PublicTypebot | null> => {
if (!publicId) return null
const typebot = await prisma.publicTypebot.findUnique({
where: { publicId },
})
return (typebot as unknown as PublicTypebot | undefined) ?? undefined
return (typebot as unknown as PublicTypebot) ?? null
}
const App = ({ typebot, ...props }: TypebotPageProps) =>

View File

@ -1,47 +0,0 @@
import { NotFoundPage } from 'layouts/NotFoundPage'
import { PublicTypebot } from 'models'
import { GetServerSideProps, GetServerSidePropsContext } from 'next'
import { TypebotPage, TypebotPageProps } from '../layouts/TypebotPage'
import prisma from '../libs/prisma'
export const getServerSideProps: GetServerSideProps = async (
context: GetServerSidePropsContext
) => {
let typebot: PublicTypebot | undefined
const isIE = /MSIE|Trident/.test(context.req.headers['user-agent'] ?? '')
const pathname = context.resolvedUrl.split('?')[0]
try {
if (!context.req.headers.host) return { props: {} }
typebot = await getTypebotFromUrl(context.req.headers.host)
return {
props: {
typebot,
isIE,
url: `https://${context.req.headers.host}${pathname}`,
},
}
} catch (err) {
console.error(err)
}
return {
props: {
isIE,
url: `https://${context.req.headers.host}${pathname}`,
},
}
}
const getTypebotFromUrl = async (
hostname: string
): Promise<PublicTypebot | undefined> => {
const publicId = hostname.split('.').shift()
if (!publicId) return
const typebot = await prisma.publicTypebot.findUnique({
where: { publicId },
})
return (typebot as unknown as PublicTypebot | undefined) ?? undefined
}
const App = ({ typebot, ...props }: TypebotPageProps) =>
typebot ? <TypebotPage {...props} typebot={typebot} /> : <NotFoundPage />
export default App