fix: partner stack not recorded when not login (#34062)

This commit is contained in:
Joel
2026-03-25 16:16:52 +08:00
committed by GitHub
parent a8e1ff85db
commit f87dafa229
5 changed files with 69 additions and 10 deletions

View File

@ -0,0 +1,45 @@
import { render } from '@testing-library/react'
import PartnerStackCookieRecorder from '../cookie-recorder'
let isCloudEdition = true
const saveOrUpdate = vi.fn()
vi.mock('@/config', () => ({
get IS_CLOUD_EDITION() {
return isCloudEdition
},
}))
vi.mock('../use-ps-info', () => ({
default: () => ({
saveOrUpdate,
}),
}))
describe('PartnerStackCookieRecorder', () => {
beforeEach(() => {
vi.clearAllMocks()
isCloudEdition = true
})
it('should call saveOrUpdate once on mount when running in cloud edition', () => {
render(<PartnerStackCookieRecorder />)
expect(saveOrUpdate).toHaveBeenCalledTimes(1)
})
it('should not call saveOrUpdate when not running in cloud edition', () => {
isCloudEdition = false
render(<PartnerStackCookieRecorder />)
expect(saveOrUpdate).not.toHaveBeenCalled()
})
it('should render null', () => {
const { container } = render(<PartnerStackCookieRecorder />)
expect(container.innerHTML).toBe('')
})
})

View File

@ -0,0 +1,19 @@
'use client'
import { useEffect } from 'react'
import { IS_CLOUD_EDITION } from '@/config'
import usePSInfo from './use-ps-info'
const PartnerStackCookieRecorder = () => {
const { saveOrUpdate } = usePSInfo()
useEffect(() => {
if (!IS_CLOUD_EDITION)
return
saveOrUpdate()
}, [])
return null
}
export default PartnerStackCookieRecorder

View File

@ -24,7 +24,7 @@ const usePSInfo = () => {
}] = useBoolean(false)
const { mutateAsync } = useBindPartnerStackInfo()
// Save to top domain. cloud.dify.ai => .dify.ai
const domain = globalThis.location.hostname.replace('cloud', '')
const domain = globalThis.location?.hostname.replace('cloud', '')
const saveOrUpdate = useCallback(() => {
if (!psPartnerKey || !psClickId)
@ -39,7 +39,7 @@ const usePSInfo = () => {
path: '/',
domain,
})
}, [psPartnerKey, psClickId, isPSChanged])
}, [psPartnerKey, psClickId, isPSChanged, domain])
const bind = useCallback(async () => {
if (psPartnerKey && psClickId && !hasBind) {
@ -59,7 +59,7 @@ const usePSInfo = () => {
Cookies.remove(PARTNER_STACK_CONFIG.cookieName, { path: '/', domain })
setBind()
}
}, [psPartnerKey, psClickId, mutateAsync, hasBind, setBind])
}, [psPartnerKey, psClickId, hasBind, domain, setBind, mutateAsync])
return {
psPartnerKey,
psClickId,