Skip to content

Commit dd654d9

Browse files
authored
fix: prevent running analytics on preview deploys (log-only) (#45)
1 parent eae9634 commit dd654d9

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

src/routes/+layout.server.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
11
export const trailingSlash = 'never' // or 'always' or 'ignore', depending on what you want
22

33
import { dev } from '$app/environment'
4+
import { env } from '$env/dynamic/private'
45

56
function should_allow_analytics({
67
dev,
7-
user_agent,
8+
deploy_context,
9+
user_agent
810
}: {
911
dev: boolean
12+
deploy_context?: string
1013
user_agent: string | null
1114
}): boolean {
12-
if (dev)
13-
return false
15+
if (dev) return false
16+
17+
if (deploy_context !== 'production') return false
1418

1519
if (user_agent) {
1620
user_agent = user_agent.toLowerCase()
1721

18-
if (user_agent.includes('playwright'))
19-
return false
20-
21-
if (user_agent.includes('sentry'))
22-
return false
23-
24-
if (user_agent.includes('bot'))
25-
return false
26-
27-
if (user_agent.includes('headless'))
28-
return false
22+
if (user_agent.includes('playwright')) return false
23+
if (user_agent.includes('sentry')) return false
24+
if (user_agent.includes('bot')) return false
25+
if (user_agent.includes('headless')) return false
2926
}
3027

3128
return true
@@ -34,7 +31,8 @@ function should_allow_analytics({
3431
export function load({ locals, request }) {
3532
let allow_analytics = should_allow_analytics({
3633
dev,
37-
user_agent: request.headers.get('user-agent'),
34+
deploy_context: env.CONTEXT,
35+
user_agent: request.headers.get('user-agent')
3836
})
3937

4038
return {

0 commit comments

Comments
 (0)