mirror of
https://github.com/flarum/core.git
synced 2025-08-09 01:46:35 +02:00
performance(frontend): Preload FontAwesome, JS and CSS (#3057)
* Add preloads support to Document class * Add frontend extender for asset preloading * Provide default preloads for FontAwesome * Add tests for preload extender and default preloads * Apply fixes from StyleCI [ci skip] [skip ci] * Fix typo * Fix two more typos 🙃 * Preload core JS and CSS * Apply fixes from StyleCI [ci skip] [skip ci] * Reorder preloads * Remove singular preloads method * Use filesystem disk driver for getting FA font paths * Update test to use full URL * Apply fixes from StyleCI [ci skip] [skip ci] * Address review comment * Apply fixes from StyleCI [ci skip] [skip ci] * Fix typo * Apply fixes from StyleCI [ci skip] [skip ci] * Correct callback wrapping * Update src/Extend/Frontend.php Co-authored-by: Sami Mazouz <sychocouldy@gmail.com> * Update src/Extend/Frontend.php Co-authored-by: Sami Mazouz <sychocouldy@gmail.com> * Update src/Extend/Frontend.php * Fix preload extender logic * Convert base FontAwesome preloads into a Singleton * Apply fixes from StyleCI [ci skip] [skip ci] Co-authored-by: luceos <luceos@users.noreply.github.com> Co-authored-by: Sami Mazouz <sychocouldy@gmail.com> Co-authored-by: Alexander Skvortsov <38059171+askvortsov1@users.noreply.github.com> Co-authored-by: Alexander Skvortsov <sasha.skvortsov109@gmail.com>
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* For detailed copyright and license information, please view the
|
||||
* LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Tests\integration\extenders;
|
||||
|
||||
use Flarum\Extend;
|
||||
use Flarum\Testing\integration\TestCase;
|
||||
|
||||
class FrontendPreloadTest extends TestCase
|
||||
{
|
||||
private $customPreloadUrls = ['/my-preload', '/my-preload2'];
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function default_preloads_are_present()
|
||||
{
|
||||
$response = $this->send(
|
||||
$this->request('GET', '/')
|
||||
);
|
||||
|
||||
$filesystem = $this->app()->getContainer()->make('filesystem')->disk('flarum-assets');
|
||||
|
||||
$urls = [
|
||||
$filesystem->url('fonts/fa-solid-900.woff2'),
|
||||
$filesystem->url('fonts/fa-regular-400.woff2'),
|
||||
];
|
||||
|
||||
$body = $response->getBody()->getContents();
|
||||
|
||||
foreach ($urls as $url) {
|
||||
$this->assertStringContainsString("<link rel=\"preload\" href=\"$url\" as=\"font\" type=\"font/woff2\" crossorigin=\"\">", $body);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function preloads_can_be_added()
|
||||
{
|
||||
$urls = $this->customPreloadUrls;
|
||||
|
||||
$this->extend(
|
||||
(new Extend\Frontend('forum'))
|
||||
->preloads(
|
||||
array_map(function ($url) {
|
||||
return ['href' => $url];
|
||||
}, $urls)
|
||||
)
|
||||
);
|
||||
|
||||
$response = $this->send(
|
||||
$this->request('GET', '/')
|
||||
);
|
||||
$body = $response->getBody()->getContents();
|
||||
|
||||
foreach ($urls as $url) {
|
||||
$this->assertStringContainsString("<link rel=\"preload\" href=\"$url\">", $body);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function preloads_can_be_added_via_callable()
|
||||
{
|
||||
$urls = $this->customPreloadUrls;
|
||||
|
||||
$this->extend(
|
||||
(new Extend\Frontend('forum'))
|
||||
->preloads(function () use ($urls) {
|
||||
return array_map(function ($url) {
|
||||
return ['href' => $url];
|
||||
}, $urls);
|
||||
})
|
||||
);
|
||||
|
||||
$response = $this->send(
|
||||
$this->request('GET', '/')
|
||||
);
|
||||
$body = $response->getBody()->getContents();
|
||||
|
||||
foreach ($urls as $url) {
|
||||
$this->assertStringContainsString("<link rel=\"preload\" href=\"$url\">", $body);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user