From 15b0051552c838050d110703f281a711d42b8e63 Mon Sep 17 00:00:00 2001
From: Richard Petersen <richard.petersen@open-xchange.com>
Date: Mon, 14 Mar 2022 17:35:58 +0100
Subject: [PATCH] Fix: /ready might trigger warmup multiple times

---
 spec/startup_test.js | 57 ++++++++++++++++++++++++++++++++++++++++++++
 src/files.js         |  6 +++++
 2 files changed, 63 insertions(+)
 create mode 100644 spec/startup_test.js

diff --git a/spec/startup_test.js b/spec/startup_test.js
new file mode 100644
index 0000000..fee579f
--- /dev/null
+++ b/spec/startup_test.js
@@ -0,0 +1,57 @@
+import { generateSimpleViteManifest, mockApp, mockConfig, mockFetch } from './util.js'
+import { expect } from 'chai'
+import * as td from 'testdouble'
+import { Response } from 'node-fetch'
+
+describe('Service startup', function () {
+  before(async function () {
+    mockConfig({ urls: ['http://ui-server/'] })
+  })
+
+  after(function () {
+    td.reset()
+  })
+
+  it('only fetches all files once', async function () {
+    const counters = {
+      '/example.js': 0,
+      '/test.txt': 0,
+      '/index.html.js': 0,
+      '/index.html': 0
+    }
+    mockFetch({
+      'http://ui-server': {
+        '/manifest.json': generateSimpleViteManifest({
+          'example.js': { imports: ['test.txt'] },
+          'test.txt': { },
+          'index.html': {
+            file: 'index.html.js',
+            isEntry: true,
+            imports: ['example.js']
+          }
+        }),
+        '/example.js': () => {
+          counters['/example.js']++
+          return new Response('this is example', { headers: { 'content-type': 'application/javascript' } })
+        },
+        '/test.txt': () => {
+          counters['/test.txt']++
+          return new Response('this is test', { headers: { 'content-type': 'text/plain' } })
+        },
+        '/index.html.js': () => {
+          counters['/index.html.js']++
+          return new Response('this is index.html.js', { headers: { 'content-type': 'application/javascript' } })
+        },
+        '/index.html': () => {
+          counters['/index.html']++
+          return new Response('<html><head></head><body>it\'s me</body></html>', { headers: { 'content-type': 'text/html' } })
+        }
+      }
+    })
+    await mockApp()
+
+    Object.entries(counters).forEach(([key, value]) => {
+      expect(value, `${key}`).to.equal(1)
+    })
+  })
+})
diff --git a/src/files.js b/src/files.js
index e13b56d..81413a9 100644
--- a/src/files.js
+++ b/src/files.js
@@ -38,9 +38,14 @@ class FileCache {
     this._hash = ''
     this._dependencies = {}
     this._oxManifests = {}
+    this._isCaching = false
   }
 
   async warmUp (manifests, deps) {
+    if (this._hash === manifests.__hash__) return logger.debug(`Tried to warm up the filecache with the same version: ${manifests.__hash__}`)
+    if (this._isCaching) return logger.debug('Cache.warmup is already running')
+    this._isCaching = true
+
     logger.debug('beginning to warm up cache')
     const cache = Object.fromEntries(await (async function () {
       const files = Object.keys(deps)
@@ -79,6 +84,7 @@ class FileCache {
     this._hash = manifests.__hash__
     this._dependencies = deps
     this._oxManifests = viteToOxManifest(manifests)
+    this._isCaching = false
 
     logger.debug('cache warmed up')
   }
-- 
GitLab