From 361ba354cbac3b7df0f14a07dc9d99ece4b48dd2 Mon Sep 17 00:00:00 2001 From: Vladimir Borisov Date: Thu, 8 Jun 2023 12:48:32 +0300 Subject: [PATCH 1/3] Custom HTTP server for proper cache headers --- Dockerfile | 3 +-- http_server.js | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100755 http_server.js diff --git a/Dockerfile b/Dockerfile index 884453382..4eeb450cf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,7 +14,6 @@ RUN mkdir -p /var/www/stremio-web WORKDIR /var/www/stremio-web COPY . /var/www/stremio-web RUN npm install -RUN npm install -g http-server # Bundle app source WORKDIR /var/www/stremio-web @@ -22,4 +21,4 @@ WORKDIR /var/www/stremio-web RUN npm run build EXPOSE 8080 -CMD ["http-server", "/var/www/stremio-web/build/", "-p", "8080", "-d", "false"] +CMD ["node", "http_server.js"] diff --git a/http_server.js b/http_server.js new file mode 100755 index 000000000..9ae2e2c80 --- /dev/null +++ b/http_server.js @@ -0,0 +1,23 @@ +#!/usr/bin/env node + +// Copyright (C) 2017-2023 Smart code 203358507 + +const INDEX_CACHE = 1600; +const ASSETS_CACHE = 3600; +const HTTP_PORT = 8080; + +const express = require('express'); +const path = require('path'); + +const build_path = path.resolve(__dirname, 'build'); +const index_path = path.join(build_path, 'index.html'); + +express().use(express.static(build_path, { + setHeaders: function(res, path) { + if (path === index_path) res.set('cache-control', `public, max-age: ${INDEX_CACHE}`); + else res.set('cache-control', `public, max-age: ${ASSETS_CACHE}`); + } +})).all('*', (_req, res) => { + // TODO: better 404 page + res.status(404).send('

404! Page not found

'); +}).listen(HTTP_PORT, () => console.info(`Server listening on port: ${HTTP_PORT}`)); From 5c17408b205f24d03d91dfd439ffef28ff225145 Mon Sep 17 00:00:00 2001 From: Vladimir Borisov Date: Thu, 8 Jun 2023 12:59:59 +0300 Subject: [PATCH 2/3] Use es6 function for headers --- http_server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/http_server.js b/http_server.js index 9ae2e2c80..5151fb24c 100755 --- a/http_server.js +++ b/http_server.js @@ -13,7 +13,7 @@ const build_path = path.resolve(__dirname, 'build'); const index_path = path.join(build_path, 'index.html'); express().use(express.static(build_path, { - setHeaders: function(res, path) { + setHeaders: (res, path) => { if (path === index_path) res.set('cache-control', `public, max-age: ${INDEX_CACHE}`); else res.set('cache-control', `public, max-age: ${ASSETS_CACHE}`); } From 958cf2edba2e5216c8bf5d42b522066cdcda847d Mon Sep 17 00:00:00 2001 From: Vladimir Borisov Date: Thu, 8 Jun 2023 13:01:10 +0300 Subject: [PATCH 3/3] Increase cache times --- http_server.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/http_server.js b/http_server.js index 5151fb24c..56cdbac10 100755 --- a/http_server.js +++ b/http_server.js @@ -2,8 +2,8 @@ // Copyright (C) 2017-2023 Smart code 203358507 -const INDEX_CACHE = 1600; -const ASSETS_CACHE = 3600; +const INDEX_CACHE = 7200; +const ASSETS_CACHE = 2629744; const HTTP_PORT = 8080; const express = require('express');