Skip to content

Commit dd14b60

Browse files
committed
perf(starters): configure adapters with proper caching headers
1 parent 031eeb4 commit dd14b60

File tree

9 files changed

+57
-3
lines changed

9 files changed

+57
-3
lines changed

.changeset/fair-cars-fry.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,19 @@ FEAT: Major improvements to prefetching with automatic bundle preloading
1616

1717
⚠️ **ATTENTION:**
1818

19-
**Keep** your service worker code as is (either `<ServiceWorkerRegister/>` or `<PrefetchServiceWorker/>`).
19+
- **Keep** your service worker code as is (either `<ServiceWorkerRegister/>` or `<PrefetchServiceWorker/>`).
20+
- **Configure** your server to provide long caching headers.
21+
22+
Service Worker:
2023

2124
This new implementation will use it to uninstall the current service worker to reduce the unnecessary duplication.
2225

2326
The builtin service workers components are deprecated but still exist for backwards compatibility.
2427

28+
Caching Headers:
29+
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`.
31+
2532
---
2633

2734
You can configure the prefetch behavior in your SSR configuration:
@@ -52,3 +59,4 @@ npm run qwik add service-worker
5259
```
5360

5461
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.
62+

starters/adapters/aws-lambda/serverless.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@ functions:
1818
custom:
1919
serverless-offline:
2020
httpPort: 4000
21+
serverless-http:
22+
# Make sure serverless-http serves files correctly
23+
binary:
24+
contentTypes:
25+
- "application/javascript"
26+
- "text/javascript"
27+
- "application/json"
28+
- "text/html"
29+
- "text/css"
30+
- "image/*"
31+
- "font/*"
32+
# the js files under build have hash names and are immutable
33+
cacheControl:
34+
- pattern: "build/**/*.js"
35+
value: "public, max-age=31536000, immutable"
2136

2237
package:
2338
excludeDevDependencies: true

starters/adapters/azure-swa/public/staticwebapp.config.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,12 @@
1919
},
2020
"platform": {
2121
"apiRuntime": "node:18"
22+
},
23+
"responseOverrides": {
24+
"build/**/*.js": {
25+
"headers": {
26+
"Cache-Control": "public, max-age=31536000, immutable"
27+
}
28+
}
2229
}
2330
}

starters/adapters/bun/src/entry.bun.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import render from "./entry.ssr";
1616
const { router, notFound, staticFile } = createQwikCity({
1717
render,
1818
qwikCityPlan,
19+
static: {
20+
cacheControl: "public, max-age=31536000, immutable",
21+
},
1922
});
2023

2124
// Allow for dynamic port

starters/adapters/cloud-run/src/entry.cloud-run.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const { router, notFound, staticFile } = createQwikCity({
5353
render,
5454
qwikCityPlan,
5555
static: {
56-
cacheControl: "public, max-age=31557600",
56+
cacheControl: "public, max-age=31536000, immutable",
5757
},
5858
getOrigin(req) {
5959
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Proto

starters/adapters/deno/src/entry.deno.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import render from "./entry.ssr";
1616
const { router, notFound, staticFile } = createQwikCity({
1717
render,
1818
qwikCityPlan,
19+
static: {
20+
cacheControl: "public, max-age=31536000, immutable",
21+
},
1922
});
2023

2124
// Allow for dynamic port

starters/adapters/firebase/firebase.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@
1919
"function": "app"
2020
}
2121
],
22-
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"]
22+
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
23+
"headers": [
24+
{
25+
"source": "build/**/*.js",
26+
"headers": [
27+
{
28+
"key": "Cache-Control",
29+
"value": "public, max-age=31536000, s-maxage=31536000, immutable"
30+
}
31+
]
32+
}
33+
]
2334
}
2435
}

starters/adapters/node-server/src/entry.node-server.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ const PORT = process.env.PORT ?? 3004;
1919
const { router, notFound, staticFile } = createQwikCity({
2020
render,
2121
qwikCityPlan,
22+
static: {
23+
cacheControl: "public, max-age=31536000, immutable",
24+
},
2225
});
2326

2427
const server = createServer();

starters/adapters/static/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Static Site Generator (Node.js)
22

3+
Be sure to configure your server to serve very long cache headers for the `build/**/*.js` files.
4+
5+
Typically you'd set the `Cache-Control` header for those files to `public, max-age=31536000, immutable`.
6+
37
```shell
48
npm run build.server
59
```

0 commit comments

Comments
 (0)