Skip to content

Commit

Permalink
Merge pull request #169 from axiomhq/dedupe-logger
Browse files Browse the repository at this point in the history
feat: dedupe useLogger
  • Loading branch information
c-ehrlich committed Nov 17, 2023
2 parents ba5dedc + d491e49 commit 1b39eac
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
11 changes: 11 additions & 0 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "next-axiom",
"description": "Send WebVitals from your Next.js project to Axiom.",
"version": "1.1.0",
"version": "1.1.1",
"author": "Axiom, Inc.",
"license": "MIT",
"contributors": [
Expand Down Expand Up @@ -52,6 +52,7 @@
"typescript": "^5.1.6"
},
"dependencies": {
"remeda": "^1.29.0",
"whatwg-fetch": "^3.6.2"
}
}
20 changes: 13 additions & 7 deletions src/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import { usePathname } from 'next/navigation';
import { Logger, LoggerConfig } from './logger';
import { useEffect } from 'react';
import { useEffect, useMemo } from 'react';
import { useDeepMemo } from './util';

export function useLogger(config: LoggerConfig = {}): Logger {
const path = usePathname();

const memoizedConfig = useDeepMemo({
...config,
args: {
...(config.args ?? {}),
path,
},
});

const logger = useMemo(() => new Logger(memoizedConfig), [memoizedConfig]);

useEffect(() => {
return () => {
if (logger) {
Expand All @@ -12,11 +24,5 @@ export function useLogger(config: LoggerConfig = {}): Logger {
};
}, [path]);

if (!config.args) {
config.args = {};
}
config.args.path = path;

const logger = new Logger(config);
return logger;
}
12 changes: 12 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { useRef } from 'react';
import { equals } from 'remeda';

export function useDeepMemo<T extends Record<string, unknown>>(value: T) {
const ref = useRef<T>();

if (!equals(value, ref.current)) {
ref.current = value;
}

return ref.current;
}

1 comment on commit 1b39eac

@vercel
Copy link

@vercel vercel bot commented on 1b39eac Nov 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

next-axiom-example – ./

next-axiom-example-lemon.vercel.app
next-axiom-example.axiom.dev
next-axiom-example-git-main.axiom.dev

Please sign in to comment.