Skip to content

Commit

Permalink
Merge pull request #5 from axiomhq/arne/implement-debounce
Browse files Browse the repository at this point in the history
Replace lodash/debounce with custom impl
  • Loading branch information
bahlo authored Jun 7, 2022
2 parents 8b35d64 + c1100a5 commit b27c8fa
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 30 deletions.
23 changes: 11 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

![](./web-vitals-dashboard.png)

Send Web-Vitals from Vercel to [Axiom](https://axiom.co).
Send Web-Vitals and logs from Next.js to [Axiom](https://axiom.co).

## Get started

1. Make sure you have the [Axiom Vercel integration](https://www.axiom.co/vercel) installed
2. In your Vercel project, run `npm install --save next-axiom`
3. Wrap your NextJS config in `withAxiom` like this in `next.config.js`:
1. Make sure you have the [Axiom Vercel integration](https://www.axiom.co/vercel) installed or export `NEXT_PUBLIC_AXIOM_INGEST_ENDPOINT`
2. In your Next.js project, run `npm install --save next-axiom`
3. Wrap your Next.js config in `withAxiom` like this in `next.config.js`:

```js
const { withAxiom } = require('next-axiom');
Expand All @@ -18,7 +18,7 @@ module.exports = withAxiom({
})
```

This will proxy the Axiom ingest call to improve deliverability.
This will proxy the Axiom ingest call from the frontend to improve deliverability.

## Reporting WebVitals

Expand All @@ -30,24 +30,23 @@ export { reportWebVitals } from 'next-axiom';
## Sending Logs

1. Import Axiom's logger

```js
import { log } from 'next-axiom';
```

2. Use the logger to send logs to Axiom, you can attach
other metadata to your logs by passing them as parameters:
2. Use the logger to send logs to Axiom, you can attach other metadata to your
logs by passing them as parameters:
```js
log.info('hello, world!')
log.debug('debugging information', {foo: 'bar', x: 'y'})
log.debug('debugging information', { foo: 'bar', x: 'y' })
log.warn('be careful!')
log.error('ops!')
log.error('oops!')
```

Deploy your site and watch data coming into your Axiom dashboard
Deploy your site and watch data coming into your Axiom dashboard.

The log functions are asynchoronus functions, make sure to wait for them
when its appropriate. Like when running on serverless/edge functions:
when its appropriate, like when running on serverless/edge functions:

```js
await log.info('function will wait for the log to be sent')
Expand Down
7 changes: 7 additions & 0 deletions examples/logger/pages/_middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { NextResponse } from 'next/server'
import { log } from 'next-axiom'

export async function middleware(_req, _ev) {
await log.info("Hello from middleware", { 'bar': 'baz' });
return NextResponse.next()
}
13 changes: 1 addition & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
"typescript": "^4.6.4"
},
"dependencies": {
"cross-fetch": "^3.1.5",
"lodash": "^4.17.21"
"cross-fetch": "^3.1.5"
}
}
}
13 changes: 10 additions & 3 deletions src/webVitals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@ import { isBrowser, proxyPath } from './config';
export { log } from './logger';

const url = `${proxyPath}/web-vitals`;
const _debounce = require('lodash/debounce');

const debounce = (fn: Function, ms = 300) => {
let timeoutId: ReturnType<typeof setTimeout>;
return function (this: any, ...args: any[]) {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => fn.apply(this, args), ms);
};
};

export declare type WebVitalsMetric = NextWebVitalsMetric & { route: string };

const debounceSendMetrics = _debounce(() => sendMetrics(), 1000);
const debouncedSendMetrics = debounce(sendMetrics, 1000);
let collectedMetrics: WebVitalsMetric[] = [];

export function reportWebVitals(metric: NextWebVitalsMetric) {
collectedMetrics.push({ route: window.__NEXT_DATA__?.page, ...metric });
debounceSendMetrics();
debouncedSendMetrics();
}

function sendMetrics() {
Expand Down

0 comments on commit b27c8fa

Please sign in to comment.