diff --git a/lib/private/Template/SCSSCacher.php b/lib/private/Template/SCSSCacher.php
index 225078ff6a41e3722499d48bda11581b24d3c9f8..1c6ca661839c29ea0b7a7ec935b9bc486e135de0 100644
--- a/lib/private/Template/SCSSCacher.php
+++ b/lib/private/Template/SCSSCacher.php
@@ -283,8 +283,9 @@ class SCSSCacher {
 	 * @return bool
 	 */
 	private function variablesChanged(): bool {
-		$injectedVariables = $this->getInjectedVariables();
-		if ($this->config->getAppValue('core', 'theming.variables') !== md5($injectedVariables)) {
+		$cachedVariables = $this->config->getAppValue('core', 'theming.variables', '');
+		$injectedVariables = $this->getInjectedVariables($cachedVariables);
+		if ($cachedVariables !== md5($injectedVariables)) {
 			$this->logger->debug('SCSSCacher::variablesChanged storedVariables: ' . json_encode($this->config->getAppValue('core', 'theming.variables')) . ' currentInjectedVariables: ' . json_encode($injectedVariables), ['app' => 'scss_cacher']);
 			$this->config->setAppValue('core', 'theming.variables', md5($injectedVariables));
 			$this->resetCache();
@@ -411,7 +412,7 @@ class SCSSCacher {
 	/**
 	 * @return string SCSS code for variables from OC_Defaults
 	 */
-	private function getInjectedVariables(): string {
+	private function getInjectedVariables(string $cache = ''): string {
 		if ($this->injectedVariables !== null) {
 			return $this->injectedVariables;
 		}
@@ -420,6 +421,15 @@ class SCSSCacher {
 			$variables .= '$' . $key . ': ' . $value . ' !default;';
 		}
 
+		/*
+		 * If we are trying to return the same variables as that are cached
+		 * Then there is no need to do the compile step
+		 */
+		if ($cache === md5($variables)) {
+			$this->injectedVariables = $variables;
+			return $variables;
+		}
+
 		// check for valid variables / otherwise fall back to defaults
 		try {
 			$scss = new Compiler();
diff --git a/tests/lib/Template/SCSSCacherTest.php b/tests/lib/Template/SCSSCacherTest.php
index 77a9bdde764ea96b84bb03f2a2e00f0e6d4eecd2..4295c800d027d9ff252bc5721a68e095b3a1c8e5 100644
--- a/tests/lib/Template/SCSSCacherTest.php
+++ b/tests/lib/Template/SCSSCacherTest.php
@@ -81,6 +81,11 @@ class SCSSCacherTest extends \Test\TestCase {
 			->willReturn('http://localhost/nextcloud');
 
 		$this->config = $this->createMock(IConfig::class);
+		$this->config->expects($this->any())
+			->method('getAppValue')
+			->will($this->returnCallback(function ($appId, $configKey, $defaultValue) {
+				return $defaultValue;
+			}));
 		$this->cacheFactory = $this->createMock(ICacheFactory::class);
 		$this->depsCache = $this->createMock(ICache::class);
 		$this->cacheFactory->expects($this->at(0))