Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 18e46d5

Browse files
Fix page view counter
1 parent dd6ba41 commit 18e46d5

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

‎src/routes/+page.svelte‎

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@
22
import LazyLoad from "$lib/LazyLoad.svelte";
33
import watermelon_image from "../images/watermelon.png?enhanced";
44
import { onMount } from "svelte";
5-
import { page_views } from "../store";
5+
import { did_update } from "../store";
66
7+
let page_views = 0;
78
onMount(() => {
8-
conststoredValue=localStorage.getItem("page_views");
9-
if (storedValue) {
10-
console.log("Stored value: ", storedValue);
11-
page_views.set(storedValue);
9+
page_views =parseInt(localStorage.getItem("page_views")||0) +1;
10+
if (!$did_update) {
11+
localStorage.setItem("page_views", page_views);
12+
did_update.update((value) =>!value);
1213
}
13-
14-
page_views.subscribe((value) => {
15-
localStorage.setItem("page_views", ++value); // add page_views by one
16-
});
1714
});
1815
</script>
1916

@@ -22,25 +19,27 @@
2219
>
2320
<LazyLoad>
2421
<h1
25-
class="text-center font-bold text-2xl sm:text-3xl xl:text-5xl px-5 md:px-12 xl:px-24 text-primary py-5"
22+
class="text-center font-bold text-2xl sm:text-3xl xl:text-5xl px-5 md:px-12 xl:px-24 text-primary"
2623
>
27-
{$page_views < 1
24+
{page_views === 1
2825
? "Welcome to the Svelte Netlify Demo"
2926
: "Welcome back!"}
3027
</h1>
3128

32-
<div class="flex justify-center items-center px-5 sm:px-12">
29+
<div
30+
class="flex justify-center items-center px-5 sm:px-12 bg-slate-300 my-10"
31+
>
3332
<enhanced:img
3433
src={watermelon_image}
3534
alt="watermelon"
36-
class="h-[50vh] object-contain"
35+
class="h-[40vh] object-contain"
3736
/>
3837
</div>
39-
{#if $page_views > 1}
38+
{#if page_views > 1}
4039
<h2
41-
class="text-primary px-5 sm:px-12 text-md py-5 flex items-center text-center justify-center"
40+
class="text-primary px-5 sm:px-12 text-md flex items-center text-center justify-center"
4241
>
43-
You have visited this site {$page_views} times
42+
You have visited this site {page_views} times
4443
</h2>
4544
{/if}
4645
</LazyLoad>

‎src/store.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import { writable } from 'svelte/store';
22

3-
export const page_views = writable(0); // counts the total page views (stored locally)
3+
export const did_update = writable(false); // counts the total page views (stored locally)
44

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /