Skip to content

Commit

Permalink
move hooks to server and add cached data
Browse files Browse the repository at this point in the history
  • Loading branch information
ddxv committed Dec 13, 2024
1 parent 2e7d60a commit f17b1fb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
20 changes: 20 additions & 0 deletions frontend/src/hooks.ts → frontend/src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
import type { Handle } from '@sveltejs/kit';
import type { ServerInit } from '@sveltejs/kit';

let appCats: any[] = [];
let appsOverview: any = {};
let companyTypes: any[] = [];

export const init: ServerInit = async () => {
[appCats, appsOverview, companyTypes] = await Promise.all([
fetch(`http://localhost:8000/api/categories`).then((res) => res.json()),
fetch(`http://localhost:8000/api/apps/overview`).then((res) => res.json()),
fetch(`http://localhost:8000/api/companies/types`).then((res) => res.json())
]);
console.log('Data initialized on server start');
};

export const getCachedData = () => ({
appCats,
appsOverview,
companyTypes
});

export const handle: Handle = async ({ event, resolve }) => {
const route = event.url.pathname;
Expand Down
11 changes: 3 additions & 8 deletions frontend/src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import type { LayoutServerLoad } from './$types';

export const load: LayoutServerLoad = async ({ fetch }) => {
console.log(`root layout load appCats, appsOverview, companyTypes start`);
const [appCats, appsOverview, companyTypes] = await Promise.all([
fetch(`http://localhost:8000/api/categories`).then((res) => res.json()),
fetch(`http://localhost:8000/api/apps/overview`).then((res) => res.json()),
fetch(`http://localhost:8000/api/companies/types`).then((res) => res.json())
]);
import { getCachedData } from '../hooks.server';

console.log(`root layout load appCats, appsOverview, companyTypes end`);
export const load: LayoutServerLoad = async () => {
const { appCats, appsOverview, companyTypes } = getCachedData();

return {
appCats: appCats,
Expand Down

0 comments on commit f17b1fb

Please sign in to comment.