-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use debug script during development mode (#15)
- Loading branch information
1 parent
3e85f39
commit 5799c4f
Showing
6 changed files
with
220 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,29 @@ | ||
import { initQueue } from './queue'; | ||
import type { BeforeSend } from './types'; | ||
import type { AnalyticsProps } from './types'; | ||
import { isBrowser, isDevelopment } from './utils'; | ||
|
||
const isBrowser = typeof window !== 'undefined'; | ||
|
||
export const inject = ({ | ||
beforeSend, | ||
}: { beforeSend?: BeforeSend } = {}): void => { | ||
if (!isBrowser) return; | ||
export const inject = ( | ||
{ beforeSend, debug }: AnalyticsProps = { debug: isDevelopment() }, | ||
): void => { | ||
if (!isBrowser()) return; | ||
initQueue(); | ||
|
||
if (beforeSend) { | ||
window.va?.('beforeSend', beforeSend); | ||
} | ||
const src = isDevelopment() | ||
? 'https://cdn.vercel-insights.com/v1/script.debug.js' | ||
: '/_vercel/insights/script.js'; | ||
|
||
if (document.head.querySelector('script[src="/va/script.js"]')) return; | ||
if (document.head.querySelector(`script[src*="${src}"]`)) return; | ||
|
||
const script = document.createElement('script'); | ||
script.src = '/va/script.js'; | ||
script.src = src; | ||
script.defer = true; | ||
|
||
if (isDevelopment() && debug === false) { | ||
script.setAttribute('data-debug', 'false'); | ||
} | ||
|
||
document.head.appendChild(script); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,15 @@ | ||
import React, { useEffect } from 'react'; | ||
import { useEffect } from 'react'; | ||
import { inject } from './generic'; | ||
import type { BeforeSend } from './types'; | ||
import type { AnalyticsProps } from './types'; | ||
import { isDevelopment } from './utils'; | ||
|
||
interface AnalyticsProps { | ||
beforeSend?: BeforeSend; | ||
} | ||
|
||
export function Analytics(props: AnalyticsProps): JSX.Element { | ||
if (process.env.NODE_ENV !== 'production') { | ||
return <NoopAnalytics />; | ||
} | ||
|
||
return <EnabledAnalytics {...props} />; | ||
} | ||
|
||
function EnabledAnalytics({ beforeSend }: AnalyticsProps): null { | ||
useEffect(() => { | ||
inject({ beforeSend }); | ||
}, [beforeSend]); | ||
|
||
return null; | ||
} | ||
|
||
function NoopAnalytics(): null { | ||
export function Analytics({ | ||
beforeSend, | ||
debug = isDevelopment(), | ||
}: AnalyticsProps): null { | ||
useEffect(() => { | ||
// eslint-disable-next-line no-console | ||
console.warn( | ||
'Vercel Analytics is set up, but detected a non-production environment.\n\nPlease note that no analytics events will be sent.', | ||
); | ||
}, []); | ||
inject({ beforeSend, debug }); | ||
}, [beforeSend, debug]); | ||
|
||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export function isBrowser(): boolean { | ||
return typeof window !== 'undefined'; | ||
} | ||
|
||
export function isDevelopment(): boolean { | ||
return ( | ||
typeof process !== 'undefined' && process.env.NODE_ENV !== 'production' | ||
); | ||
} |
Oops, something went wrong.