feat: save the visitcount to working dir
All checks were successful
create archive with lfs / tag (push) Successful in 9s
All checks were successful
create archive with lfs / tag (push) Successful in 9s
This commit is contained in:
parent
9af7bf36e4
commit
fe82c60fe5
1
.gitignore
vendored
1
.gitignore
vendored
@ -5,6 +5,7 @@ node_modules
|
|||||||
.vercel
|
.vercel
|
||||||
/.svelte-kit
|
/.svelte-kit
|
||||||
/build
|
/build
|
||||||
|
/visitcount
|
||||||
|
|
||||||
# OS
|
# OS
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { scopeCookies } from '$lib';
|
import { scopeCookies } from '$lib';
|
||||||
|
import { existsSync, readFileSync, writeFileSync } from 'fs';
|
||||||
import { get, writable } from 'svelte/store';
|
import { get, writable } from 'svelte/store';
|
||||||
|
|
||||||
export const csr = true;
|
export const csr = true;
|
||||||
@ -6,20 +7,29 @@ export const ssr = true;
|
|||||||
export const prerender = true;
|
export const prerender = true;
|
||||||
export const trailingSlash = 'always';
|
export const trailingSlash = 'always';
|
||||||
|
|
||||||
const visitCount = writable(0);
|
const visitCountFile = 'visitcount'
|
||||||
|
const visitCount = writable(parseInt(existsSync(visitCountFile) ? readFileSync(visitCountFile).toString() : '0'));
|
||||||
|
|
||||||
export async function load({ cookies, url, setHeaders }) {
|
export async function load({ cookies, url, setHeaders }) {
|
||||||
setHeaders({ 'Cache-Control': 'no-cache' })
|
setHeaders({ 'Cache-Control': 'no-cache' })
|
||||||
const scopedCookies = scopeCookies(cookies, '/')
|
const scopedCookies = scopeCookies(cookies, '/')
|
||||||
|
// parse the last visit timestamp from cookies if it exists
|
||||||
const visitedTimestamp = parseInt(scopedCookies.get('visitedTimestamp') || "0")
|
const visitedTimestamp = parseInt(scopedCookies.get('visitedTimestamp') || "0")
|
||||||
|
// get unix timestamp
|
||||||
const currentTime = new Date().getTime()
|
const currentTime = new Date().getTime()
|
||||||
const timeSinceVisit = currentTime - visitedTimestamp
|
const timeSinceVisit = currentTime - visitedTimestamp
|
||||||
if (visitedTimestamp === 0 || timeSinceVisit > 1000 * 60 * 60) {
|
let currentVisitCount = get(visitCount)
|
||||||
visitCount.set(get(visitCount) + 1)
|
// 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())
|
scopedCookies.set('visitedTimestamp', currentTime.toString())
|
||||||
|
// write the visit count to a file so we can load it later again
|
||||||
|
writeFileSync(visitCountFile, currentVisitCount.toString())
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
route: url.pathname,
|
route: url.pathname,
|
||||||
visitCount: get(visitCount),
|
visitCount: currentVisitCount,
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user