Skip to content

Commit 0d165a6

Browse files
authored
fix: add cache busting to web component extractor (#1731)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - Bug Fixes - Ensures UI assets use content-hashed filenames so browsers load the latest scripts and styles after updates, reducing stale-cache issues. - Keeps scripts and their related styles in sync for consistent rendering and fewer cache-related glitches. - Ignores non-asset manifest entries to prevent accidental inclusion of invalid items and ensure correct asset loading. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent f4f3e3c commit 0d165a6

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

plugin/source/dynamix.unraid.net/usr/local/emhttp/plugins/dynamix.my.servers/include/web-components-extractor.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ private function processManifestFiles(): string
6363

6464
// Process each entry in the manifest
6565
foreach ($manifest as $key => $entry) {
66+
if ($key === 'ts') {
67+
continue;
68+
}
6669
// Skip if not an array with a 'file' key
6770
if (!is_array($entry) || !isset($entry['file']) || empty($entry['file'])) {
6871
continue;

plugin/tests/test-extractor.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class ExtractorTest {
1414
private $passed = 0;
1515
private $failed = 0;
1616
private $verbose = false;
17+
private $standaloneJsFile = 'standalone-apps-AbCdEf12.js';
18+
private $standaloneCssFile = 'standalone-apps-ZyXwVuTs.css';
1719

1820
// Color codes for terminal output
1921
const RED = "\033[0;31m";
@@ -46,13 +48,13 @@ private function setup() {
4648

4749
// Create test manifest files
4850
file_put_contents($this->componentDir . '/standalone-apps/standalone.manifest.json', json_encode([
49-
'standalone-apps-RlN0czLV.css' => [
50-
'file' => 'standalone-apps-RlN0czLV.css',
51-
'src' => 'standalone-apps-RlN0czLV.css'
51+
$this->standaloneCssFile => [
52+
'file' => $this->standaloneCssFile,
53+
'src' => $this->standaloneCssFile
5254
],
53-
'standalone-apps.js' => [
54-
'file' => 'standalone-apps.js',
55-
'src' => 'standalone-apps.js',
55+
$this->standaloneJsFile => [
56+
'file' => $this->standaloneJsFile,
57+
'src' => $this->standaloneJsFile,
5658
'css' => ['app-styles.css', 'theme.css']
5759
],
5860
'ts' => 1234567890
@@ -144,8 +146,8 @@ private function runTests() {
144146
echo "Test: Script Tag Generation\n";
145147
echo "----------------------------\n";
146148
$this->test(
147-
"Generates script tag for standalone-apps.js",
148-
strpos($output, 'script id="unraid-standalone-apps-standalone-apps-js"') !== false
149+
"Generates script tag for hashed standalone JS",
150+
strpos($output, 'script id="unraid-standalone-apps-' . $this->sanitizeForExpectedId($this->standaloneJsFile) . '"') !== false
149151
);
150152
$this->test(
151153
"Generates script tag for components.mjs",
@@ -160,8 +162,8 @@ private function runTests() {
160162
echo "\nTest: CSS Link Generation\n";
161163
echo "--------------------------\n";
162164
$this->test(
163-
"Generates link tag for standalone CSS",
164-
strpos($output, 'link id="unraid-standalone-apps-standalone-apps-RlN0czLV-css"') !== false
165+
"Generates link tag for hashed standalone CSS",
166+
strpos($output, 'link id="unraid-standalone-apps-' . $this->sanitizeForExpectedId($this->standaloneCssFile) . '"') !== false
165167
);
166168
$this->test(
167169
"Generates link tag for UI styles",
@@ -209,7 +211,7 @@ private function runTests() {
209211
echo "------------------------\n";
210212
$this->test(
211213
"Correctly constructs standalone-apps path",
212-
strpos($output, '/plugins/dynamix.my.servers/unraid-components/standalone-apps/standalone-apps.js') !== false
214+
strpos($output, '/plugins/dynamix.my.servers/unraid-components/standalone-apps/' . $this->standaloneJsFile) !== false
213215
);
214216
$this->test(
215217
"Correctly constructs ui-components path",
@@ -274,11 +276,11 @@ private function runTests() {
274276
echo "--------------------------------\n";
275277
$this->test(
276278
"Loads CSS from JS entry css array (app-styles.css)",
277-
strpos($output, 'id="unraid-standalone-apps-standalone-apps-js-css-app-styles-css"') !== false
279+
strpos($output, 'id="unraid-standalone-apps-' . $this->sanitizeForExpectedId($this->standaloneJsFile) . '-css-app-styles-css"') !== false
278280
);
279281
$this->test(
280282
"Loads CSS from JS entry css array (theme.css)",
281-
strpos($output, 'id="unraid-standalone-apps-standalone-apps-js-css-theme-css"') !== false
283+
strpos($output, 'id="unraid-standalone-apps-' . $this->sanitizeForExpectedId($this->standaloneJsFile) . '-css-theme-css"') !== false
282284
);
283285
$this->test(
284286
"CSS from manifest has correct href path (app-styles.css)",
@@ -344,6 +346,11 @@ private function removeDirectory($dir) {
344346
}
345347
rmdir($dir);
346348
}
349+
350+
private function sanitizeForExpectedId(string $input): string
351+
{
352+
return preg_replace('/[^a-zA-Z0-9-]/', '-', $input);
353+
}
347354

348355
private function reportResults() {
349356
echo "\n";
@@ -366,4 +373,4 @@ private function reportResults() {
366373

367374
// Run tests
368375
$test = new ExtractorTest();
369-
exit($test->run());
376+
exit($test->run());

web/vite.config.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,11 @@ export default defineConfig({
128128
rollupOptions: {
129129
output: {
130130
format: 'es',
131-
entryFileNames: 'standalone-apps.js',
131+
entryFileNames: 'standalone-apps-[hash].js',
132132
chunkFileNames: '[name]-[hash].js',
133133
assetFileNames: (assetInfo) => {
134-
// Keep CSS files with predictable names
135134
if (assetInfo.name?.endsWith('.css')) {
136-
return 'standalone-apps.css';
135+
return 'standalone-apps-[hash][extname]';
137136
}
138137
return '[name]-[hash][extname]';
139138
},

0 commit comments

Comments
 (0)