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

Support webworkers #161

Merged
merged 10 commits into from
Oct 25, 2023
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;
dasfmi marked this conversation as resolved.
Show resolved Hide resolved
}

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);
dasfmi marked this conversation as resolved.
Show resolved Hide resolved
}

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