11/// <reference types="@fastly/js-compute" />
22import { getServer } from '../static-publisher/statics.js' ;
33import { CacheOverride } from "fastly:cache-override" ;
4- // import { allowDynamicBackends } from "fastly:experimental";
5- //
6- // allowDynamicBackends(true);
4+ // import { SimpleCache } from 'fastly:cache';
5+
76const staticContentServer = getServer ( ) ;
87
98const backendName = "docs-vercel" ;
109
1110import { Logger } from "fastly:logger" ;
11+ import { env } from "fastly:env" ;
1212
1313// eslint-disable-next-line no-restricted-globals
1414addEventListener ( "fetch" , ( event ) => event . respondWith ( handleRequest ( event ) ) ) ;
1515async 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
0 commit comments