From 88a06a2e90e7e2911a4c038f958f7c8cc76e8f56 Mon Sep 17 00:00:00 2001
From: Richard Petersen <richard.petersen@open-xchange.com>
Date: Thu, 11 Nov 2021 14:20:21 +0100
Subject: [PATCH] Only add css dependencies for files in the header

This prevents unnesessary large headers as the UI will filter for CSS files only
---
 spec/headers_test.js | 2 +-
 src/createApp.js     | 8 +++-----
 src/manifests.js     | 6 ++++++
 3 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/spec/headers_test.js b/spec/headers_test.js
index efe7644..44eb2ed 100644
--- a/spec/headers_test.js
+++ b/spec/headers_test.js
@@ -58,6 +58,6 @@ describe('Responses contain custom headers', () => {
   it('javascript file contains dependencies', async () => {
     const response = await request(app).get('/index.html.js')
     expect(response.statusCode).toBe(200)
-    expect(response.headers.dependencies).toEqual('example.js,main.css')
+    expect(response.headers.dependencies).toEqual('main.css')
   })
 })
diff --git a/src/createApp.js b/src/createApp.js
index 4406610..0bd24a8 100644
--- a/src/createApp.js
+++ b/src/createApp.js
@@ -18,7 +18,7 @@ import promBundle from 'express-prom-bundle'
 import swaggerUi from 'swagger-ui-express'
 import yaml from 'js-yaml'
 import fs from 'fs'
-import { getDependencies, getOxManifests, getVersion } from './manifests.js'
+import { getCSSDependenciesFor, getDependencies, getOxManifests, getVersion } from './manifests.js'
 import { fileCache } from './files.js'
 
 const ignorePaths = ['/ready', '/healthy']
@@ -93,8 +93,7 @@ export function createApp () {
   app.use(async (req, res, next) => {
     const { 'content-type': contentType, content } = fileCache.get(req.path)
     if (content) {
-      const allDependencies = await getDependencies()
-      const dependencies = allDependencies[req.path.substr(1)] || []
+      const dependencies = await getCSSDependenciesFor(req.path.substr(1))
       return res
         .setHeader('content-type', contentType)
         .setHeader('dependencies', dependencies.join(','))
@@ -107,8 +106,7 @@ export function createApp () {
     try {
       const { 'content-type': contentType, content } = await fileCache.fetchAndStore(req.path)
       if (content) {
-        const allDependencies = await getDependencies()
-        const dependencies = allDependencies[req.path.substr(1)] || []
+        const dependencies = await getCSSDependenciesFor(req.path.substr(1))
         return res
           .setHeader('content-type', contentType)
           .setHeader('dependencies', dependencies.join(','))
diff --git a/src/manifests.js b/src/manifests.js
index 3660508..3a9fda7 100644
--- a/src/manifests.js
+++ b/src/manifests.js
@@ -106,6 +106,12 @@ export const getDependencies = (() => {
   }
 })()
 
+export async function getCSSDependenciesFor (file) {
+  const allDependencies = await getDependencies()
+  const dependencies = allDependencies[file] || []
+  return dependencies.filter(dep => /\.css/i.test(dep))
+}
+
 export const getVersion = (() => {
   let prevViteManifest
   let version
-- 
GitLab