Skip to content

Commit

Permalink
🐛 Use custom domain origin for API calls
Browse files Browse the repository at this point in the history
Closes #1629
  • Loading branch information
baptisteArno committed Jul 11, 2024
1 parent d838c2c commit cbaa7e7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 4 additions & 0 deletions apps/viewer/src/components/TypebotPageV3.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Typebot } from '@typebot.io/schemas/features/typebot/typebot'
import { BackgroundType } from '@typebot.io/schemas/features/typebot/theme/constants'
import { defaultSettings } from '@typebot.io/schemas/features/typebot/settings/constants'
import { Font } from '@typebot.io/schemas'
import { useMemo } from 'react'

export type TypebotV3PageProps = {
url: string
Expand Down Expand Up @@ -40,6 +41,8 @@ export const TypebotPageV3 = ({
push(asPath.split('?')[0], undefined, { shallow: true })
}

const apiOrigin = useMemo(() => new URL(url).origin, [url])

return (
<div
style={{
Expand All @@ -58,6 +61,7 @@ export const TypebotPageV3 = ({
typebot={publicId}
onInit={clearQueryParamsIfNecessary}
font={font ?? undefined}
apiHost={apiOrigin}
/>
</div>
)
Expand Down
11 changes: 9 additions & 2 deletions apps/viewer/src/pages/[[...publicId]].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ export const getServerSideProps: GetServerSideProps = async (
const { host, forwardedHost } = getHost(context.req)
log(`host: ${host}`)
log(`forwardedHost: ${forwardedHost}`)
const protocol =
context.req.headers['x-forwarded-proto'] === 'https' ||
(context.req.socket as unknown as { encrypted: boolean }).encrypted
? 'https'
: 'http'

log(`Request protocol: ${protocol}`)
try {
if (!host) return { props: {} }
const viewerUrls = env.NEXT_PUBLIC_VIEWER_URL
Expand All @@ -71,7 +78,7 @@ export const getServerSideProps: GetServerSideProps = async (
props: {
publishedTypebot,
incompatibleBrowser,
url: `https://${forwardedHost ?? host}${pathname}`,
url: `${protocol}://${forwardedHost ?? host}${pathname}`,
},
}
} catch (err) {
Expand All @@ -80,7 +87,7 @@ export const getServerSideProps: GetServerSideProps = async (
return {
props: {
incompatibleBrowser,
url: `https://${forwardedHost ?? host}${pathname}`,
url: `${protocol}://${forwardedHost ?? host}${pathname}`,
},
}
}
Expand Down

0 comments on commit cbaa7e7

Please sign in to comment.