Skip to content

Commit

Permalink
fix sveltekit redis env loading (#1076)
Browse files Browse the repository at this point in the history
### Description

<!--
✍️ Write a short summary of your work. Screenshots and videos are
welcome!
-->

### Demo URL

<!--
Provide a URL to a live deployment where we can test your PR. If a demo
isn't possible feel free to omit this section.
-->

### Type of Change

- [ ] New Example
- [ ] Example updates (Bug fixes, new features, etc.)
- [ ] Other (changes to the codebase, but not to examples)

### New Example Checklist

- [ ] 🛫 `npm run new-example` was used to create the example
- [ ] 📚 The template wasn't used but I carefuly read the [Adding a new
example](https://github.com/vercel/examples#adding-a-new-example) steps
and implemented them in the example
- [ ] 📱 Is it responsive? Are mobile and tablets considered?
  • Loading branch information
correttojs authored Feb 10, 2025
1 parent 2e244c4 commit 424c2a5
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions storage/kv-redis-sveltekit/src/routes/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { REDIS_URL, KV_URL } from '$env/static/private'
import * as rawEnv from '$env/static/private'
import { createClient } from 'redis'

const kv = await createClient({
url: REDIS_URL ?? KV_URL,
}).connect()
let client: Awaited<ReturnType<typeof createClient>> | null = null

const getClient = async () => {
if (!client) {
client = await createClient({
url:
(rawEnv as { REDIS_URL?: string }).REDIS_URL ??
(rawEnv as { KV_URL?: string }).KV_URL,
}).connect()
}
return client
}

/** @type {import('./$types').PageLoad} */
export async function load() {
const kv = await getClient()
const pageVisits = await kv.get('pageVisits')
await kv.set('pageVisits', Number.parseInt(pageVisits ?? '0', 10) + 1)
const updatedPageVisits = await kv.get('pageVisits')
Expand Down

0 comments on commit 424c2a5

Please sign in to comment.