From b939e24f675dd7d480399557c8046a050e8fd6d4 Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Mon, 28 Oct 2024 11:01:31 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=84=20feat:=20Add=20Configurable=20Cac?= =?UTF-8?q?he=20Headers=20for=20Index.html=20(#4565)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: move o1 model check, after vision request check * feat(server): add configurable cache headers for index.html • Add environment variables to control index.html cache headers • Default to no-cache configuration for consistent app updates • Document cache control options in .env.example --- .env.example | 16 ++++++++++++++++ api/app/clients/OpenAIClient.js | 4 ++-- api/server/index.js | 10 ++++++++-- index.html | 4 ---- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/.env.example b/.env.example index 5fd1711a01..75604e4447 100644 --- a/.env.example +++ b/.env.example @@ -491,3 +491,19 @@ HELP_AND_FAQ_URL=https://librechat.ai # E2E_USER_EMAIL= # E2E_USER_PASSWORD= + +#=====================================================# +# Cache Headers # +#=====================================================# +# Headers that control caching of the index.html # +# Default configuration prevents caching to ensure # +# users always get the latest version. Customize # +# only if you understand caching implications. # + +# INDEX_HTML_CACHE_CONTROL=no-cache, no-store, must-revalidate +# INDEX_HTML_PRAGMA=no-cache +# INDEX_HTML_EXPIRES=0 + +# no-cache: Forces validation with server before using cached version +# no-store: Prevents storing the response entirely +# must-revalidate: Prevents using stale content when offline \ No newline at end of file diff --git a/api/app/clients/OpenAIClient.js b/api/app/clients/OpenAIClient.js index d2762ca1d7..bc773db172 100644 --- a/api/app/clients/OpenAIClient.js +++ b/api/app/clients/OpenAIClient.js @@ -100,8 +100,6 @@ class OpenAIClient extends BaseClient { this.options.modelOptions, ); - this.isO1Model = /\bo1\b/i.test(this.modelOptions.model); - this.defaultVisionModel = this.options.visionModel ?? 'gpt-4-vision-preview'; if (typeof this.options.attachments?.then === 'function') { this.options.attachments.then((attachments) => this.checkVisionRequest(attachments)); @@ -109,6 +107,8 @@ class OpenAIClient extends BaseClient { this.checkVisionRequest(this.options.attachments); } + this.isO1Model = /\bo1\b/i.test(this.modelOptions.model); + const { OPENROUTER_API_KEY, OPENAI_FORCE_PROMPT } = process.env ?? {}; if (OPENROUTER_API_KEY && !this.azure) { this.apiKey = OPENROUTER_API_KEY; diff --git a/api/server/index.js b/api/server/index.js index 3bc0a05003..7278273600 100644 --- a/api/server/index.js +++ b/api/server/index.js @@ -112,10 +112,16 @@ const startServer = async () => { app.use('/api/tags', routes.tags); app.use((req, res) => { - // Replace lang attribute in index.html with lang from cookies or accept-language header + res.set({ + 'Cache-Control': process.env.INDEX_CACHE_CONTROL || 'no-cache, no-store, must-revalidate', + Pragma: process.env.INDEX_PRAGMA || 'no-cache', + Expires: process.env.INDEX_EXPIRES || '0', + }); + const lang = req.cookies.lang || req.headers['accept-language']?.split(',')[0] || 'en-US'; - const saneLang = lang.replace(/"/g, '"'); // sanitize untrusted user input + const saneLang = lang.replace(/"/g, '"'); const updatedIndexHtml = indexHTML.replace(/lang="en-US"/g, `lang="${saneLang}"`); + res.type('html'); res.send(updatedIndexHtml); }); diff --git a/index.html b/index.html index 36de3d1adf..d09d456ff6 100644 --- a/index.html +++ b/index.html @@ -7,10 +7,6 @@ - - - - LibreChat