Skip to content

Commit a681bb6

Browse files
committed
use fork of static-publisher; add caching
1 parent 10be481 commit a681bb6

7 files changed

Lines changed: 44 additions & 38 deletions

File tree

compute-js/.npmrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# temporarily override until https://github.com/fastly/compute-js-static-publish/pull/13 is merged
2+
npm config set @fastly:registry https://git.resf.org/api/packages/sig_core/npm/

compute-js/package-lock.json

Lines changed: 5 additions & 7 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.0"
8+
"@fastly/compute-js-static-publish": "^6.0.1"
99
},
1010
"dependencies": {
1111
"@fastly/js-compute": "^3.0.0"

compute-js/src/index.js

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,57 +15,61 @@ addEventListener("fetch", (event) => event.respondWith(handleRequest(event)));
1515
async function handleRequest(event) {
1616
const originalRequest = event.request;
1717
const url = new URL(event.request.url);
18-
const path = url.pathname;
18+
let path = url.pathname;
1919

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

2222
function doLog(msg) {
23-
console.log("[" + path + "]" + msg);
24-
logger.log("[" + path + "]" + msg);
23+
console.log("[" + path + "] " + msg);
24+
logger.log("[" + path + "] " + msg);
2525
}
2626

27+
let cacheOverride = new CacheOverride("override", {
28+
swr: '600', // stale while revalidate
29+
surrogateKey: 'docs',
30+
ttl: 21600,
31+
});
32+
2733
// Check if the requested path has a locale slug (e.g., /fr/)
2834
const localeRegex = /\/(af|de|fr|es|id|it|ja|ko|zh|sv|tr|pl|pt|pt-BR|ru|uk)\//
2935
const hasLocaleSlug = localeRegex.test(path);
3036

37+
var beresp;
38+
3139
// If there's a locale slug, try serving the translation
3240
if (hasLocaleSlug) {
3341
doLog("Attempting to serve localized page for " + path);
34-
var response = await staticContentServer.serveRequest(event.request);
35-
if (response == null || response.status > 400) {
36-
doLog("Failed to serve localized page. Attempting to serve page from Vercel");
37-
// let cacheOverride = new CacheOverride("override", { ttl: 60 });
38-
response = await fetch(event.request, { backend: backendName });
39-
doLog("[vercel] " +response.url+"|"+response.status);
40-
if (response != null && response.status < 400) {
41-
doLog("Fetched localized content from Vercel");
42-
return response
42+
beresp = await staticContentServer.serveRequest(event.request, 'public, max-age=21600, stale-while-revalidate=600');
43+
if (beresp == null || beresp.status > 400) {
44+
// doLog("Failed to serve localized page. Attempting to serve page from Vercel");
45+
beresp = await fetch(event.request, {backend: backendName, cacheOverride});
46+
// doLog("[vercel] " +beresp.url+"|"+beresp.status);
47+
if (beresp != null && beresp.status < 400) {
48+
doLog("Localized content fetched from Vercel");
49+
return beresp
4350
}
4451
}
4552
}
4653

47-
doLog("begin default route handling");
4854
// If no translation is found or there's no locale slug, serve the English version
49-
const englishPath = path.replace(localeRegex, '/');
50-
const englishRequest = new Request(url.origin + englishPath);
51-
const englishResponse = await staticContentServer.serveRequest(englishRequest);
55+
path = hasLocaleSlug ? path.replace(localeRegex, '/') : path;
56+
const bereq = new Request(url.origin + path);
57+
beresp = await staticContentServer.serveRequest(bereq, 'public, max-age=21600, stale-while-revalidate=600');
5258

53-
if (englishResponse != null && englishResponse.status < 400) {
54-
doLog("fetched content from edge cache");
55-
return englishResponse;
59+
if (beresp != null && beresp.status < 400) {
60+
doLog("Static content fetched from edge cache");
61+
return beresp;
5662
}
5763

58-
doLog("failed to fetch content from edge cache; attempt to serve from vercel");
5964
// If we **still** can't find the artifact, try to find it on docs.r.o for the user, I guess
6065
// let cacheOverride = new CacheOverride("override", { ttl: 60 });
61-
response = await fetch(originalRequest, { backend: backendName });
66+
beresp = await fetch(originalRequest, {backend: backendName, cacheOverride});
6267

63-
if (response != null && response.status < 400) {
64-
doLog("fetched content from vercel");
65-
return response
68+
if (beresp != null && beresp.status < 400) {
69+
doLog("content fetched from vercel (fallback)");
70+
return beresp
6671
}
6772

68-
doLog("request failed. return 404");
6973
// If neither translation nor English version is found, return a 404 response
7074
return new Response('Not found', { status: 404 });
7175
}

compute-js/static-publish.rc.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ const config = {
1414
// excludeDotFiles: true,
1515
// includeWellKnown: true,
1616
// contentAssetInclusionTest: (filename) => true,
17-
// contentCompression: [ 'br', 'gzip' ], // For this config value, default is [] if kvStoreName is null.
17+
// contentCompression: [ 'br' ], // 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 },
2121
// ],
2222
server: {
2323
publicDirPrefix: "",
24-
staticItems: ["search/search_index.json"],
25-
staticDir: "../build/minified/assets",
24+
staticItems: ["/search/search_index.json"],
25+
staticDir: "../build/minified/site/assets",
2626
// compression: [ 'br', 'gzip' ],
2727
spaFile: false,
2828
notFoundPageFile: "/404.html",

deploy.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ fi
2424
pushd compute-js || exit $?
2525
npm install
2626
$FASTLY compute build || exit $?
27-
$FASTLY compute deploy -p pkg/resf-rocky-linux-docs.tar.gz
27+
$FASTLY compute deploy -p pkg/resf-rocky-linux-docs.tar.gz || exit $?
2828

2929
popd || exit

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"scripts": {
66
"load-docs": "rm -rf docs; git clone https://github.com/rocky-linux/documentation docs",
77
"dev": "mkdocs serve",
8-
"build": "bash setup-deps.sh && bash build-all.sh && bash deploy.sh"
8+
"prebuild": "bash setup-deps.sh",
9+
"build": "bash build-all.sh",
10+
"postbuild": "bash deploy.sh"
911
},
1012
"dependencies": {
1113
"pngquant": "^4.2.0"

0 commit comments

Comments
 (0)