preload data for all

This commit is contained in:
Akis 2023-02-18 18:42:23 +02:00
parent 727bfdaae7
commit 4074e775b1
Signed by untrusted user: akis
GPG Key ID: 267BF5C6677944ED
9 changed files with 98 additions and 63 deletions

View File

@ -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();

View File

@ -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;

View File

@ -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 : ""
};

View File

@ -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;

View File

@ -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(),
users: get(pubnixUsers),
...meta
};
} else {
return {
error: true,
message: "Error: " + request.status,
...meta
};
}
} catch (err) {
return { error: true, message: "Error: " + err, ...meta };
}
}) satisfies PageServerLoad;

View File

@ -31,7 +31,7 @@
<h2>Online users</h2>
{#if !data.error}
{#if !data.users.error}
{#if data.users.users.some(isOnline)}
<div class="flex flex-row flex-wrap gap-4">
{#each data.users.users as user}
@ -44,5 +44,5 @@
<p>No users online</p>
{/if}
{:else}
<p>{data.message}</p>
<p>{data.users.message}</p>
{/if}

View File

@ -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(),
users: get(pubnixUsers),
...meta
};
} else {
return {
error: true,
message: "Error: " + request.status,
...meta
};
}
} catch (err) {
return { error: true, message: "Error: " + err, ...meta };
}
}) satisfies PageServerLoad;

View File

@ -7,7 +7,7 @@
<h1>{data.title}</h1>
{#if !data.error}
{#if !data.users.error}
{#if data.users.users.length > 0}
<div class="flex flex-row flex-wrap gap-4">
{#each data.users.users as user}
@ -18,5 +18,5 @@
<p>No users</p>
{/if}
{:else}
<p>{data.message}</p>
<p>{data.users.message}</p>
{/if}

View File

@ -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({});