mirror of
https://github.com/BradNut/personal-website-sveltekit
synced 2025-09-08 23:20:18 +00:00
Fix caching for the articles page and only changing the head for title.
This commit is contained in:
parent
eb25b5db49
commit
9f4f39fff1
3 changed files with 19 additions and 16 deletions
|
|
@ -2,25 +2,13 @@ import { json } from '@sveltejs/kit';
|
||||||
import type { RequestHandler, RequestEvent } from './$types';
|
import type { RequestHandler, RequestEvent } from './$types';
|
||||||
import { fetchArticlesApi } from '$root/routes/api';
|
import { fetchArticlesApi } from '$root/routes/api';
|
||||||
|
|
||||||
export const GET: RequestHandler = async ({ url, setHeaders }: RequestEvent) => {
|
export const GET: RequestHandler = async ({ url }: RequestEvent) => {
|
||||||
try {
|
try {
|
||||||
const response = await fetchArticlesApi('get', `fetchArticles`, {
|
const response = await fetchArticlesApi('get', `fetchArticles`, {
|
||||||
page: url?.searchParams?.get('page') || '1'
|
page: url?.searchParams?.get('page') || '1'
|
||||||
});
|
});
|
||||||
|
|
||||||
if (response?.articles) {
|
if (response?.articles) {
|
||||||
if (response?.cacheControl) {
|
|
||||||
if (!response.cacheControl.includes('no-cache')) {
|
|
||||||
setHeaders({
|
|
||||||
'cache-control': response?.cacheControl
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
setHeaders({
|
|
||||||
'cache-control': 'max-age=43200'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return json(response);
|
return json(response);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
|
||||||
|
|
@ -7,13 +7,24 @@ export type ArticlePageLoad = {
|
||||||
totalPages: number;
|
totalPages: number;
|
||||||
limit: number;
|
limit: number;
|
||||||
totalArticles: number;
|
totalArticles: number;
|
||||||
|
cacheControl: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const load: PageServerLoad = async ({ fetch, params }) => {
|
export const load: PageServerLoad = async ({ fetch, params, setHeaders }) => {
|
||||||
const { page } = params;
|
const { page } = params;
|
||||||
const resp = await fetch(`/api/articles?page=${page}`);
|
const resp = await fetch(`/api/articles?page=${page}`);
|
||||||
const { articles, currentPage, totalPages, limit, totalArticles }: ArticlePageLoad =
|
const { articles, currentPage, totalPages, limit, totalArticles, cacheControl }: ArticlePageLoad =
|
||||||
await resp.json();
|
await resp.json();
|
||||||
|
|
||||||
|
if (cacheControl?.includes('no-cache')) {
|
||||||
|
setHeaders({
|
||||||
|
'cache-control': cacheControl
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
setHeaders({
|
||||||
|
'cache-control': 'max-age=43200' // 12 hours
|
||||||
|
});
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
articles,
|
articles,
|
||||||
currentPage,
|
currentPage,
|
||||||
|
|
|
||||||
|
|
@ -11,9 +11,13 @@
|
||||||
let totalArticles: number;
|
let totalArticles: number;
|
||||||
let limit: number;
|
let limit: number;
|
||||||
$: ({ articles, currentPage, totalPages, totalArticles, limit } = data);
|
$: ({ articles, currentPage, totalPages, totalArticles, limit } = data);
|
||||||
|
$: seoTitle = `Tech Articles - Page ${currentPage} | Bradley Shellnut`;
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<SEO title={`Tech Articles - Page ${currentPage}`} />
|
<svelte:head>
|
||||||
|
<title>{seoTitle}</title>
|
||||||
|
<meta name="og:site_name" content={seoTitle} />
|
||||||
|
</svelte:head>
|
||||||
|
|
||||||
<div class="pageStyles">
|
<div class="pageStyles">
|
||||||
<h1 style="margin-bottom: 2rem">Favorite Tech Articles</h1>
|
<h1 style="margin-bottom: 2rem">Favorite Tech Articles</h1>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue