feat: check if client bot or not when counting visits
All checks were successful
create archive with lfs / tag (push) Successful in 9s

This commit is contained in:
dusk 2024-10-01 23:24:41 +09:00
parent b7d6f2ad3e
commit 988785147c
Signed by: dusk
SSH Key Fingerprint: SHA256:Abmvag+juovVufZTxyWY8KcVgrznxvBjQpJesv071Aw
4 changed files with 26 additions and 16 deletions

BIN
bun.lockb Normal file → Executable file

Binary file not shown.

View File

@ -42,7 +42,8 @@
"@std/toml": "npm:@jsr/std__toml",
"base64url": "^3.0.1",
"rehype-autolink-headings": "^7.1.0",
"rehype-slug": "^6.0.0"
"rehype-slug": "^6.0.0",
"typescript-svelte-plugin": "^0.3.42"
},
"trustedDependencies": [
"@sveltejs/kit",

View File

@ -7,26 +7,31 @@ export const ssr = true;
export const prerender = 'auto';
export const trailingSlash = 'always';
export async function load({ cookies, url, setHeaders }) {
setHeaders({ 'Cache-Control': 'no-cache' })
export async function load({ request, cookies, url, setHeaders }) {
setHeaders({ 'Cache-Control': 'no-cache' })
let currentVisitCount = get(visitCount)
// check whether the request is from a bot or not (this doesnt need to be accurate we just want to filter out honest bots)
const ua = request.headers.get('user-agent')
const isBot = ua ? ua.toLowerCase().match(/(bot|crawl|spider|walk)/) !== null : true
if (!isBot) {
const scopedCookies = scopeCookies(cookies, '/')
// parse the last visit timestamp from cookies if it exists
const visitedTimestamp = parseInt(scopedCookies.get('visitedTimestamp') || "0")
// get unix timestamp
const currentTime = new Date().getTime()
const timeSinceVisit = currentTime - visitedTimestamp
let currentVisitCount = get(visitCount)
// check if this is the first time a client is visiting or if an hour has passed since they last visited
if (visitedTimestamp === 0 || timeSinceVisit > 1000 * 60 * 60 * 24) {
// increment current and write to the store
currentVisitCount += 1; visitCount.set(currentVisitCount)
// update the cookie with the current timestamp
scopedCookies.set('visitedTimestamp', currentTime.toString())
// write the visit count to a file so we can load it later again
writeFileSync(visitCountFile, currentVisitCount.toString())
}
return {
route: url.pathname,
visitCount: currentVisitCount,
// increment current and write to the store
currentVisitCount += 1; visitCount.set(currentVisitCount)
// update the cookie with the current timestamp
scopedCookies.set('visitedTimestamp', currentTime.toString())
// write the visit count to a file so we can load it later again
writeFileSync(visitCountFile, currentVisitCount.toString())
}
}
return {
route: url.pathname,
visitCount: currentVisitCount,
}
}

View File

@ -10,7 +10,11 @@
"sourceMap": true,
"strict": true,
"moduleResolution": "bundler",
"allowImportingTsExtensions": true
"allowImportingTsExtensions": true,
"plugins": [{
"name": "typescript-svelte-plugin",
"enabled": true
}]
}
// Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias
// except $lib which is handled by https://kit.svelte.dev/docs/configuration#files