Skip to content

Commit

Permalink
Merge pull request #908 from MuckRock/cache-typo
Browse files Browse the repository at this point in the history
Header is cache-control, not max-age
  • Loading branch information
eyeseast authored Dec 1, 2024
2 parents ac7226a + 111acfc commit 2eae3bc
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@
</ResultsList>
</Story>

<Story name="Highlighted">
<ResultsList results={highlighted} {count} {next} />
</Story>
<!-- too big to render
<Story name="Highlighted">
<ResultsList results={highlighted} {count} {next} />
</Story>
-->

<Story name="Unverified user">
<ResultsList {results} {count} {next}>
Expand Down
2 changes: 1 addition & 1 deletion src/routes/(app)/add-ons/+layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export async function load({ parent, setHeaders }) {

if (!me) {
setHeaders({
"max-age": `public, max-age=${VIEWER_MAX_AGE}`,
"cache-control": `public, max-age=${VIEWER_MAX_AGE}`,
});
}

Expand Down
11 changes: 1 addition & 10 deletions src/routes/(app)/documents/+layout.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
import { breadcrumbTrail } from "$lib/utils/navigation";

import { VIEWER_MAX_AGE } from "@/config/config.js";

export async function load({ parent, setHeaders }) {
const { me } = await parent();
export async function load({ parent }) {
const breadcrumbs = await breadcrumbTrail(parent, [
{ href: "/documents/", title: "Documents" },
]);

if (!me) {
setHeaders({
"max-age": `public, max-age=${VIEWER_MAX_AGE}`,
});
}

return {
breadcrumbs,
};
Expand Down
12 changes: 10 additions & 2 deletions src/routes/(app)/documents/+page.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { SearchOptions } from "$lib/api/types";

import { DEFAULT_PER_PAGE } from "@/config/config.js";
import { DEFAULT_PER_PAGE, VIEWER_MAX_AGE } from "@/config/config.js";
import { search } from "$lib/api/documents";

export async function load({ url, fetch, data }) {
export async function load({ url, fetch, data, parent, setHeaders }) {
const query = url.searchParams.get("q") || "";
const per_page = +(url.searchParams.get("per_page") ?? DEFAULT_PER_PAGE);
const cursor = url.searchParams.get("cursor") || "";
Expand All @@ -23,6 +23,14 @@ export async function load({ url, fetch, data }) {

const searchResults = search(query, options, fetch);

const { me } = await parent();

if (!me) {
setHeaders({
"cache-control": `public, max-age=${VIEWER_MAX_AGE}`,
});
}

return {
...data,
query,
Expand Down
27 changes: 20 additions & 7 deletions src/routes/(app)/documents/[id]-[slug]/+page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ import type { ReadMode } from "@/lib/api/types";

import { redirect } from "@sveltejs/kit";

import { VIEWER_MAX_AGE } from "@/config/config.js";
import * as documents from "$lib/api/documents";
import { breadcrumbTrail } from "$lib/utils/index";

import loadDocument from "$lib/load/document";
import { getQuery } from "$lib/utils/search";

/** @type {import('./$types').PageLoad} */
export async function load({ fetch, params, parent, depends, url }) {
export async function load({
fetch,
params,
parent,
depends,
url,
setHeaders,
}) {
const { document, asset_url, mode } = await loadDocument({
fetch,
params,
Expand All @@ -34,18 +40,25 @@ export async function load({ fetch, params, parent, depends, url }) {

let action = url.searchParams.get("action");

const breadcrumbs = await breadcrumbTrail(parent, [
{ href: canonical.pathname, title: document.title },
const [breadcrumbs, { me }] = await Promise.all([
breadcrumbTrail(parent, [
{ href: canonical.pathname, title: document.title },
]),
parent(),
]);

const query = getQuery(url);
if (!me) {
setHeaders({
"cache-control": `public, max-age=${VIEWER_MAX_AGE}`,
"last-modified": new Date(document.updated_at).toUTCString(),
});
}

return {
document,
mode,
asset_url,
action,
breadcrumbs,
query,
};
}

0 comments on commit 2eae3bc

Please sign in to comment.