···55.vercel
66/.svelte-kit
77/build
88+/visitcount
89910# OS
1011.DS_Store
+14-4
src/routes/+layout.server.ts
···11import { scopeCookies } from '$lib';
22+import { existsSync, readFileSync, writeFileSync } from 'fs';
23import { get, writable } from 'svelte/store';
3445export const csr = true;
···67export const prerender = true;
78export const trailingSlash = 'always';
8999-const visitCount = writable(0);
1010+const visitCountFile = 'visitcount'
1111+const visitCount = writable(parseInt(existsSync(visitCountFile) ? readFileSync(visitCountFile).toString() : '0'));
10121113export async function load({ cookies, url, setHeaders }) {
1214 setHeaders({ 'Cache-Control': 'no-cache' })
1315 const scopedCookies = scopeCookies(cookies, '/')
1616+ // parse the last visit timestamp from cookies if it exists
1417 const visitedTimestamp = parseInt(scopedCookies.get('visitedTimestamp') || "0")
1818+ // get unix timestamp
1519 const currentTime = new Date().getTime()
1620 const timeSinceVisit = currentTime - visitedTimestamp
1717- if (visitedTimestamp === 0 || timeSinceVisit > 1000 * 60 * 60) {
1818- visitCount.set(get(visitCount) + 1)
2121+ let currentVisitCount = get(visitCount)
2222+ // check if this is the first time a client is visiting or if an hour has passed since they last visited
2323+ if (visitedTimestamp === 0 || timeSinceVisit > 1000 * 60 * 60 * 24) {
2424+ // increment current and write to the store
2525+ currentVisitCount += 1; visitCount.set(currentVisitCount)
2626+ // update the cookie with the current timestamp
1927 scopedCookies.set('visitedTimestamp', currentTime.toString())
2828+ // write the visit count to a file so we can load it later again
2929+ writeFileSync(visitCountFile, currentVisitCount.toString())
2030 }
2131 return {
2232 route: url.pathname,
2323- visitCount: get(visitCount),
3333+ visitCount: currentVisitCount,
2434 }
2535}