Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit b9c6489

Browse files
committed
Use localStorage to store count
1 parent 014c0a0 commit b9c6489

File tree

6 files changed

+15
-24
lines changed

6 files changed

+15
-24
lines changed

examples/hello-world-spa/api/counter/[action].ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
import type { APIRequest } from 'aleph/types.ts'
22

3-
const store = globalThis as any
4-
53
export default async function handler(req: APIRequest) {
6-
let count = store.$count || 0
4+
let count = parseInt(localStorage.getItem('count') || '0')
75

86
switch (req.params['action']) {
97
case 'increase':
108
count++
11-
store.$count = count
9+
localStorage.setItem('count', count.toString())
1210
req.json({ count })
1311
break
1412
case 'decrease':
1513
count--
16-
store.$count = count
14+
localStorage.setItem('count', count.toString())
1715
req.json({ count })
1816
break
1917
default:
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { APIRequest } from 'aleph/types.ts'
22

3-
const store = globalThis as any
4-
53
export default async function handler(req: APIRequest) {
6-
req.json({ count: store.$count || 0 })
4+
const count = parseInt(localStorage.getItem('count') || '0')
5+
req.json({ count })
76
}

examples/hello-world-src-dir/src/api/counter/[action].ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
import type { APIRequest } from 'aleph/types.ts'
22

3-
const store = globalThis as any
4-
53
export default async function handler(req: APIRequest) {
6-
let count = store.$count || 0
4+
let count = parseInt(localStorage.getItem('count') || '0')
75

86
switch (req.params['action']) {
97
case 'increase':
108
count++
11-
store.$count = count
9+
localStorage.setItem('count', count.toString())
1210
req.json({ count })
1311
break
1412
case 'decrease':
1513
count--
16-
store.$count = count
14+
localStorage.setItem('count', count.toString())
1715
req.json({ count })
1816
break
1917
default:
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { APIRequest } from 'aleph/types.ts'
22

3-
const store = globalThis as any
4-
53
export default async function handler(req: APIRequest) {
6-
req.json({ count: store.$count || 0 })
4+
const count = parseInt(localStorage.getItem('count') || '0')
5+
req.json({ count })
76
}

examples/hello-world/api/counter/[action].ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
import type { APIRequest } from 'aleph/types.ts'
22

3-
const store = globalThis as any
4-
53
export default async function handler(req: APIRequest) {
6-
let count = store.$count || 0
4+
let count = parseInt(localStorage.getItem('count') || '0')
75

86
switch (req.params['action']) {
97
case 'increase':
108
count++
11-
store.$count = count
9+
localStorage.setItem('count', count.toString())
1210
req.json({ count })
1311
break
1412
case 'decrease':
1513
count--
16-
store.$count = count
14+
localStorage.setItem('count', count.toString())
1715
req.json({ count })
1816
break
1917
default:
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { APIRequest } from 'aleph/types.ts'
22

3-
const store = globalThis as any
4-
53
export default async function handler(req: APIRequest) {
6-
req.json({ count: store.$count || 0 })
4+
const count = parseInt(localStorage.getItem('count') || '0')
5+
req.json({ count })
76
}

0 commit comments

Comments
 (0)