From 75b5f823043990ef9ddd4a120d420552fd00f0c5 Mon Sep 17 00:00:00 2001
From: Sam Light <samlight1994@gmail.com>
Date: Sun, 16 Jun 2024 21:38:06 +0100
Subject: Create a version hash and add to svg url

---
 src/SvgCollection.php | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

(limited to 'src/SvgCollection.php')

diff --git a/src/SvgCollection.php b/src/SvgCollection.php
index 63d7508..fdf0664 100644
--- a/src/SvgCollection.php
+++ b/src/SvgCollection.php
@@ -27,8 +27,13 @@ class SvgCollection
 
     public function getSvgUrl(string $svg): string
     {
-        $this->getState()->addSvg($svg);
-        return route('laralight-svg.serve-svg', $this->getName()) . "#{$svg}";
+        $state = $this->getState();
+        $state->addSvg($svg);
+        $hash = $state->getHash();
+        return route(
+            'laralight-svg.serve-svg',
+            ['collection' => $this->getName(), 'v' => $hash]
+        ) . "#{$svg}";
     }
 
     protected function getState(): SvgCollectionState
@@ -70,11 +75,15 @@ class SvgCollection
             if ($file !== null && $compiler->addSvg($pendingSvg, $file)) {
                 $added[] = $pendingSvg;
             }
+            else {
+                \Log::error("Failed to add {$pendingSvg} to svg sprite");
+            }
         }
 
         $state->clearPending(false);
         $state->concatExisting($added, false);
         $state->setFullSvg($compiler->getSvg(), false);
+        $state->updateHash();
         $state->push();
     }
 
@@ -87,4 +96,9 @@ class SvgCollection
         return null;
     }
 
+    public function clearState(): void
+    {
+        $this->getState()->clear();
+    }
+
 }
-- 
cgit v1.2.3