Skip to content

Commit 039a304

Browse files
authored
Merge pull request #7535 from QwikDev/assets-caching
chore: add caching headers for assets/
2 parents cfda2c7 + f0388d4 commit 039a304

File tree

10 files changed

+32
-10
lines changed

10 files changed

+32
-10
lines changed

.changeset/fair-cars-fry.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ The builtin service workers components are deprecated but still exist for backwa
2727

2828
Caching Headers:
2929

30-
The bundles under build/ are named with their content hash and may therefore be cached indefinitely. Typically you should serve `build/**/*.js` with `Cache-Control: public, max-age=31536000, immutable`.
30+
The files under build/ and assets/ are named with their content hash and may therefore be cached indefinitely. Typically you should serve `build/*` and `assets/*` with `Cache-Control: public, max-age=31536000, immutable`.
31+
32+
However, if you changed the rollup configuration for output filenames, you will have to adjust the caching configuration accordingly.
3133

3234
---
3335

@@ -58,4 +60,3 @@ npm run qwik add service-worker
5860
```
5961

6062
This will add a basic service worker setup that you can customize for specific caching strategies, offline support, or other PWA features beyond just prefetching.
61-

packages/docs/public/_headers

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
/build/*
1+
/assets/*
22
Cache-Control: public, max-age=31536000, s-maxage=31536000, immutable
33

4-
/fonts/*
4+
/build/*
55
Cache-Control: public, max-age=31536000, s-maxage=31536000, immutable
66

7-
/*.png
8-
Cache-Control: public, max-age=86400, s-maxage=86400
9-
107
/*.svg
118
Cache-Control: public, max-age=86400, s-maxage=86400
129

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,7 @@ export const onGet: RequestHandler = async ({ cacheControl }) => {
143143
}, "CDN-Cache-Control");
144144
};
145145
```
146+
147+
### Caching bundles and assets
148+
149+
See [Cache Headers](/docs/deployments/#cache-headers) for more information.

packages/insights/public/_headers

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/build/*
22
Cache-Control: public, max-age=31536000, s-maxage=31536000, immutable
3+
/assets/*
4+
Cache-Control: public, max-age=31536000, s-maxage=31536000, immutable

starters/adapters/cloudflare-pages/public/_headers

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@
77

88
/build/*
99
Cache-Control: public, max-age=31536000, s-maxage=31536000, immutable
10+
/assets/*
11+
Cache-Control: public, max-age=31536000, s-maxage=31536000, immutable

starters/adapters/express/src/entry.express.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ declare global {
2525
// Directories where the static assets are located
2626
const distDir = join(fileURLToPath(import.meta.url), "..", "..", "dist");
2727
const buildDir = join(distDir, "build");
28+
const assetsDir = join(distDir, "assets");
2829

2930
// Allow for dynamic port
3031
const PORT = process.env.PORT ?? 3000;
@@ -53,6 +54,10 @@ const app = express();
5354
// Static asset handlers
5455
// https://expressjs.com/en/starter/static-files.html
5556
app.use(`/build`, express.static(buildDir, { immutable: true, maxAge: "1y" }));
57+
app.use(
58+
`/assets`,
59+
express.static(assetsDir, { immutable: true, maxAge: "1y" }),
60+
);
5661
app.use(express.static(distDir, { redirect: false }));
5762

5863
// Use Qwik City's page and endpoint request handler

starters/adapters/fastify/src/entry.fastify.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ declare global {
2121
// Directories where the static assets are located
2222
const distDir = join(fileURLToPath(import.meta.url), "..", "..", "dist");
2323
const buildDir = join(distDir, "build");
24+
const assetsDir = join(distDir, "assets");
2425

2526
// Allow for dynamic port and host
2627
const PORT = parseInt(process.env.PORT ?? "3000");
@@ -39,7 +40,7 @@ const start = async () => {
3940
// await fastify.register(import('@fastify/compress'))
4041

4142
// Handle Qwik City using a plugin
42-
await fastify.register(FastifyQwik, { distDir, buildDir });
43+
await fastify.register(FastifyQwik, { distDir, buildDir, assetsDir });
4344

4445
// Start the fastify server
4546
await fastify.listen({ port: PORT, host: HOST });

starters/adapters/fastify/src/plugins/fastify-qwik.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import render from "../entry.ssr";
99
export interface FastifyQwikOptions {
1010
distDir: string;
1111
buildDir: string;
12+
assetsDir: string;
1213
}
1314

1415
const { router, notFound } = createQwikCity({ render, qwikCityPlan });
@@ -17,7 +18,7 @@ const qwikPlugin: FastifyPluginAsync<FastifyQwikOptions> = async (
1718
fastify,
1819
options,
1920
) => {
20-
const { buildDir, distDir } = options;
21+
const { buildDir, distDir, assetsDir } = options;
2122

2223
fastify.register(fastifyStatic, {
2324
root: buildDir,
@@ -27,6 +28,13 @@ const qwikPlugin: FastifyPluginAsync<FastifyQwikOptions> = async (
2728
decorateReply: false,
2829
});
2930

31+
fastify.register(fastifyStatic, {
32+
root: assetsDir,
33+
prefix: "/assets",
34+
immutable: true,
35+
maxAge: "1y",
36+
});
37+
3038
fastify.register(fastifyStatic, {
3139
root: distDir,
3240
redirect: false,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
/assets/*
2+
Cache-Control: public, max-age=31536000, s-maxage=31536000, immutable
13
/build/*
24
Cache-Control: public, max-age=31536000, s-maxage=31536000, immutable

starters/adapters/vercel-edge/vercel.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
]
1111
},
1212
{
13-
"source": "/build/(.*)",
13+
"source": "/(assets|build)/.*",
1414
"headers": [
1515
{
1616
"key": "Cache-Control",

0 commit comments

Comments
 (0)