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 { fetchArticlesApi } from '$root/routes/api';
|
||||
|
||||
export const GET: RequestHandler = async ({ url, setHeaders }: RequestEvent) => {
|
||||
export const GET: RequestHandler = async ({ url }: RequestEvent) => {
|
||||
try {
|
||||
const response = await fetchArticlesApi('get', `fetchArticles`, {
|
||||
page: url?.searchParams?.get('page') || '1'
|
||||
});
|
||||
|
||||
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);
|
||||
}
|
||||
} catch (error) {
|
||||
|
|
|
|||
|
|
@ -7,13 +7,24 @@ export type ArticlePageLoad = {
|
|||
totalPages: number;
|
||||
limit: number;
|
||||
totalArticles: number;
|
||||
cacheControl: string;
|
||||
};
|
||||
|
||||
export const load: PageServerLoad = async ({ fetch, params }) => {
|
||||
export const load: PageServerLoad = async ({ fetch, params, setHeaders }) => {
|
||||
const { page } = params;
|
||||
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();
|
||||
|
||||
if (cacheControl?.includes('no-cache')) {
|
||||
setHeaders({
|
||||
'cache-control': cacheControl
|
||||
});
|
||||
} else {
|
||||
setHeaders({
|
||||
'cache-control': 'max-age=43200' // 12 hours
|
||||
});
|
||||
}
|
||||
return {
|
||||
articles,
|
||||
currentPage,
|
||||
|
|
|
|||
|
|
@ -11,9 +11,13 @@
|
|||
let totalArticles: number;
|
||||
let limit: number;
|
||||
$: ({ articles, currentPage, totalPages, totalArticles, limit } = data);
|
||||
$: seoTitle = `Tech Articles - Page ${currentPage} | Bradley Shellnut`;
|
||||
</script>
|
||||
|
||||
<SEO title={`Tech Articles - Page ${currentPage}`} />
|
||||
<svelte:head>
|
||||
<title>{seoTitle}</title>
|
||||
<meta name="og:site_name" content={seoTitle} />
|
||||
</svelte:head>
|
||||
|
||||
<div class="pageStyles">
|
||||
<h1 style="margin-bottom: 2rem">Favorite Tech Articles</h1>
|
||||
|
|
|
|||
Loading…
Reference in a new issue