From 051c79d6516d7f2389265bc2822dae64ff308e84 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 8 Oct 2022 10:08:17 -0400 Subject: [PATCH] Improved network debugging --- app/utils/ApiClient.ts | 9 +++++++++ app/utils/errors.ts | 2 ++ server/routes/index.ts | 9 ++++++--- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/app/utils/ApiClient.ts b/app/utils/ApiClient.ts index f4591911a..f9474dc9e 100644 --- a/app/utils/ApiClient.ts +++ b/app/utils/ApiClient.ts @@ -9,6 +9,7 @@ import Logger from "./Logger"; import download from "./download"; import { AuthorizationError, + BadGatewayError, BadRequestError, NetworkError, NotFoundError, @@ -101,6 +102,7 @@ class ApiClient { } let response; + const timeStart = window.performance.now(); try { response = await fetchWithRetry(urlToFetch, { @@ -127,6 +129,7 @@ class ApiClient { } } + const timeEnd = window.performance.now(); const success = response.status >= 200 && response.status < 300; if (options.download && success) { @@ -196,6 +199,12 @@ class ApiClient { ); } + if (response.status === 502) { + throw new BadGatewayError( + `Request to ${urlToFetch} failed in ${timeEnd - timeStart}ms.` + ); + } + const err = new RequestError(`Error ${response.status}`); Logger.error("Request failed", err, { ...error, diff --git a/app/utils/errors.ts b/app/utils/errors.ts index da1d0b3f9..67f4b1763 100644 --- a/app/utils/errors.ts +++ b/app/utils/errors.ts @@ -12,6 +12,8 @@ export class OfflineError extends ExtendableError {} export class ServiceUnavailableError extends ExtendableError {} +export class BadGatewayError extends ExtendableError {} + export class RateLimitExceededError extends ExtendableError {} export class RequestError extends ExtendableError {} diff --git a/server/routes/index.ts b/server/routes/index.ts index 7e14004a4..a066ad5ec 100644 --- a/server/routes/index.ts +++ b/server/routes/index.ts @@ -16,10 +16,11 @@ const isProduction = env.ENVIRONMENT === "production"; const koa = new Koa(); const router = new Router(); -// serve static assets +// serve public assets koa.use( serve(path.resolve(__dirname, "../../../public"), { - maxage: 60 * 60 * 24 * 30 * 1000, + // 1 month expiry, these assets are mostly static but do not contain a hash + maxAge: 30 * 24 * 60 * 60 * 1000, }) ); @@ -43,10 +44,12 @@ if (isProduction) { await send(ctx, pathname, { root: path.join(__dirname, "../../app/"), + // Hashed static assets get 1 year expiry plus immutable flag + maxAge: 365 * 24 * 60 * 60 * 1000, + immutable: true, setHeaders: (res) => { res.setHeader("Service-Worker-Allowed", "/"); res.setHeader("Access-Control-Allow-Origin", "*"); - res.setHeader("Cache-Control", `max-age=${365 * 24 * 60 * 60}`); }, }); } catch (err) {