diff --git a/spec/startup_test.js b/spec/startup_test.js new file mode 100644 index 0000000000000000000000000000000000000000..fee579f5f242566353ab6a74ebf851dea6b1917d --- /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 e13b56d0d8d18871e8a92c67378fa891a4e5ce8a..81413a959aac47d18bf146a8818c56c094b24966 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') }