Closed ribizli closed 11 months ago
SvelteKit/vite uses Node for the build process so using Deno APIs isn't so easy.
This would technically work:
export async function load() {
let count = 0;
if ('Deno' in globalThis) {
const kv = await Deno.openKv();
count = (await kv.get<number>(['count'])).value ?? 0;
}
return {count};
}
I'm not sure I'd recommend this style for maintainability. At least create something like $lib/deno.js
and keep all this logic in one place.
What I've done in production is have an entirely separate Deno server to handle data and provide API routes for SvelteKit.
@dbushell thanks for the reply, I will go to this direction.
Maybe a last question: do you also experience a huge performance penalty when vite dev is started with deno?
I've just been using Node for development
ok, the whole idea to use KV came from your blog here: https://dbushell.com/2023/06/26/sveltekit-oauth-deno-deploy/.
but this is then just a theoretical possibility, not really a good practice, I see 😉
I wouldn't say it's bad practice it's just that SvelteKit & vite are basically Node.js projects so using another runtime in development isn't easy.
This adapter is just for build/production runtime where Deno KV would be available. I don't have any solid advice on how to handle that during development!
I cannot use Deno API in
+page.server.ts
(probably also.js
) files, becausevite build
ends with an error:The error happens during or after "Analysing routes" step.
The
+page.server.ts
is something like this: