Skip to content

Commit 71afe57

Browse files
committed
use kv store instead of embedding in wasm
1 parent a681bb6 commit 71afe57

6 files changed

Lines changed: 35 additions & 27 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ docs/
1111

1212
# dev envrc
1313
.envrc.local
14+
15+
node_modules/

compute-js/fastly.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ language = "javascript"
77
manifest_version = 2
88
name = "@resf/rocky-linux-docs"
99
service_id = "lACFUzQjUGLnZsvoHgcnx6"
10-
# kv_store_name = "mkdocs"
10+
kv_store_name = "docs.rockylinux.org"
1111

1212
[scripts]
1313
build = "npm run build"
@@ -17,3 +17,5 @@ service_id = "lACFUzQjUGLnZsvoHgcnx6"
1717
[local_server.backends.docs-vercel]
1818
url = "https://cname.vercel-dns.com/"
1919
override_host = "docs.rockylinux.org"
20+
[local_server.kv_stores]
21+
[[local_server.kv_stores."docs.rockylinux.org"]]

compute-js/package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compute-js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"author": "neil@resf.org",
66
"type": "module",
77
"devDependencies": {
8-
"@fastly/compute-js-static-publish": "^6.0.1"
8+
"@fastly/compute-js-static-publish": "^6.0.3"
99
},
1010
"dependencies": {
1111
"@fastly/js-compute": "^3.0.0"

compute-js/src/index.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
/// <reference types="@fastly/js-compute" />
22
import { getServer } from '../static-publisher/statics.js';
33
import { CacheOverride } from "fastly:cache-override";
4-
// import { allowDynamicBackends } from "fastly:experimental";
5-
//
6-
// allowDynamicBackends(true);
4+
// import { SimpleCache } from 'fastly:cache';
5+
76
const staticContentServer = getServer();
87

98
const backendName = "docs-vercel";
109

1110
import { Logger } from "fastly:logger";
11+
import { env } from "fastly:env";
1212

1313
// eslint-disable-next-line no-restricted-globals
1414
addEventListener("fetch", (event) => event.respondWith(handleRequest(event)));
1515
async function handleRequest(event) {
1616
const originalRequest = event.request;
17-
const url = new URL(event.request.url);
18-
let path = url.pathname;
17+
const url = new URL(originalRequest.url);
18+
const path = url.pathname;
1919

2020
const logger = new Logger("JavaScriptLog");
2121

22-
function doLog(msg) {
23-
console.log("[" + path + "] " + msg);
24-
logger.log("[" + path + "] " + msg);
22+
const region = env("FASTLY_REGION");
23+
const devMode = region === "Somewhere" ? true : false;
24+
25+
async function debugLog(msg) {
26+
if (devMode) {
27+
console.log("[" + path + "] " + msg);
28+
logger.log("[" + path + "] " + msg);
29+
}
2530
}
2631

2732
let cacheOverride = new CacheOverride("override", {
@@ -38,36 +43,35 @@ async function handleRequest(event) {
3843

3944
// If there's a locale slug, try serving the translation
4045
if (hasLocaleSlug) {
41-
doLog("Attempting to serve localized page for " + path);
46+
debugLog("Attempting to serve localized page for " + path);
4247
beresp = await staticContentServer.serveRequest(event.request, 'public, max-age=21600, stale-while-revalidate=600');
4348
if (beresp == null || beresp.status > 400) {
4449
// doLog("Failed to serve localized page. Attempting to serve page from Vercel");
4550
beresp = await fetch(event.request, {backend: backendName, cacheOverride});
4651
// doLog("[vercel] " +beresp.url+"|"+beresp.status);
4752
if (beresp != null && beresp.status < 400) {
48-
doLog("Localized content fetched from Vercel");
49-
return beresp
53+
debugLog("Localized content fetched from Vercel");
54+
return beresp;
5055
}
5156
}
5257
}
5358

5459
// If no translation is found or there's no locale slug, serve the English version
55-
path = hasLocaleSlug ? path.replace(localeRegex, '/') : path;
60+
fallbackPath = hasLocaleSlug ? path.replace(localeRegex, '/') : path;
5661
const bereq = new Request(url.origin + path);
57-
beresp = await staticContentServer.serveRequest(bereq, 'public, max-age=21600, stale-while-revalidate=600');
58-
59-
if (beresp != null && beresp.status < 400) {
60-
doLog("Static content fetched from edge cache");
62+
beresp = await staticContentServer.serveRequest(bereq, 'public, max-age=21600, stale-while-revalidate=600')
63+
if (beresp != null && beresp.ok) {
64+
debugLog("Static content fetched from edge cache");
6165
return beresp;
6266
}
6367

68+
6469
// If we **still** can't find the artifact, try to find it on docs.r.o for the user, I guess
65-
// let cacheOverride = new CacheOverride("override", { ttl: 60 });
6670
beresp = await fetch(originalRequest, {backend: backendName, cacheOverride});
6771

6872
if (beresp != null && beresp.status < 400) {
69-
doLog("content fetched from vercel (fallback)");
70-
return beresp
73+
debugLog("content fetched from vercel (fallback)");
74+
return beresp;
7175
}
7276

7377
// If neither translation nor English version is found, return a 404 response

compute-js/static-publish.rc.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
const config = {
1010
rootDir: "../build/minified/site",
1111
staticContentRootDir: "./static-publisher",
12-
// kvStoreName: "mkdocs",
12+
kvStoreName: "docs.rockylinux.org",
1313
// excludeDirs: [ './node_modules' ],
1414
// excludeDotFiles: true,
1515
// includeWellKnown: true,
1616
// contentAssetInclusionTest: (filename) => true,
17-
// contentCompression: [ 'br' ], // For this config value, default is [] if kvStoreName is null.
17+
contentCompression: [ 'br', 'gzip' ], // For this config value, default is [] if kvStoreName is null.
1818
// moduleAssetInclusionTest: (filename) => false,
1919
// contentTypes: [
2020
// { test: /.custom$/, contentType: 'application/x-custom', text: false },

0 commit comments

Comments
 (0)