diff --git a/spec/file-depencies_test.js b/spec/file-depencies_test.js index ed85b7a10e16d0a5f30b90104d03a61eb49de9d2..b65d4d832ef44e18dca211e888f3a5401d61273b 100644 --- a/spec/file-depencies_test.js +++ b/spec/file-depencies_test.js @@ -7,11 +7,12 @@ import RedisMock from 'ioredis-mock' describe('JS files with dependencies contain events', function () { let app + let mockFetchConfig - before(async function () { + beforeEach(async function () { mockConfig({ urls: ['http://ui-server/'] }) mockRedis() - mockFetch({ + mockFetch(mockFetchConfig = { 'http://ui-server': { '/manifest.json': generateSimpleViteManifest({ 'example.js': {}, @@ -34,9 +35,6 @@ describe('JS files with dependencies contain events', function () { afterEach(async function () { await new RedisMock().flushdb() - }) - - after(function () { td.reset() }) @@ -46,4 +44,34 @@ describe('JS files with dependencies contain events', function () { expect(response.headers.dependencies).to.equal('main.css') expect(response.text).to.equal('console.log("this is index.html.js")\n/*injected by ui-middleware*/document.dispatchEvent(new CustomEvent("load-css",{detail:{css:["main.css"]}}))') }) + + it('javascript files from different versions have corrent dependencies', async function () { + const r1 = await request(app).get('/index.html.js') + expect(r1.headers.dependencies).to.equal('main.css') + + mockFetchConfig['http://ui-server']['/manifest.json'] = generateSimpleViteManifest({ + 'example.js': {}, + 'other.css': {}, + 'index.html': { + file: 'index.html.js', + isEntry: true, + imports: ['example.js'], + css: ['other.css'] + } + }) + + await import('../src/version.js').then(({ updateVersionProcessor }) => updateVersionProcessor()) + + const r2 = await request(app).get('/index.html.js') + expect(r2.headers.dependencies).to.equal('other.css') + + const r3 = await request(app).get('/index.html.js').set('version', r1.headers.version) + expect(r3.headers.dependencies).to.equal('main.css') + + const r4 = await request(app).get('/index.html.js').set('version', r2.headers.version) + expect(r4.headers.dependencies).to.equal('other.css') + + const r5 = await request(app).get('/index.html.js') + expect(r5.headers.dependencies).to.equal('other.css') + }) }) diff --git a/src/cache.js b/src/cache.js index d32396e8634418c1a5c716b9de6ea7f71f819347..4da5246161acc9e2e71a4c5a00ad300e0ffeb853 100644 --- a/src/cache.js +++ b/src/cache.js @@ -20,7 +20,7 @@ export async function clear () { export function get (key, fallback) { if (cache[key]) { - logger.debug(`[Cache] Resolve "${key}" from memory`) + logger.debug(`[Cache] Resolve from memory: ${key}`) return cache[key] } @@ -28,7 +28,7 @@ export function get (key, fallback) { if (redis.isEnabled()) { let result = await redis.client.get(key) if (result) { - logger.debug(`[Cache] Resolve "${key}" from redis`) + logger.debug(`[Cache] Resolve from redis: ${key}`) result = JSON.parse(result) cache[key] = result return result @@ -39,7 +39,7 @@ export function get (key, fallback) { const fallbackResult = await fallback() if (fallbackResult) { - logger.debug(`[Cache] Found a fallback for "${key}"`) + logger.debug(`[Cache] Found a getter for: ${key}`) cache[key] = fallbackResult if (redis.isEnabled()) redis.client.set(key, JSON.stringify(fallbackResult)) } diff --git a/src/files.js b/src/files.js index ff0f20fb060a5cd2fa7cadd21d745c81a2faaa69..3404edd79a572c0cf03cd4b34105e4bec0635980 100644 --- a/src/files.js +++ b/src/files.js @@ -61,7 +61,10 @@ export function getFile ({ version, path }) { // try to get the file synchronously. const data = cache.getCache()[key] - if (data) return data + if (data) { + logger.debug(`[Files] Resolve from memory: ${path}`) + return data + } // if synchronously does not work, store the async promise for further requests const promise = (async () => { @@ -75,18 +78,21 @@ export function getFile ({ version, path }) { ]) if (body) { + logger.debug(`[Files] Resolve from redis: ${path}`) return (cache.getCache()[key] = { body, ...JSON.parse(meta) }) } } const dataFromServer = await fetchFileWithHeaders({ version, path }) if (redis.isEnabled()) { + logger.debug(`[Files] Store in redis: ${path}`) const { body, ...rest } = dataFromServer redis.client.set(bodyKey, createWritable(body)) redis.client.set(metaKey, JSON.stringify(rest)) } // overwrite cache with synchronous data + logger.debug(`[Files] Store in memory: ${path}`) return (cache.getCache()[key] = dataFromServer) })()