From 4074e775b18018f7fa96dec16678a1e849793a6d Mon Sep 17 00:00:00 2001 From: Akis Date: Sat, 18 Feb 2023 18:42:23 +0200 Subject: [PATCH] preload data for all --- src/hooks.server.ts | 66 ++++++++++++++++++++++++- src/routes/blog/+page.server.ts | 7 +-- src/routes/blog/[title]/+page.server.ts | 5 +- src/routes/blog/fetchGhost.ts | 19 +------ src/routes/pubnix/+page.server.ts | 24 +++------ src/routes/pubnix/+page.svelte | 4 +- src/routes/pubnix/users/+page.server.ts | 24 +++------ src/routes/pubnix/users/+page.svelte | 4 +- src/stores.ts | 8 +++ 9 files changed, 98 insertions(+), 63 deletions(-) diff --git a/src/hooks.server.ts b/src/hooks.server.ts index c46f43b..ea5d35a 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -5,7 +5,7 @@ import type { Provider } from "@auth/core/providers"; import type { Profile } from "@auth/core/types"; import { redirect, type Handle } from "@sveltejs/kit"; import { sequence } from "@sveltejs/kit/hooks"; -import { announcements } from "./stores"; +import { announcements, pubnixUsers, blogPosts, blogTags, blogAuthors } from "./stores"; import axios from "axios"; import { Agent } from "https"; @@ -59,6 +59,19 @@ export const handle: Handle = sequence( } ); +export const fetchGhost = async (action: string, additional?: string) => { + return await axios( + env.GHOST_URL + + "/ghost/api/content/" + + action + + "/?key=" + + env.GHOST_API_KEY + + "&include=authors,tags&limit=all&formats=html,plaintext" + + (additional ? additional : ""), + { httpsAgent: agent, timeout: 10000 } + ); +}; + const updateMap = async () => { try { const res = await axios( @@ -74,6 +87,57 @@ const updateMap = async () => { } catch (err) { announcements.set({ error: true, message: "Error: " + err }); } + + try { + const res = await axios( + "https://publapi.p.projectsegfau.lt/users", + { httpsAgent: agent, timeout: 10000 } + ); + + if (res.status === 200) { + pubnixUsers.set(res.data); + } else { + pubnixUsers.set({ error: true, message: "Error: " + res.status }); + } + } catch (err) { + pubnixUsers.set({ error: true, message: "Error: " + err }); + } + + try { + const res = await fetchGhost("posts"); + + if (res.status === 200) { + blogPosts.set(res.data); + } else { + blogPosts.set({ error: true, message: "Error: " + res.status }); + } + } catch (err) { + blogPosts.set({ error: true, message: "Error: " + err }); + } + + try { + const res = await fetchGhost("tags"); + + if (res.status === 200) { + blogTags.set(res.data); + } else { + blogTags.set({ error: true, message: "Error: " + res.status }); + } + } catch (err) { + blogTags.set({ error: true, message: "Error: " + err }); + } + + try { + const res = await fetchGhost("authors"); + + if (res.status === 200) { + blogAuthors.set(res.data); + } else { + blogAuthors.set({ error: true, message: "Error: " + res.status }); + } + } catch (err) { + blogAuthors.set({ error: true, message: "Error: " + err }); + } }; updateMap(); diff --git a/src/routes/blog/+page.server.ts b/src/routes/blog/+page.server.ts index 6aaf574..022054c 100644 --- a/src/routes/blog/+page.server.ts +++ b/src/routes/blog/+page.server.ts @@ -1,13 +1,14 @@ import type { PageServerLoad } from "./$types"; -import fetchGhost from "./fetchGhost"; +import { blogPosts } from "../../stores"; +import { get } from "svelte/store"; -export const load = (async ({ fetch }) => { +export const load = (async () => { const meta = { title: "Blog" }; return { - posts: fetchGhost("posts"), + posts: get(blogPosts), ...meta }; }) satisfies PageServerLoad; diff --git a/src/routes/blog/[title]/+page.server.ts b/src/routes/blog/[title]/+page.server.ts index 4cce993..9d6d9e4 100644 --- a/src/routes/blog/[title]/+page.server.ts +++ b/src/routes/blog/[title]/+page.server.ts @@ -1,11 +1,12 @@ import type { PageServerLoad } from "./$types"; import fetchGhost from "../fetchGhost"; +import { blogPosts } from "../../../stores"; +import { get } from "svelte/store"; export const load = (async ({ params, fetch }) => { const data = await fetchGhost("posts/slug/" + params.title); - const allPosts = await fetchGhost("posts"); - + const allPosts = get(blogPosts); const meta = { title: !allPosts.error ? data.posts[0].title : "" }; diff --git a/src/routes/blog/fetchGhost.ts b/src/routes/blog/fetchGhost.ts index 5fc12fa..58275fc 100644 --- a/src/routes/blog/fetchGhost.ts +++ b/src/routes/blog/fetchGhost.ts @@ -1,23 +1,8 @@ -import { env } from "$env/dynamic/private"; -import axios from "axios"; -import { Agent } from "https"; - -const agent = new Agent({ - family: 4 -}); +import { fetchGhost as func } from "../../hooks.server"; const fetchGhost = async (action: string, additional?: string) => { try { - const request = await axios( - env.GHOST_URL + - "/ghost/api/content/" + - action + - "/?key=" + - env.GHOST_API_KEY + - "&include=authors,tags&limit=all&formats=html,plaintext" + - (additional ? additional : ""), - { httpsAgent: agent } - ); + const request = await func(action, additional); if (request.status === 200) { return request.data; diff --git a/src/routes/pubnix/+page.server.ts b/src/routes/pubnix/+page.server.ts index 4499957..f48f33c 100644 --- a/src/routes/pubnix/+page.server.ts +++ b/src/routes/pubnix/+page.server.ts @@ -1,26 +1,14 @@ import type { PageServerLoad } from "./$types"; +import { pubnixUsers } from "../../stores"; +import { get } from "svelte/store"; -export const load = (async ({ fetch }) => { +export const load = (async () => { const meta = { title: "Pubnix" }; - try { - const request = await fetch("https://publapi.p.projectsegfau.lt/users"); - - if (request.ok) { - return { - users: await request.json(), - ...meta - }; - } else { - return { - error: true, - message: "Error: " + request.status, - ...meta - }; - } - } catch (err) { - return { error: true, message: "Error: " + err, ...meta }; + return { + users: get(pubnixUsers), + ...meta } }) satisfies PageServerLoad; diff --git a/src/routes/pubnix/+page.svelte b/src/routes/pubnix/+page.svelte index 3c8923d..6b55f38 100644 --- a/src/routes/pubnix/+page.svelte +++ b/src/routes/pubnix/+page.svelte @@ -31,7 +31,7 @@

Online users

-{#if !data.error} +{#if !data.users.error} {#if data.users.users.some(isOnline)}
{#each data.users.users as user} @@ -44,5 +44,5 @@

No users online

{/if} {:else} -

{data.message}

+

{data.users.message}

{/if} diff --git a/src/routes/pubnix/users/+page.server.ts b/src/routes/pubnix/users/+page.server.ts index 1ec2b6e..3960332 100644 --- a/src/routes/pubnix/users/+page.server.ts +++ b/src/routes/pubnix/users/+page.server.ts @@ -1,26 +1,14 @@ import type { PageServerLoad } from "./$types"; +import { pubnixUsers } from "../../../stores"; +import { get } from "svelte/store"; -export const load = (async ({ fetch }) => { +export const load = (async () => { const meta = { title: "Pubnix users" }; - try { - const request = await fetch("https://publapi.p.projectsegfau.lt/users"); - - if (request.ok) { - return { - users: await request.json(), - ...meta - }; - } else { - return { - error: true, - message: "Error: " + request.status, - ...meta - }; - } - } catch (err) { - return { error: true, message: "Error: " + err, ...meta }; + return { + users: get(pubnixUsers), + ...meta } }) satisfies PageServerLoad; diff --git a/src/routes/pubnix/users/+page.svelte b/src/routes/pubnix/users/+page.svelte index f099c4e..6610b2e 100644 --- a/src/routes/pubnix/users/+page.svelte +++ b/src/routes/pubnix/users/+page.svelte @@ -7,7 +7,7 @@

{data.title}

-{#if !data.error} +{#if !data.users.error} {#if data.users.users.length > 0}
{#each data.users.users as user} @@ -18,5 +18,5 @@

No users

{/if} {:else} -

{data.message}

+

{data.users.message}

{/if} diff --git a/src/stores.ts b/src/stores.ts index b97b945..d11523e 100644 --- a/src/stores.ts +++ b/src/stores.ts @@ -1,3 +1,11 @@ import { writable, type Writable } from "svelte/store"; export const announcements: Writable<{}> = writable({}); + +export const pubnixUsers: Writable<{}> = writable({}); + +export const blogPosts: Writable<{}> = writable({}); + +export const blogTags: Writable<{}> = writable({}); + +export const blogAuthors: Writable<{}> = writable({}); \ No newline at end of file