data endpoint for entity 90008 (aka. a website)
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: check if client bot or not when counting visits

dusk 98878514 b7d6f2ad

+26 -16
bun.lockb

This is a binary file and will not be displayed.

+2 -1
package.json
··· 42 42 "@std/toml": "npm:@jsr/std__toml", 43 43 "base64url": "^3.0.1", 44 44 "rehype-autolink-headings": "^7.1.0", 45 - "rehype-slug": "^6.0.0" 45 + "rehype-slug": "^6.0.0", 46 + "typescript-svelte-plugin": "^0.3.42" 46 47 }, 47 48 "trustedDependencies": [ 48 49 "@sveltejs/kit",
+19 -14
src/routes/+layout.server.ts
··· 7 7 export const prerender = 'auto'; 8 8 export const trailingSlash = 'always'; 9 9 10 - export async function load({ cookies, url, setHeaders }) { 11 - setHeaders({ 'Cache-Control': 'no-cache' }) 10 + export async function load({ request, cookies, url, setHeaders }) { 11 + setHeaders({ 'Cache-Control': 'no-cache' }) 12 + let currentVisitCount = get(visitCount) 13 + // check whether the request is from a bot or not (this doesnt need to be accurate we just want to filter out honest bots) 14 + const ua = request.headers.get('user-agent') 15 + const isBot = ua ? ua.toLowerCase().match(/(bot|crawl|spider|walk)/) !== null : true 16 + if (!isBot) { 12 17 const scopedCookies = scopeCookies(cookies, '/') 13 18 // parse the last visit timestamp from cookies if it exists 14 19 const visitedTimestamp = parseInt(scopedCookies.get('visitedTimestamp') || "0") 15 20 // get unix timestamp 16 21 const currentTime = new Date().getTime() 17 22 const timeSinceVisit = currentTime - visitedTimestamp 18 - let currentVisitCount = get(visitCount) 19 23 // check if this is the first time a client is visiting or if an hour has passed since they last visited 20 24 if (visitedTimestamp === 0 || timeSinceVisit > 1000 * 60 * 60 * 24) { 21 - // increment current and write to the store 22 - currentVisitCount += 1; visitCount.set(currentVisitCount) 23 - // update the cookie with the current timestamp 24 - scopedCookies.set('visitedTimestamp', currentTime.toString()) 25 - // write the visit count to a file so we can load it later again 26 - writeFileSync(visitCountFile, currentVisitCount.toString()) 25 + // increment current and write to the store 26 + currentVisitCount += 1; visitCount.set(currentVisitCount) 27 + // update the cookie with the current timestamp 28 + scopedCookies.set('visitedTimestamp', currentTime.toString()) 29 + // write the visit count to a file so we can load it later again 30 + writeFileSync(visitCountFile, currentVisitCount.toString()) 27 31 } 28 - return { 29 - route: url.pathname, 30 - visitCount: currentVisitCount, 31 - } 32 - } 32 + } 33 + return { 34 + route: url.pathname, 35 + visitCount: currentVisitCount, 36 + } 37 + }
+5 -1
tsconfig.json
··· 10 10 "sourceMap": true, 11 11 "strict": true, 12 12 "moduleResolution": "bundler", 13 - "allowImportingTsExtensions": true 13 + "allowImportingTsExtensions": true, 14 + "plugins": [{ 15 + "name": "typescript-svelte-plugin", 16 + "enabled": true 17 + }] 14 18 } 15 19 // Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias 16 20 // except $lib which is handled by https://kit.svelte.dev/docs/configuration#files