Skip to content

Commit

Permalink
fix: Split view incrementation from store
Browse files Browse the repository at this point in the history
Resolves #1213. TODO: Clean up paradigm for view inc in general. Pretty
shit currently.
  • Loading branch information
kiosion committed Apr 5, 2024
1 parent a01d661 commit 0bdd7f3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
6 changes: 4 additions & 2 deletions svelte-app/src/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ const constructUrl = (
return `${basePath}${queryParams.length ? `?${queryParams.join('&')}` : ''}`;
};

const incViews = (fetch: RouteFetch, doc: DocumentRegistry[keyof DocumentRegistry]) => {
export const incViews = (
fetch: RouteFetch,
doc: DocumentRegistry[keyof DocumentRegistry]
) => {
if (doc._type !== 'post' && doc._type !== 'project') {
return;
}
Expand Down Expand Up @@ -185,7 +188,6 @@ const findOne = async <T extends keyof DocumentRegistry>(
if (browser && params.preview !== 'true') {
const cachedData = cacheGet(cacheKey);
if (cachedData) {
incViews(fetch, cachedData as DocumentRegistry[T]);
return cachedData as DocumentRegistry[T];
}
}
Expand Down
4 changes: 3 additions & 1 deletion svelte-app/src/routes/[[lang=lang]]/thoughts/[slug]/+page.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DEFAULT_APP_LANG } from '$lib/consts';
import { handleLoadError } from '$lib/data';
import { findOne } from '$lib/store';
import { findOne, incViews } from '$lib/store';

import type { PageLoad } from './$types';

Expand All @@ -19,5 +19,7 @@ export const load = (async ({ parent, fetch, params, url }) => {

preview && (opts.preview = true);

post && incViews(fetch, post);

return { post, routeFetch: fetch };
}) satisfies PageLoad;
4 changes: 3 additions & 1 deletion svelte-app/src/routes/[[lang=lang]]/work/[slug]/+page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DEFAULT_APP_LANG } from '$lib/consts';
import { fetchRepoStats, handleLoadError } from '$lib/data';
import Logger from '$lib/logger';
import { buildImageUrl, getCrop } from '$lib/sanity';
import { findOne } from '$lib/store';
import { findOne, incViews } from '$lib/store';

import type { PageLoad } from './$types';
import type { ProjectImage } from '$types';
Expand Down Expand Up @@ -83,5 +83,7 @@ export const load = (async ({ parent, fetch, params, url }) => {
project.githubStars = stars;
project.githubWatchers = watchers;

project && incViews(fetch, project);

return { project, images, routeFetch: fetch };
}) satisfies PageLoad;

0 comments on commit 0bdd7f3

Please sign in to comment.