Skip to content

Commit b54ecc1

Browse files
authored
Merge pull request #7537 from QwikDev/revert-q-data-no-cache
fix(qwik-city): stop q-data.json cache busting
2 parents f736793 + 7f6fb7f commit b54ecc1

File tree

19 files changed

+194
-197
lines changed

19 files changed

+194
-197
lines changed

.changeset/gentle-bats-thank.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@builder.io/qwik-city': minor
3+
---
4+
5+
FIX: qwik-city no longer forces `q-data.json` downloads, instead relying on the cache headers. This means that you have to make sure your `q-data.json` is served with `Cache-Control` headers that suit you. That file contains all the information about the route and is read for each qwik-city navigation. By default the data is cached for one hour.

packages/docs/public/_headers

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
Cache-Control: public, max-age=604800, s-maxage=604800
1212

1313
/*
14-
Cache-Control: public, max-age=3600, s-maxage=3600, stale-while-revalidate=86400
14+
Cache-Control: public, max-age=3600, s-maxage=3600

packages/docs/src/components/sidebar/sidebar.tsx

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1-
import { component$, sync$, useContext, useOnDocument, useStyles$ } from '@builder.io/qwik';
2-
import { type ContentMenu, useContent, useLocation, routeLoader$ } from '@builder.io/qwik-city';
1+
import {
2+
component$,
3+
sync$,
4+
useComputed$,
5+
useContext,
6+
useOnDocument,
7+
useStyles$,
8+
} from '@builder.io/qwik';
9+
import { server$, useContent, useLocation, type ContentMenu } from '@builder.io/qwik-city';
310
import { GlobalStore } from '../../context';
411
import { CloseIcon } from '../svgs/close-icon';
512
import styles from './sidebar.css?inline';
613

7-
export const useMarkdownItems = routeLoader$(async () => {
8-
const rawData = await Promise.all(
14+
let markdownItems: MarkdownItems | undefined;
15+
let markdownItemsPromise: Promise<MarkdownItems> | undefined;
16+
export const getMarkdownItems = server$(() => {
17+
if (markdownItems) {
18+
return markdownItems;
19+
}
20+
21+
markdownItemsPromise ||= Promise.all(
922
Object.entries(import.meta.glob<{ frontmatter?: MDX }>('../../routes/**/*.{md,mdx}')).map(
1023
async ([k, v]) => {
1124
return [
@@ -20,20 +33,22 @@ export const useMarkdownItems = routeLoader$(async () => {
2033
] as const;
2134
}
2235
)
23-
);
24-
const markdownItems: MarkdownItems = {};
25-
rawData.map(([k, v]) => {
26-
if (v.frontmatter?.updated_at) {
27-
markdownItems[k] = {
28-
title: v.frontmatter.title,
29-
contributors: v.frontmatter.contributors,
30-
created_at: v.frontmatter.created_at,
31-
updated_at: v.frontmatter.updated_at,
32-
};
33-
}
34-
});
36+
).then((rawData) => {
37+
markdownItems = {};
38+
rawData.map(([k, v]) => {
39+
if (v.frontmatter?.updated_at) {
40+
markdownItems![k] = {
41+
title: v.frontmatter.title,
42+
contributors: v.frontmatter.contributors,
43+
created_at: v.frontmatter.created_at,
44+
updated_at: v.frontmatter.updated_at,
45+
};
46+
}
47+
});
3548

36-
return markdownItems;
49+
return markdownItems;
50+
});
51+
return markdownItemsPromise;
3752
});
3853

3954
type MarkdownItems = Record<string, MDX>;
@@ -64,7 +79,7 @@ export const SideBar = component$((props: { allOpen?: boolean }) => {
6479
const globalStore = useContext(GlobalStore);
6580
const { menu } = useContent();
6681
const { url } = useLocation();
67-
const markdownItems = useMarkdownItems();
82+
const markdownItems = useComputed$(() => getMarkdownItems());
6883
const allOpen = url.pathname.startsWith('/qwikcity/') || props.allOpen;
6984

7085
useOnDocument(
@@ -78,7 +93,7 @@ export const SideBar = component$((props: { allOpen?: boolean }) => {
7893
el.scrollTop = savedScroll;
7994
el.style.visibility = 'visible';
8095
}
81-
} catch (err) {
96+
} catch {
8297
//
8398
}
8499
})
@@ -103,7 +118,7 @@ export const SideBar = component$((props: { allOpen?: boolean }) => {
103118
try {
104119
const scrollTop = document.getElementById('qwik-sidebar')!.scrollTop;
105120
sessionStorage.setItem('qwik-sidebar', String(scrollTop));
106-
} catch (err) {
121+
} catch {
107122
//
108123
}
109124
})}

packages/docs/src/routes/community/layout.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import { OnThisPage } from '../../components/on-this-page/on-this-page';
66
import { ContentNav } from '../../components/content-nav/content-nav';
77
import styles from '../docs/docs.css?inline';
88

9-
export { useMarkdownItems } from '../../components/sidebar/sidebar';
10-
119
export default component$(() => {
1210
useStyles$(styles);
1311

packages/docs/src/routes/docs/(qwikcity)/caching/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ created_at: '2023-05-24T03:52:24Z'
1919

2020
Caching responses is critical for keeping your site as fast as possible, both for [pages](/docs/(qwikcity)/pages/index.mdx) as well as [middleware](/docs/(qwikcity)/middleware/index.mdx).
2121

22-
A good default is to use [stale-while-revalidate](https://web.dev/stale-while-revalidate/) caching for all responses.
22+
A option is to use [stale-while-revalidate](https://web.dev/stale-while-revalidate/) caching for all responses. Note that this means that users will see a cached response even if the server is updated, and only when the user refreshes will they see the updated content.
2323

2424
For instance, we can add an `onGet` export to our root [layout](/docs/(qwikcity)/layout/index.mdx) (`src/routes/layout.tsx`) like so, to apply good caching defaults site-wide:
2525

packages/docs/src/routes/docs/layout.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import { createBreadcrumbs, SideBar } from '../../components/sidebar/sidebar';
99
import { GlobalStore } from '../../context';
1010
import styles from './docs.css?inline';
1111

12-
export { useMarkdownItems } from '../../components/sidebar/sidebar';
13-
1412
export default component$(() => {
1513
useStyles$(styles);
1614
const loc = useLocation();

packages/docs/src/routes/examples/[...id]/index!.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,6 @@ export const onGet: RequestHandler = ({ cacheControl }) => {
178178
cacheControl({
179179
public: true,
180180
maxAge: 3600,
181-
sMaxAge: 3600,
182-
staleWhileRevalidate: 86400,
183181
});
184182
};
185183

packages/docs/src/routes/layout.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,5 @@ export const onGet: RequestHandler = ({ cacheControl }) => {
1010
cacheControl({
1111
public: true,
1212
maxAge: 3600,
13-
sMaxAge: 3600,
14-
staleWhileRevalidate: 86400,
1513
});
1614
};

packages/docs/src/routes/playground/index!.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,5 @@ export const onGet: RequestHandler = ({ cacheControl }) => {
135135
cacheControl({
136136
public: true,
137137
maxAge: 3600,
138-
sMaxAge: 3600,
139-
staleWhileRevalidate: 86400,
140138
});
141139
};

packages/docs/src/routes/tutorial/layout.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,5 @@ export const onGet: RequestHandler = ({ cacheControl }) => {
172172
cacheControl({
173173
public: true,
174174
maxAge: 3600,
175-
sMaxAge: 3600,
176-
staleWhileRevalidate: 86400,
177175
});
178176
};

0 commit comments

Comments
 (0)