feat: add visited count

This commit is contained in:
dusk 2024-10-01 02:44:39 +03:00
parent 2ab43bda7b
commit 9af7bf36e4
Signed by: dusk
SSH Key Fingerprint: SHA256:Abmvag+juovVufZTxyWY8KcVgrznxvBjQpJesv071Aw
6 changed files with 62 additions and 27 deletions

View File

@ -0,0 +1,15 @@
import type { Cookies } from '@sveltejs/kit'
export const scopeCookies = (cookies: Cookies, path: string) => {
return {
get: (key: string) => {
return cookies.get(key)
},
set: (key: string, value: string, props: import('cookie').CookieSerializeOptions = {}) => {
cookies.set(key, value, { ...props, path })
},
delete: (key: string, props: import('cookie').CookieSerializeOptions = {}) => {
cookies.delete(key, { ...props, path })
}
}
}

View File

@ -0,0 +1,25 @@
import { scopeCookies } from '$lib';
import { get, writable } from 'svelte/store';
export const csr = true;
export const ssr = true;
export const prerender = true;
export const trailingSlash = 'always';
const visitCount = writable(0);
export async function load({ cookies, url, setHeaders }) {
setHeaders({ 'Cache-Control': 'no-cache' })
const scopedCookies = scopeCookies(cookies, '/')
const visitedTimestamp = parseInt(scopedCookies.get('visitedTimestamp') || "0")
const currentTime = new Date().getTime()
const timeSinceVisit = currentTime - visitedTimestamp
if (visitedTimestamp === 0 || timeSinceVisit > 1000 * 60 * 60) {
visitCount.set(get(visitCount) + 1)
scopedCookies.set('visitedTimestamp', currentTime.toString())
}
return {
route: url.pathname,
visitCount: get(visitCount),
}
}

View File

@ -150,16 +150,25 @@
{/if} {/if}
{/each} {/each}
<div class="hidden md:block grow" /> <div class="hidden md:block grow" />
<div <div class="navbox">
class="flex gap-3 px-1.5 text-nowrap align-middle items-center text-center place-content-center border-ralsei-white border-groove border-4"
>
<a title="previous site" class="hover:underline" href="https://xn--sr8hvo.ws/previous"></a> <a title="previous site" class="hover:underline" href="https://xn--sr8hvo.ws/previous"></a>
<a class="hover:underline" href="https://xn--sr8hvo.ws">IndieWeb Webring</a> <a class="hover:underline" href="https://xn--sr8hvo.ws">IndieWeb Webring</a>
<a title="next site" class="hover:underline" href="https://xn--sr8hvo.ws/next"></a> <a title="next site" class="hover:underline" href="https://xn--sr8hvo.ws/next"></a>
</div> </div>
<a class="align-middle" href="/entries/_rss"> <div class="navbox">
<img class="min-w-fit hover:opacity-60" src="/valid-rss.png" alt="rss feed" /> <p><span class="text-ralsei-green-light text-shadow-green">{data.visitCount}</span> visit(s)</p>
</a> </div>
<div class="navbox [gap:0.25rem_!important]">
<a class="align-middle hover:underline" href="/entries/_rss">rss</a>
/
<a class="align-middle hover:underline" href="/entries/_jsonfeed">jsonfeed</a>
</div>
</div> </div>
</div> </div>
</nav> </nav>
<style lang="postcss">
.navbox {
@apply flex gap-3 px-1.5 text-nowrap align-middle items-center text-center place-content-center border-ralsei-white border-groove border-4;
}
</style>

View File

@ -1,9 +0,0 @@
export const csr = true;
export const ssr = true;
export const prerender = true;
export const trailingSlash = 'always';
export async function load({ url, setHeaders }) {
setHeaders({'Cache-Control': 'no-cache'})
return { route: url.pathname }
}

View File

@ -1,6 +1,7 @@
import { GUESTBOOK_BASE_URL } from '$env/static/private' import { GUESTBOOK_BASE_URL } from '$env/static/private'
import { redirect, type Cookies } from '@sveltejs/kit' import { redirect, type Cookies } from '@sveltejs/kit'
import auth from '$lib/guestbookAuth' import auth from '$lib/guestbookAuth'
import { scopeCookies as _scopeCookies } from '$lib';
export const prerender = false; export const prerender = false;
@ -11,17 +12,7 @@ interface Entry {
} }
const scopeCookies = (cookies: Cookies) => { const scopeCookies = (cookies: Cookies) => {
return { return _scopeCookies(cookies, '/guestbook')
get: (key: string) => {
return cookies.get(key)
},
set: (key: string, value: string, props: import('cookie').CookieSerializeOptions = {}) => {
cookies.set(key, value, { ...props, path: "/guestbook/" })
},
delete: (key: string, props: import('cookie').CookieSerializeOptions = {}) => {
cookies.delete(key, { ...props, path: "/guestbook/" })
}
}
} }
const postAction = (client: any, scopes: string[]) => { const postAction = (client: any, scopes: string[]) => {

View File

@ -65,8 +65,12 @@
text-shadow: 0 0 4px theme(colors.ralsei.pink.regular); text-shadow: 0 0 4px theme(colors.ralsei.pink.regular);
} }
a,button,input[type=submit] { .text-shadow-green {
text-shadow: 0 0 2px theme(colors.ralsei.black), 0 0 5px theme(colors.ralsei.green.light); text-shadow: 0 0 2px theme(colors.ralsei.black), 0 0 5px theme(colors.ralsei.green.light);
}
a,button,input[type=submit] {
@apply text-shadow-green;
cursor: url('/icons/gaze.png'), pointer; cursor: url('/icons/gaze.png'), pointer;
} }