Skip to content

Commit b125376

Browse files
fix: Serve icons sprite from Panel route
Co-authored-by: Nico Hoffmann <me@nhoffmann.com>
1 parent 8481659 commit b125376

4 files changed

Lines changed: 16 additions & 77 deletions

File tree

src/Panel/Assets.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,6 @@ public function favicons(): array
190190
*/
191191
public function icons(): string
192192
{
193-
// the version hash busts the browser cache for every release;
194-
// in dev mode, the sprite changes without a release, so the
195-
// modification time of the source file is used instead
196193
$version = $this->isDev === true
197194
? F::modified($this->iconsRoot())
198195
: $this->kirby->versionHash();
@@ -221,7 +218,6 @@ public function importMaps(): array
221218

222219
/**
223220
* Whether the Panel is running in dev mode
224-
* and the assets are served by the Vite dev server
225221
* @since 6.0.0
226222
*/
227223
public function isDev(): bool

src/Panel/Router.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,13 @@ protected function garbage(): void
113113
}
114114

115115
/**
116-
* Response for the SVG icon sprite, which is referenced
117-
* by all `<use>` elements in the Panel
116+
* Response for the SVG icon sprite
118117
* @since 6.0.0
119118
*/
120119
public function icons(): Response
121120
{
122121
$assets = $this->panel->assets();
123-
124-
// the version hash in the URL only changes with a new release,
125-
// so the sprite can be cached forever; the dev mode URL is busted
126-
// by the modification time, which is only accurate to the second
127-
$cache = $assets->isDev() === true
122+
$cache = $assets->isDev() === true
128123
? 'no-store'
129124
: 'public, max-age=31536000, immutable';
130125

tests/Panel/AssetsTest.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,6 @@ public function testIconsInDevMode(): void
316316
{
317317
$this->setDevMode();
318318

319-
// the sprite is never served by Vite, as `<use>` references
320-
// are limited to the same origin as the Panel document
321319
$assets = new Assets();
322320
$modified = F::modified($this->app->root('panel') . '/public/img/icons.svg');
323321

@@ -327,17 +325,6 @@ public function testIconsInDevMode(): void
327325
);
328326
}
329327

330-
public function testIconsInDevModeAfterProduction(): void
331-
{
332-
$production = (new Assets())->icons();
333-
334-
$this->setDevMode();
335-
336-
// the dev URL must differ from the production URL, which the
337-
// browser has cached as immutable and would not request again
338-
$this->assertNotSame($production, (new Assets())->icons());
339-
}
340-
341328
public function testIconsWithCustomMediaUrl(): void
342329
{
343330
$this->app = $this->app->clone([
@@ -407,9 +394,7 @@ public function testImportMaps(): void
407394
public function testIsDev(): void
408395
{
409396
$this->assertFalse((new Assets())->isDev());
410-
411397
$this->setDevMode();
412-
413398
$this->assertTrue((new Assets())->isDev());
414399
}
415400

tests/Panel/RouterTest.php

Lines changed: 14 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,16 @@
22

33
namespace Kirby\Panel;
44

5-
use Kirby\Cms\Url;
65
use Kirby\Exception\Exception;
76
use Kirby\Exception\NotFoundException;
87
use Kirby\Filesystem\F;
98
use Kirby\Http\Response;
10-
use Kirby\Toolkit\Str;
119
use PHPUnit\Framework\Attributes\CoversClass;
1210

1311
#[CoversClass(Router::class)]
1412
class RouterTest extends TestCase
1513
{
16-
public const string TMP = KIRBY_TMP_DIR . '/Panel.Panel';
17-
public const string VITE_RUNNING_PATH = KIRBY_DIR . '/panel/.vite-running';
18-
19-
protected bool $hadViteRunning;
20-
21-
protected function setUp(): void
22-
{
23-
parent::setUp();
24-
25-
// initialize development mode to a known state
26-
$this->hadViteRunning = is_file(static::VITE_RUNNING_PATH);
27-
F::remove(static::VITE_RUNNING_PATH);
28-
}
29-
30-
protected function tearDown(): void
31-
{
32-
parent::tearDown();
33-
34-
// reset development mode
35-
if ($this->hadViteRunning === true) {
36-
touch(static::VITE_RUNNING_PATH);
37-
} else {
38-
F::remove(static::VITE_RUNNING_PATH);
39-
}
40-
}
41-
42-
public function testCallIcons(): void
43-
{
44-
$router = new Router($this->app->panel());
45-
$response = $router->call('assets/' . $this->app->versionHash() . '/icons.svg');
46-
47-
// the sprite is served before any area route can catch the path
48-
$this->assertSame(200, $response->code());
49-
$this->assertSame('image/svg+xml', $response->type());
50-
$this->assertStringContainsString('id="icon-accessibility"', $response->body());
51-
}
14+
public const string TMP = KIRBY_TMP_DIR . '/Panel.Panel';
5215

5316
public function testIcons(): void
5417
{
@@ -60,32 +23,33 @@ public function testIcons(): void
6023
'public, max-age=31536000, immutable',
6124
$response->header('Cache-Control')
6225
);
63-
$this->assertStringContainsString('id="icon-accessibility"', $response->body());
26+
$this->assertStringContainsString('<svg', $response->body());
6427
}
6528

6629
public function testIconsInDevMode(): void
6730
{
31+
// fake a panel dir with a running Vite dev server
32+
$panel = static::TMP . '/panel';
33+
F::write($panel . '/public/img/icons.svg', '<svg></svg>');
34+
F::write($panel . '/.vite-running', '');
35+
6836
$this->app = $this->app->clone([
37+
'roots' => [
38+
'panel' => $panel
39+
],
6940
'options' => [
7041
'panel' => [
7142
'dev' => true
7243
]
7344
]
7445
]);
7546

76-
touch(static::VITE_RUNNING_PATH);
77-
78-
$panel = $this->app->panel();
79-
$path = Str::after(Url::path($panel->assets()->icons()), 'panel/');
80-
81-
// the modification time in the dev URL must resolve to the same route
82-
$response = $panel->router()->call($path);
83-
84-
$this->assertSame(200, $response->code());
85-
$this->assertStringContainsString('id="icon-accessibility"', $response->body());
47+
$router = new Router($this->app->panel());
48+
$response = $router->icons();
8649

87-
// the sprite must not be cached while it is still being edited
50+
$this->assertSame('image/svg+xml', $response->type());
8851
$this->assertSame('no-store', $response->header('Cache-Control'));
52+
$this->assertStringContainsString('<svg', $response->body());
8953
}
9054

9155
public function testResponse(): void
@@ -147,7 +111,6 @@ public function testRoutes(): void
147111
$routes = $router->routes($areas);
148112

149113
$this->assertSame('assets/(:any)/icons.svg', $routes[0]['pattern']);
150-
$this->assertFalse($routes[0]['auth']);
151114
$this->assertSame('browser', $routes[1]['pattern']);
152115
$this->assertSame(['/', 'installation', 'login'], $routes[2]['pattern']);
153116
$this->assertSame('(:all)', $routes[3]['pattern']);

0 commit comments

Comments
 (0)