-
Notifications
You must be signed in to change notification settings - Fork 125
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
fix cdn race condition #424
Conversation
example/pages/index.tsx
Outdated
const [settings, setSettings] = useState<AnalyticsSettings | undefined>( | ||
undefined | ||
) | ||
const [writeKey, setWriteKey] = useLocalStorage<string>('__dev_writeKey', '') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a somewhat unrelated change that I'm sneaking in -- I got a bit tired of having the writeKey input not persist between refreshes / server restarts / etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
love it!
@@ -42,6 +39,7 @@ export async function loadIntegration( | |||
): Promise<LegacyIntegration> { | |||
const pathName = normalizeName(name) | |||
const obfuscatedPathName = obfuscatePathName(pathName, obfuscate) | |||
const path = getNextIntegrationsURL() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removing this method call from the global scope makes it easier to reason about, especially since it's a wrapper around a mutable global; we don't need to worry about stale references.
example/pages/index.tsx
Outdated
const [writeKey, setWriteKey] = useLocalStorage<string>('__dev_writeKey', '') | ||
const [settings, setSettings] = useState< | ||
AnalyticsBrowserSettings | undefined | ||
>({ | ||
writeKey, | ||
cdnURL: 'https://cdn.segment.com', | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const [writeKey, setWriteKey] = useLocalStorage<string>('__dev_writeKey', '') | |
const [settings, setSettings] = useState< | |
AnalyticsBrowserSettings | undefined | |
>({ | |
writeKey, | |
cdnURL: 'https://cdn.segment.com', | |
}) | |
const [writeKey, setWriteKey] = useLocalStorage<string>('__dev_writeKey', '') | |
const [settings, setSettings] = useState< | |
AnalyticsBrowserSettings | undefined | |
>({ | |
writeKey: process.env.NEXT_PUBLIC_SEGMENT_WRITE_KEY, | |
cdnURL: process.env.NEXT_PUBLIC_SEGMENT_CDN_URL, | |
}) |
I would not like to teach people to do this ... it will cause issues like caching the wrong value or somebody blocking the localStorage
... you named it.
You do you, but I rather teach people to put such information in the env
file since that is probably where people should be injecting such thing, and it will fix your issue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- I didn't even consider that people might want to override cdnURL, but that's a good idea!
- on the subject of writeKey -- on a regular application, I agree. However, this is a playground for debugging (run
make dev
if you haven't yet to see what I mean!) -- so there's a writeKey input field that allows people to swap out writeKeys at runtime. This just makes it easier for people to use their most recent one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't even consider that people might want to override cdnURL, but that's a good idea!
Well, we have to do a proxy to Segment to bypass AdBlocker ... so probably most companies that know a bit about infra would do that most of the time 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
run make dev if you haven't yet to see what I mean!
I see I see, would call the directory "playground" because people are getting used to the idea of using examples
directory to learn how to get up to speed with this ... which is my actually worry (not you, but others reading code out of context)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't even consider that people might want to override cdnURL, but that's a good idea!
Well, we have to do a proxy to Segment to bypass AdBlocker ... so probably most companies that know a bit about infra would do that most of the time 😄
Yep, I just meant the few of us who use the playground (just me?) — I’d just seen it used to demo segment and develop locally directly against the default segment cdn. Anyway, now it’s overridable!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
run make dev if you haven't yet to see what I mean!
I see I see, would call the directory "playground" because people are getting used to the idea of using
examples
directory to learn how to get up to speed with this ... which is my actually worry (not you, but others reading code out of context)
I think it’s meant to serve a duel role, but that’s a good point — it’s not typically what you think of as an ‘example’ in an examples repo!
@@ -17,14 +17,17 @@ const getCDNUrlFromScriptTag = (): string | undefined => { | |||
return cdn | |||
} | |||
|
|||
let _globalCDN: string | undefined // set globalCDN as in-memory singleton |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I LOVE IT
@silesky I think the purpose of the |
691267f
to
2d8f259
Compare
@pooyaj Ah, If that's true that it's a cache, do you mind if we get rid of it? It seems like an unneeded premature optimization, particularly for a function that's only called during a couple initialization steps. It would make things a lot cleaner and remove the possibility of race conditions (which shouldn't happen in practice, but you have to know about the lifecycle to discount that possibility). We can do this in additional PRs FWIW |
952b810
to
c4c2f2c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great!
With regards to your question on if we need to setCdn for standalone/browser.umd, I think @pooyaj's explanation makes sense. If we can guarantee that we would only do the script tag lookup once with the changes you've made we can likely remove it - but I think to keep the scope of this PR focused I'd rather create a new issue or JIRA to track making that change/testing it.
], | ||
}) | ||
expect(fetchCalls[0][0]).toContain(overriddenCDNUrl) | ||
expect.assertions(3) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a neat trick 😄
c4c2f2c
to
8c04016
Compare
8c04016
to
1266c93
Compare
setGlobalCDNUrl
call frombrower-umd.ts
andstandalone.ts
(if anyone has any thoughts / I'm interested!).