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: save the visitcount to working dir

dusk fe82c60f 9af7bf36

+15 -4
+1
.gitignore
··· 5 5 .vercel 6 6 /.svelte-kit 7 7 /build 8 + /visitcount 8 9 9 10 # OS 10 11 .DS_Store
+14 -4
src/routes/+layout.server.ts
··· 1 1 import { scopeCookies } from '$lib'; 2 + import { existsSync, readFileSync, writeFileSync } from 'fs'; 2 3 import { get, writable } from 'svelte/store'; 3 4 4 5 export const csr = true; ··· 6 7 export const prerender = true; 7 8 export const trailingSlash = 'always'; 8 9 9 - const visitCount = writable(0); 10 + const visitCountFile = 'visitcount' 11 + const visitCount = writable(parseInt(existsSync(visitCountFile) ? readFileSync(visitCountFile).toString() : '0')); 10 12 11 13 export async function load({ cookies, url, setHeaders }) { 12 14 setHeaders({ 'Cache-Control': 'no-cache' }) 13 15 const scopedCookies = scopeCookies(cookies, '/') 16 + // parse the last visit timestamp from cookies if it exists 14 17 const visitedTimestamp = parseInt(scopedCookies.get('visitedTimestamp') || "0") 18 + // get unix timestamp 15 19 const currentTime = new Date().getTime() 16 20 const timeSinceVisit = currentTime - visitedTimestamp 17 - if (visitedTimestamp === 0 || timeSinceVisit > 1000 * 60 * 60) { 18 - visitCount.set(get(visitCount) + 1) 21 + let currentVisitCount = get(visitCount) 22 + // check if this is the first time a client is visiting or if an hour has passed since they last visited 23 + if (visitedTimestamp === 0 || timeSinceVisit > 1000 * 60 * 60 * 24) { 24 + // increment current and write to the store 25 + currentVisitCount += 1; visitCount.set(currentVisitCount) 26 + // update the cookie with the current timestamp 19 27 scopedCookies.set('visitedTimestamp', currentTime.toString()) 28 + // write the visit count to a file so we can load it later again 29 + writeFileSync(visitCountFile, currentVisitCount.toString()) 20 30 } 21 31 return { 22 32 route: url.pathname, 23 - visitCount: get(visitCount), 33 + visitCount: currentVisitCount, 24 34 } 25 35 }