Skip to content

Commit d15e3cf

Browse files
committed
Improve prerendering
1 parent 864bf15 commit d15e3cf

5 files changed

Lines changed: 33 additions & 18 deletions

File tree

src/routes/(anonymous)/+layout.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/routes/(anonymous)/+page.svelte

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
11
<script lang="ts">
2-
import { metaApi } from '$lib/api';
3-
import { handleApiError } from '$lib/errorhandling/apiErrorHandling';
4-
import { onMount } from 'svelte';
5-
6-
let deviceCount = $state(0);
7-
8-
onMount(async () => {
9-
try {
10-
const { data } = await metaApi.publicGetOnlineDevicesStatistics();
11-
deviceCount = data.devicesOnline;
12-
} catch (err) {
13-
handleApiError(err);
14-
}
15-
});
2+
let { data } = $props();
163
</script>
174

185
<section class="flex flex-col items-center justify-center h-full text-white text-center space-y-6">
@@ -22,7 +9,9 @@
229
</span>
2310
<p class="text-lg md:text-2xl opacity-75">
2411
The go-to platform for safe, reliable, real low-latency remote shocking.<br />
25-
<span class="font-semibold">{deviceCount}</span> people online right now.
12+
{#if data.ok}
13+
<span class="font-semibold">{data.deviceCount}</span> people online right now.
14+
{/if}
2615
</p>
2716
<div class="flex space-x-4 pt-8 text-sm opacity-75">
2817
<a href="https://openshock.org" target="_blank" rel="noopener" class="hover:underline">

src/routes/(anonymous)/+page.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { metaApi } from '$lib/api';
2+
3+
export const ssr = true;
4+
export const prerender = false;
5+
6+
export async function load({ setHeaders }): Promise<{ ok: false, error: string } | { ok: true, deviceCount: number }> {
7+
setHeaders({
8+
'cache-control': 'public, max-age=300'
9+
})
10+
11+
let deviceCount: number;
12+
13+
try {
14+
const { data } = await metaApi.publicGetOnlineDevicesStatistics();
15+
16+
deviceCount = data.devicesOnline;
17+
} catch (e) {
18+
console.error(e);
19+
20+
return {
21+
ok: false,
22+
error: 'Failed to fetch device count',
23+
};
24+
}
25+
26+
return { ok: true, deviceCount };
27+
}

src/routes/(anonymous)/logout/+page.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { goto } from '$app/navigation';
33
import { accountV1Api } from '$lib/api';
44
import { destroyAuth } from '$lib/init';
55

6+
export const prerender = false;
7+
68
export async function load() {
79
if (!browser) return; // Do not run the following on server
810

src/routes/+layout.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
// Set the default for the application
2-
export const ssr = false; // Only this file and pages under it in browser
31
export const prerender = true;

0 commit comments

Comments
 (0)