Skip to content

Commit

Permalink
chore: update ua-parser-js
Browse files Browse the repository at this point in the history
  • Loading branch information
olros committed Nov 28, 2024
1 parent 14e1017 commit 3ca8c78
Show file tree
Hide file tree
Showing 4 changed files with 1,770 additions and 1,761 deletions.
2 changes: 1 addition & 1 deletion web/app/routes/api.$teamSlug.$projectSlug.pageview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const getPageViewUserAgentData = (ua: UserAgentData): PageviewRequestData
const getPageViewNextRequest = async (request: Request): Promise<Request | undefined> => {
const geo = geolocation(request);
const ip = ipAddress(request);
const ua = userAgent(request);
const ua = await userAgent(request);
const date = new Date();

const geoData: PageviewRequestData['geo'] | undefined =
Expand Down
18 changes: 7 additions & 11 deletions web/app/user-agent.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import type { IResult } from 'ua-parser-js';
import { UAParser } from 'ua-parser-js';
import { isBot, isAIBot } from 'ua-parser-js/helpers';

export type UserAgentData = IResult & {
isBot: boolean;
};

const isBot = (input: string): boolean =>
/Googlebot|Mediapartners-Google|AdsBot-Google|googleweblight|Storebot-Google|Google-PageRenderer|Google-InspectionTool|Bingbot|BingPreview|Slurp|DuckDuckBot|baiduspider|yandex|sogou|LinkedInBot|bitlybot|tumblr|vkShare|quora link preview|facebookexternalhit|facebookcatalog|Twitterbot|applebot|redditbot|Slackbot|Discordbot|WhatsApp|SkypeUriPreview|ia_archiver/i.test(
input,
);

const userAgentFromString = (input: string | undefined): UserAgentData => ({
...UAParser(input),
isBot: input === undefined ? false : isBot(input),
});

export const userAgent = (request: Request): UserAgentData => userAgentFromString(request.headers.get('user-agent') || undefined);
export const userAgent = async (request: Request): Promise<UserAgentData> => {
const result = await UAParser(Object.fromEntries(request.headers)).withClientHints();
return ({
...result,
isBot: !result ? false : isBot(result) || isAIBot(result),
});}
3 changes: 1 addition & 2 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"tailwindcss-animate": "^1.0.7",
"three": "^0.170.0",
"tiny-invariant": "^1.3.3",
"ua-parser-js": "^1.0.39",
"ua-parser-js": "^2.0.0",
"uglify-js": "^3.19.3",
"undici": "^7.0.0"
},
Expand All @@ -93,7 +93,6 @@
"@types/react-dom": "^18.3.1",
"@types/react-syntax-highlighter": "^15.5.13",
"@types/three": "^0.170.0",
"@types/ua-parser-js": "^0.7.39",
"@types/uglify-js": "^3.17.5",
"eslint": "^8.57.1",
"eslint-config-prettier": "^9.1.0",
Expand Down
Loading

0 comments on commit 3ca8c78

Please sign in to comment.