Skip to content

Commit

Permalink
support web-workers
Browse files Browse the repository at this point in the history
  • Loading branch information
dasfmi committed Oct 24, 2023
1 parent edfe59c commit d5b653d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ import GenericConfig from './platform/generic';
import VercelConfig from './platform/vercel';
import NetlifyConfig from './platform/netlify';

declare global {
var Cloudflare: string;
}

export const Version = require('../package.json').version;
export const isVercel = process.env.NEXT_PUBLIC_AXIOM_INGEST_ENDPOINT || process.env.AXIOM_INGEST_ENDPOINT;
export const isNetlify = process.env.NETLIFY == 'true';
export const isBrowser =
typeof window !== 'undefined' || (typeof self !== 'undefined' && typeof Cloudflare === 'undefined');

// Detect the platform provider, and return the appropriate config
// fallback to generic config if no provider is detected
Expand Down
7 changes: 4 additions & 3 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { config, isVercel, Version } from './config';
import { config, isBrowser, isVercel, Version } from './config';
import { NetlifyInfo } from './platform/netlify';
import { isNoPrettyPrint, throttle } from './shared';

const url = config.getLogsEndpoint();

const LOG_LEVEL = process.env.NEXT_PUBLIC_AXIOM_LOG_LEVEL || 'debug';

export interface LogEvent {
Expand Down Expand Up @@ -178,7 +179,7 @@ export class Logger {
if (typeof fetch === 'undefined') {
const fetch = await require('whatwg-fetch');
return fetch(url, reqOptions).catch(console.error);
} else if (config.isBrowser && isVercel && navigator.sendBeacon) {
} else if (isBrowser && isVercel && navigator.sendBeacon) {
// sendBeacon fails if message size is greater than 64kb, so
// we fall back to fetch.
if (!navigator.sendBeacon(url, body)) {
Expand Down Expand Up @@ -238,7 +239,7 @@ export function prettyPrint(ev: LogEvent) {
let msgString = '';
let args: any[] = [ev.level, ev.message];

if (config.isBrowser) {
if (isBrowser) {
msgString = '%c%s - %s';
args = [`color: ${levelColors[ev.level].browser};`, ...args];
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/platform/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { GetServerSidePropsContext, NextApiRequest } from "next";
import { LogEvent, RequestReport } from "../logger";
import { EndpointType } from "../shared";
import type Provider from "./base";
import { isBrowser } from "../config";

// This is the generic config class for all platforms that doesn't have a special
// implementation (e.g: vercel, netlify). All config classes extends this one.
export default class GenericConfig implements Provider {
proxyPath = '/_axiom';
isBrowser = typeof window !== 'undefined';
shouldSendEdgeReport = false;
token = process.env.NEXT_PUBLIC_AXIOM_TOKEN || process.env.AXIOM_TOKEN;
dataset = process.env.NEXT_PUBLIC_AXIOM_DATASET || process.env.AXIOM_DATASET;
Expand All @@ -24,11 +24,11 @@ export default class GenericConfig implements Provider {
}

getLogsEndpoint(): string {
return this.isBrowser ? `${this.proxyPath}/logs` : this.getIngestURL(EndpointType.logs);
return isBrowser ? `${this.proxyPath}/logs` : this.getIngestURL(EndpointType.logs);
}

getWebVitalsEndpoint(): string {
return this.isBrowser ? `${this.proxyPath}/logs` : this.getIngestURL(EndpointType.webVitals);
return isBrowser ? `${this.proxyPath}/logs` : this.getIngestURL(EndpointType.webVitals);
}

wrapWebVitalsObject(metrics: any[]): any {
Expand Down
5 changes: 5 additions & 0 deletions src/platform/vercel.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isBrowser } from '../config';
import { LogEvent } from '../logger';
import { EndpointType } from '../shared';
import type Provider from './base';
Expand Down Expand Up @@ -27,6 +28,10 @@ export default class VercelConfig extends GenericConfig implements Provider {
return `${this.proxyPath}/web-vitals`;
}

getLogsEndpoint(): string {
return isBrowser ? `${this.proxyPath}/logs` : this.getIngestURL(EndpointType.logs);
}

wrapWebVitalsObject(metrics: any[]) {
return {
webVitals: metrics,
Expand Down
4 changes: 2 additions & 2 deletions src/webVitals/webVitals.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NextWebVitalsMetric } from 'next/app';
import { config, isVercel, Version } from '../config';
import { config, isBrowser, isVercel, Version } from '../config';
import { throttle } from '../shared';

const url = config.getWebVitalsEndpoint();
Expand Down Expand Up @@ -35,7 +35,7 @@ function sendMetrics() {
fetch(url, reqOptions).catch(console.error);
}

if (config.isBrowser && isVercel && navigator.sendBeacon) {
if (isBrowser && isVercel && navigator.sendBeacon) {
try {
// See https://github.com/vercel/next.js/pull/26601
// Navigator has to be bound to ensure it does not error in some browsers
Expand Down

0 comments on commit d5b653d

Please sign in to comment.