Skip to content

Commit 929ffcc

Browse files
Merge pull request #108 from MatrixAI/feature-cf-cache-change
Add cf cache header changes for versioned assets.
2 parents 3cd9714 + fb3ccd3 commit 929ffcc

File tree

5 files changed

+28
-4
lines changed

5 files changed

+28
-4
lines changed

.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
CLOUDFLARE_ACCOUNT_ID=5e0333335a21846798fafca9e55e044f
33
# Cloudflare API TOKEN
44
CLOUDFLARE_API_TOKEN=
5+
# Development Enivronment Key
6+
# Change this to 'production' when in production
7+
POLYKEY_DOCS_ENV=development

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ jobs:
6565
deployment_tier: 'development'
6666
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
6767
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
68+
POLYKEY_DOCS_ENV: ${{ secrets.POLYKEY_DOCS_ENV }}
6869
run: |
6970
echo 'Perform service deployment for feature'
7071
nix develop .#ci --command bash -c $'
@@ -124,6 +125,7 @@ jobs:
124125
deployment_tier: 'staging'
125126
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
126127
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
128+
POLYKEY_DOCS_ENV: ${{ secrets.POLYKEY_DOCS_ENV }}
127129
run: |
128130
nix develop .#ci --command bash -c $'
129131
npm run deploy -- --env staging
@@ -186,6 +188,7 @@ jobs:
186188
deployment_tier: 'production'
187189
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
188190
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
191+
POLYKEY_DOCS_ENV: ${{ secrets.POLYKEY_DOCS_ENV }}
189192
run: |
190193
nix develop .#ci --command bash -c $'
191194
npm run deploy -- --env production

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
"scripts": {
55
"lint": "eslint '{src,server,scripts}/**/*.{js,ts,jsx,tsx,json}' 'docusaurus.config.ts'",
66
"lintfix": "eslint '{src,server,scripts}/**/*.{js,ts,jsx,tsx,json}' 'docusaurus.config.ts' --fix",
7+
"lintcontent": "prettier --check '{blog,docs}/**/*.{md,mdx}'",
8+
"lintcontentfix": "prettier --write '{blog,docs}/**/*.{md,mdx}'",
79
"start": "docusaurus start",
810
"swizzle": "docusaurus swizzle",
911
"build": "docusaurus build --out-dir=./public",

scripts/deploy.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ async function main(argv = process.argv) {
6464
];
6565
console.error(['wrangler', ...secretBulkArgs].join(' '));
6666
childProcess.execFileSync('wrangler', secretBulkArgs, {
67-
input: JSON.stringify({}),
67+
input: JSON.stringify({
68+
POLYKEY_DOCS_ENV: process.env.POLYKEY_DOCS_ENV,
69+
}),
6870
});
6971
const deployArgs = ['deploy', '--config', './wrangler.toml', ...restArgs];
7072
console.error(['wrangler', ...deployArgs].join(' '));

server/worker.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import assetManifestJSON from '__STATIC_CONTENT_MANIFEST';
77
export interface Env {
88
// CF Worker Sites automatically injects this to point to the KV namespace
99
__STATIC_CONTENT: string;
10+
POLYKEY_DOCS_ENV: string;
1011
}
1112

1213
const router = ittyRouter.Router();
@@ -30,9 +31,8 @@ function mapRequestToDocs(req: Request): Request {
3031

3132
router.all('*', async (req: Request, env: Env, ctx: ExecutionContext) => {
3233
const cacheControl = {
33-
browserTTL: 30 * 24 * 60 * 60,
3434
edgeTTL: 2 * 24 * 60 * 60,
35-
bypassCache: false,
35+
bypassCache: env.POLYKEY_DOCS_ENV === 'development' ? true : false,
3636
};
3737
const url = new URL(req.url);
3838
// Check if the URL pathname is exactly '/docs'
@@ -44,7 +44,7 @@ router.all('*', async (req: Request, env: Env, ctx: ExecutionContext) => {
4444
// wrangler as a cache busting measure.
4545
const assetManifest = JSON.parse(assetManifestJSON);
4646
try {
47-
return await cloudflareKVAssetHandler.getAssetFromKV(
47+
const response = await cloudflareKVAssetHandler.getAssetFromKV(
4848
{
4949
request: req,
5050
waitUntil: ctx.waitUntil.bind(ctx),
@@ -56,6 +56,14 @@ router.all('*', async (req: Request, env: Env, ctx: ExecutionContext) => {
5656
ASSET_MANIFEST: assetManifest,
5757
},
5858
);
59+
60+
if (req.url.includes('assets')) {
61+
response.headers.set('Cache-Control', 'max-age=31536000, immutable');
62+
} else {
63+
response.headers.set('Cache-Control', 'max-age=86400');
64+
}
65+
66+
return response;
5967
} catch (e) {
6068
if (e instanceof cloudflareKVAssetHandler.NotFoundError) {
6169
console.log('Requested resource not found', e.message);
@@ -74,8 +82,14 @@ router.all('*', async (req: Request, env: Env, ctx: ExecutionContext) => {
7482
ASSET_MANIFEST: assetManifest,
7583
},
7684
);
85+
86+
const headers = new Headers(response404.headers);
87+
88+
headers.set('Cache-Control', 'max-age=86400');
89+
7790
return new Response(response404.body, {
7891
...response404,
92+
headers,
7993
status: 404,
8094
});
8195
} else if (e instanceof cloudflareKVAssetHandler.MethodNotAllowedError) {

0 commit comments

Comments
 (0)