diff --git a/src/Command/GenerateIconsCommand.php b/src/Command/GenerateIconsCommand.php index e2ec3c4..cd27393 100644 --- a/src/Command/GenerateIconsCommand.php +++ b/src/Command/GenerateIconsCommand.php @@ -16,7 +16,7 @@ use Symfony\Component\Mime\MimeTypes; use function count; -#[AsCommand(name: 'pwa:generate-icons', description: 'Generate icons for your PWA')] +#[AsCommand(name: 'pwa:create:icons', description: 'Generate icons for your PWA')] final class GenerateIconsCommand extends Command { public function __construct( diff --git a/src/Command/GenerateServiceWorkerCommand.php b/src/Command/GenerateServiceWorkerCommand.php index 13e3643..09d9d76 100644 --- a/src/Command/GenerateServiceWorkerCommand.php +++ b/src/Command/GenerateServiceWorkerCommand.php @@ -15,7 +15,7 @@ use Symfony\Component\HttpKernel\Config\FileLocator; use function count; -#[AsCommand(name: 'pwa:sw', description: 'Generate a basic Service Worker')] +#[AsCommand(name: 'pwa:create:sw', description: 'Generate a basic Service Worker')] final class GenerateServiceWorkerCommand extends Command { public function __construct( diff --git a/src/Command/TakeScreenshotCommand.php b/src/Command/TakeScreenshotCommand.php index 6ba252f..db5700b 100644 --- a/src/Command/TakeScreenshotCommand.php +++ b/src/Command/TakeScreenshotCommand.php @@ -19,7 +19,7 @@ use function count; #[AsCommand( - name: 'pwa:take-screenshot', + name: 'pwa:create:screenshot', description: 'Take a screenshot of the application store it in your asset folder' )] final class TakeScreenshotCommand extends Command diff --git a/src/Resources/config/services.php b/src/Resources/config/services.php index 2d6d52a..b927072 100644 --- a/src/Resources/config/services.php +++ b/src/Resources/config/services.php @@ -5,7 +5,7 @@ use SpomkyLabs\PwaBundle\Dto\Manifest; use SpomkyLabs\PwaBundle\ImageProcessor\GDImageProcessor; use SpomkyLabs\PwaBundle\ImageProcessor\ImagickImageProcessor; -use SpomkyLabs\PwaBundle\Service\Builder; +use SpomkyLabs\PwaBundle\Service\ManifestBuilder; use SpomkyLabs\PwaBundle\Service\ServiceWorkerBuilder; use SpomkyLabs\PwaBundle\Subscriber\AssetsCompileEventListener; use SpomkyLabs\PwaBundle\Subscriber\PwaDevServerSubscriber; @@ -23,13 +23,13 @@ ->autowire() ; - $container->set(Builder::class) + $container->set(ManifestBuilder::class) ->args([ '$config' => '%spomky_labs_pwa.config%', ]) ; $container->set(Manifest::class) - ->factory([service(Builder::class), 'createManifest']) + ->factory([service(ManifestBuilder::class), 'createManifest']) ; $container->load('SpomkyLabs\\PwaBundle\\Command\\', '../../Command/*'); diff --git a/src/Resources/workbox.js b/src/Resources/workbox.js index e154ffb..e5c5227 100644 --- a/src/Resources/workbox.js +++ b/src/Resources/workbox.js @@ -5,7 +5,6 @@ importScripts( // *** Recipes *** // You are free to change or remove any of these presets as you wish. // See https://developer.chrome.com/docs/workbox/modules/workbox-recipes for more information. - const { pageCache, imageCache, @@ -13,14 +12,10 @@ const { googleFontsCache, } = workbox.recipes; -// => Cache pages with a network-first strategy. -pageCache(); -// => Cache CSS, JS, and Web Worker requests with a cache-first strategy. -staticResourceCache(); -// => Cache images with a cache-first strategy. -imageCache(); -// => Cache the underlying font files with a cache-first strategy. -googleFontsCache(); +pageCache();// => Cache pages with a network-first strategy. +staticResourceCache();// => Cache CSS, JS, and Web Worker requests with a cache-first strategy. +imageCache();// => Cache images with a cache-first strategy. +googleFontsCache();// => Cache the underlying font files with a cache-first strategy. // *** Bundle rules *** //PRECACHING_PLACEHOLDER diff --git a/src/Service/Builder.php b/src/Service/ManifestBuilder.php similarity index 96% rename from src/Service/Builder.php rename to src/Service/ManifestBuilder.php index 8496532..10b11ae 100644 --- a/src/Service/Builder.php +++ b/src/Service/ManifestBuilder.php @@ -8,7 +8,7 @@ use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; use function assert; -final class Builder +final class ManifestBuilder { private null|Manifest $manifest = null; diff --git a/tests/Functional/ManifestFileDevServerTest.php b/tests/Functional/DevServerTest.php similarity index 52% rename from tests/Functional/ManifestFileDevServerTest.php rename to tests/Functional/DevServerTest.php index bdf492b..af86ad5 100644 --- a/tests/Functional/ManifestFileDevServerTest.php +++ b/tests/Functional/DevServerTest.php @@ -10,10 +10,10 @@ /** * @internal */ -final class ManifestFileDevServerTest extends WebTestCase +final class DevServerTest extends WebTestCase { #[Test] - public static function aScreenshotIsCorrectlyTake(): void + public static function theManifestIsServed(): void { // Given $client = static::createClient(); @@ -24,6 +24,19 @@ public static function aScreenshotIsCorrectlyTake(): void // Then static::assertResponseIsSuccessful(); static::assertResponseHeaderSame('Content-Type', 'application/manifest+json'); - dump($client->getResponse()->getContent()); + } + + #[Test] + public static function theServiceWorkerIsServed(): void + { + // Given + $client = static::createClient(); + + // When + $client->request('GET', '/sw.js'); + + // Then + static::assertResponseIsSuccessful(); + static::assertResponseHeaderSame('Content-Type', 'application/javascript'); } } diff --git a/tests/Functional/GenerateIconsCommandTest.php b/tests/Functional/GenerateIconsCommandTest.php index 59e3f84..9a0dd56 100644 --- a/tests/Functional/GenerateIconsCommandTest.php +++ b/tests/Functional/GenerateIconsCommandTest.php @@ -13,10 +13,10 @@ final class GenerateIconsCommandTest extends AbstractPwaTestCase { #[Test] - public static function aScreenshotIsCorrectlyTake(): void + public static function iconsAreCorrectlyCreated(): void { // Given - $command = self::$application->find('pwa:generate-icons'); + $command = self::$application->find('pwa:create:icons'); $commandTester = new CommandTester($command); $output = sprintf('%s/samples/icons', self::$kernel->getCacheDir()); diff --git a/tests/Functional/ServiceWorkerCommandTest.php b/tests/Functional/ServiceWorkerCommandTest.php index 4100caa..a090c93 100644 --- a/tests/Functional/ServiceWorkerCommandTest.php +++ b/tests/Functional/ServiceWorkerCommandTest.php @@ -16,7 +16,7 @@ final class ServiceWorkerCommandTest extends AbstractPwaTestCase public static function theCommandCanGenerateTheServiceWorker(): void { // Given - $command = self::$application->find('pwa:sw'); + $command = self::$application->find('pwa:create:sw'); $commandTester = new CommandTester($command); $output = sprintf('%s/samples/my-sw.js', self::$kernel->getCacheDir()); diff --git a/tests/Functional/TakeScreenshotCommandTest.php b/tests/Functional/TakeScreenshotCommandTest.php index 7569418..5655e42 100644 --- a/tests/Functional/TakeScreenshotCommandTest.php +++ b/tests/Functional/TakeScreenshotCommandTest.php @@ -16,7 +16,7 @@ final class TakeScreenshotCommandTest extends AbstractPwaTestCase public static function aScreenshotIsCorrectlyTake(): void { // Given - $command = self::$application->find('pwa:take-screenshot'); + $command = self::$application->find('pwa:create:screenshot'); $commandTester = new CommandTester($command); $output = sprintf('%s/samples/screenshots/', self::$kernel->getCacheDir()); diff --git a/tests/config.php b/tests/config.php index cc969ee..400a589 100644 --- a/tests/config.php +++ b/tests/config.php @@ -213,7 +213,7 @@ ], 'handle_links' => 'auto', 'serviceworker' => [ - 'src' => '/sw/my-sw.js', + 'src' => __DIR__ . '/sw.js', 'scope' => '/', 'use_cache' => true, ], diff --git a/tests/sw.js b/tests/sw.js new file mode 100644 index 0000000..e5c5227 --- /dev/null +++ b/tests/sw.js @@ -0,0 +1,24 @@ +importScripts( + 'https://storage.googleapis.com/workbox-cdn/releases/7.0.0/workbox-sw.js' +); + +// *** Recipes *** +// You are free to change or remove any of these presets as you wish. +// See https://developer.chrome.com/docs/workbox/modules/workbox-recipes for more information. +const { + pageCache, + imageCache, + staticResourceCache, + googleFontsCache, +} = workbox.recipes; + +pageCache();// => Cache pages with a network-first strategy. +staticResourceCache();// => Cache CSS, JS, and Web Worker requests with a cache-first strategy. +imageCache();// => Cache images with a cache-first strategy. +googleFontsCache();// => Cache the underlying font files with a cache-first strategy. + +// *** Bundle rules *** +//PRECACHING_PLACEHOLDER +//WARM_CACHE_URLS_PLACEHOLDER +//OFFLINE_FALLBACK_PLACEHOLDER +//WIDGETS_PLACEHOLDER