Skip to content

Commit 6e51eb0

Browse files
feat: Implement static caching for Vite manifest loading.
1 parent b3be349 commit 6e51eb0

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/Support/Vite.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,7 @@ public function url(string $asset, $assetPath = '/assets/build')
3030
}
3131

3232
$manifestPath = source_path($assetPath . '/manifest.json');
33-
34-
if (! file_exists($manifestPath)) {
35-
throw new Exception('The Vite manifest does not exist. Please run `npm run build` first or start the dev server.');
36-
}
37-
38-
$manifest = json_decode(file_get_contents($manifestPath), true);
33+
$manifest = $this->loadManifest($manifestPath);
3934

4035
if (! isset($manifest[$asset])) {
4136
throw new Exception('Main entry point not found in Vite manifest.');
@@ -62,4 +57,23 @@ public function devServer()
6257

6358
return new HtmlString(sprintf('<script type="module" src="%s"></script>', "{$devServerUrl}/@vite/client"));
6459
}
60+
61+
private function loadManifest($manifestPath)
62+
{
63+
static $manifest = [];
64+
if (array_key_exists($manifestPath, $manifest)) {
65+
return $manifest[$manifestPath];
66+
}
67+
68+
return $manifest[$manifestPath] = $this->uncachedManifest($manifestPath);
69+
}
70+
71+
private function uncachedManifest($manifestPath)
72+
{
73+
if (! file_exists($manifestPath)) {
74+
throw new Exception('The Vite manifest does not exist. Please run `npm run build` first or start the dev server.');
75+
}
76+
77+
return json_decode(file_get_contents($manifestPath), true);
78+
}
6579
}

0 commit comments

Comments
 (0)