Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Toolbar loading from state faster #849

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/extensions/toolbar.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { _getHashParam, _register_event, loadScript, logger } from '../utils'
import { _getHashParam, _register_event, _try, loadScript, logger, window } from '../utils'
import { PostHog } from '../posthog-core'
import { DecideResponse, ToolbarParams } from '../types'
import { POSTHOG_MANAGED_HOSTS } from './cloud'

// TRICKY: Many web frameworks will modify the route on load, potentially before posthog is initialized.
// To get ahead of this we grab it as soon as the posthog-js is parsed
const STATE_FROM_WINDOW = window.location
? _getHashParam(window.location.hash, '__posthog') || _getHashParam(location.hash, 'state')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sneaky but interesting 😊

: null

export class Toolbar {
instance: PostHog
constructor(instance: PostHog) {
Expand Down Expand Up @@ -49,10 +55,24 @@ export class Toolbar {
localStorage = window.localStorage
}

const stateHash = _getHashParam(location.hash, '__posthog') || _getHashParam(location.hash, 'state')
const state = stateHash ? JSON.parse(decodeURIComponent(stateHash)) : null
const parseFromUrl = state && state['action'] === 'ph_authorize'
/**
* Info about the state
* The state is a json object
* 1. (Legacy) The state can be `state={}` as a urlencoded object of info. In this case
* 2. The state should now be found in `__posthog={}` and can be base64 encoded or urlencoded.
* 3. Base64 encoding is preferred and will gradually be rolled out everywhere
*/

const stateHash =
STATE_FROM_WINDOW || _getHashParam(location.hash, '__posthog') || _getHashParam(location.hash, 'state')

let toolbarParams: ToolbarParams
const state = stateHash
? _try(() => JSON.parse(atob(decodeURIComponent(stateHash)))) ||
_try(() => JSON.parse(decodeURIComponent(stateHash)))
: null

const parseFromUrl = state && state['action'] === 'ph_authorize'

if (parseFromUrl) {
// happens if they are initializing the toolbar using an old snippet
Expand Down
8 changes: 8 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,14 @@ export const _formatDate = function (d: Date): string {
)
}

export const _try = function <T>(fn: () => T): T | undefined {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really lovely

try {
return fn()
} catch (e) {
return undefined
}
}

export const _safewrap = function <F extends (...args: any[]) => any = (...args: any[]) => any>(f: F): F {
return function (...args) {
try {
Expand Down
Loading