Skip to content

Commit

Permalink
Better Service Worker
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Feb 6, 2024
1 parent 5e704bd commit e5c94c5
Show file tree
Hide file tree
Showing 13 changed files with 225 additions and 124 deletions.
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -483,4 +483,4 @@ parameters:
-
message: "#^Parameter \\#1 \\$string of function mb_strlen expects string, string\\|null given\\.$#"
count: 1
path: src/Subscriber/PwaDevServerSubscriber.php
path: src/Subscriber/PwaDevServerSubscriber.php
43 changes: 29 additions & 14 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,6 @@ private function setupServiceWorker(ArrayNodeDefinition $node): void
)
->example('//STANDARD_RULES_PLACEHOLDER')
->end()
->scalarNode('precaching_placeholder')
->defaultValue('//PRECACHING_PLACEHOLDER')
->info(
'The placeholder for the precaching. Will be replaced by the assets and versions.'
)
->example('//PRECACHING_PLACEHOLDER')
->end()
->scalarNode('warm_cache_placeholder')
->defaultValue('//WARM_CACHE_URLS_PLACEHOLDER')
->info('The placeholder for the warm cache. Will be replaced by the URLs.')
->example('//WARM_CACHE_URLS_PLACEHOLDER')
->end()
->scalarNode('offline_fallback_placeholder')
->defaultValue('//OFFLINE_FALLBACK_PLACEHOLDER')
->info('The placeholder for the offline fallback. Will be replaced by the URL.')
Expand All @@ -156,10 +144,37 @@ private function setupServiceWorker(ArrayNodeDefinition $node): void
->end()
->append(
$this->getUrlNode(
'offline_fallback',
'The URL of the offline fallback. If not set, the offline fallback will be disabled.'
'page_fallback',
'The URL of the offline page fallback.'
)
)
->append(
$this->getUrlNode(
'image_fallback',
'The URL of the offline image fallback.'
)
)
->append(
$this->getUrlNode(
'font_fallback',
'The URL of the offline font fallback.'
)
)
->scalarNode('image_regex')
->defaultValue('/\.(ico|png|jpe?g|gif|svg|webp|bmp)$/')
->info('The regex to match the images.')
->example('/\.(ico|png|jpe?g|gif|svg|webp|bmp)$/')
->end()
->integerNode('max_image_cache_entries')
->defaultValue(60)
->info('The maximum number of entries in the image cache.')
->example([50, 100, 200])
->end()
->integerNode('network_timeout_seconds')
->defaultValue(3)
->info('The network timeout in seconds before cache is called (for warm cache URLs only).')
->example([1, 2, 5])
->end()
->arrayNode('warm_cache_urls')
->treatNullLike([])
->treatFalseLike([])
Expand Down
17 changes: 17 additions & 0 deletions src/Dto/Asset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace SpomkyLabs\PwaBundle\Dto;

final class Asset
{
public function __construct(public string $src)
{
}

public static function create(string $data): self
{
return new self($data);
}
}
2 changes: 1 addition & 1 deletion src/Dto/Icon.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

final class Icon
{
public string $src;
public Asset $src;

/**
* @var array<int>
Expand Down
2 changes: 1 addition & 1 deletion src/Dto/Screenshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class Screenshot
{
use TranslatableTrait;

public null|string $src = null;
public Asset $src;

Check failure on line 14 in src/Dto/Screenshot.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Class SpomkyLabs\PwaBundle\Dto\Screenshot has an uninitialized property $src. Give it default value or assign it in the constructor.

public null|int $height = null;

Expand Down
2 changes: 1 addition & 1 deletion src/Dto/ServiceWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ final class ServiceWorker
{
public bool $enabled;

public string $src;
public Asset $src;

public string $dest;

Expand Down
26 changes: 18 additions & 8 deletions src/Dto/Workbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,37 @@ final class Workbox
#[SerializedName('workbox_import_placeholder')]
public string $workboxImportPlaceholder;

#[SerializedName('warm_cache_placeholder')]
public string $warmCachePlaceholder;

#[SerializedName('standard_rules_placeholder')]
public string $standardRulesPlaceholder;

#[SerializedName('precaching_placeholder')]
public string $precachingPlaceholder;

#[SerializedName('offline_fallback_placeholder')]
public string $offlineFallbackPlaceholder;

#[SerializedName('widgets_placeholder')]
public string $widgetsPlaceholder;

#[SerializedName('offline_fallback')]
public null|Url $offlineFallback = null;
#[SerializedName('page_fallback')]
public null|Url $pageFallback = null;

#[SerializedName('image_fallback')]
public null|Url $imageFallback = null;

#[SerializedName('font_fallback')]
public null|Url $fontFallback = null;

/**
* @var array<Url>
*/
#[SerializedName('warm_cache_urls')]
public array $warmCacheUrls = [];
#[SerializedName('network_timeout_seconds')]
public int $networkTimeoutSeconds = 3;
#[SerializedName('max_image_cache_entries')]
public int $maxImageCacheEntries = 60;

#[SerializedName('image_regex')]
public string $imageRegex = '/\.(ico|png|jpe?g|gif|svg|webp|bmp)$/';

#[SerializedName('static_regex')]
public string $staticRegex = '/\.(css|js|json|xml|txt|woff2|ttf|eot|otf|map|webmanifest)$/';
}
66 changes: 66 additions & 0 deletions src/Normalizer/AssetNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace SpomkyLabs\PwaBundle\Normalizer;

use SpomkyLabs\PwaBundle\Dto\Asset;
use Symfony\Component\AssetMapper\AssetMapperInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use function assert;
use function is_string;

final readonly class AssetNormalizer implements NormalizerInterface, DenormalizerInterface
{
public function __construct(
private AssetMapperInterface $assetMapper,
) {
}

/**
* @return array{src: string, sizes?: string, form_factor?: string, label?: string, platform?: string, format?: string}
*/
public function normalize(mixed $object, string $format = null, array $context = []): string

Check failure on line 24 in src/Normalizer/AssetNormalizer.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Method SpomkyLabs\PwaBundle\Normalizer\AssetNormalizer::normalize() has parameter $context with no value type specified in iterable type array.

Check failure on line 24 in src/Normalizer/AssetNormalizer.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

PHPDoc tag @return with type array<string, string> is incompatible with native type string.
{
assert($object instanceof Asset);
$url = null;
dump($object);
if (! str_starts_with($object->src, '/')) {
$asset = $this->assetMapper->getAsset($object->src);
$url = $asset?->publicPath;
}
if ($url === null) {
$url = $object->src;
}

return $url;
}

public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed

Check failure on line 40 in src/Normalizer/AssetNormalizer.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Method SpomkyLabs\PwaBundle\Normalizer\AssetNormalizer::denormalize() has parameter $context with no value type specified in iterable type array.
{
assert(is_string($data));

return Asset::create($data);
}

public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool

Check failure on line 47 in src/Normalizer/AssetNormalizer.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Method SpomkyLabs\PwaBundle\Normalizer\AssetNormalizer::supportsNormalization() has parameter $context with no value type specified in iterable type array.
{
return $data instanceof Asset;
}

public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool

Check failure on line 52 in src/Normalizer/AssetNormalizer.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Method SpomkyLabs\PwaBundle\Normalizer\AssetNormalizer::supportsDenormalization() has parameter $context with no value type specified in iterable type array.
{
return $type === Asset::class;
}

/**
* @return array<class-string, bool>
*/
public function getSupportedTypes(?string $format): array
{
return [
Asset::class => true,
];
}
}
15 changes: 5 additions & 10 deletions src/Normalizer/IconNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,14 @@ public function __construct(
public function normalize(mixed $object, string $format = null, array $context = []): array
{
assert($object instanceof Icon);
$url = null;
$asset = null;
if (! str_starts_with($object->src, '/')) {
$asset = $this->assetMapper->getAsset($object->src);
$url = $asset?->publicPath;
$format = null;
if (! str_starts_with($object->src->src, '/')) {
$asset = $this->assetMapper->getAsset($object->src->src);
$format = $this->getFormat($object, $asset);
}
if ($url === null) {
$url = $object->src;
}
$format = $this->getFormat($object, $asset);

$result = [
'src' => $url,
'src' => $object->src,
'sizes' => $object->getSizeList(),
'type' => $format,
'purpose' => $object->purpose,
Expand Down
8 changes: 4 additions & 4 deletions src/Normalizer/ScreenshotNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ public function normalize(mixed $object, string $format = null, array $context =
assert($object instanceof Screenshot);
$url = null;
$asset = null;
if (! str_starts_with($object->src, '/')) {
$asset = $this->assetMapper->getAsset($object->src);
if (! str_starts_with($object->src->src, '/')) {
$asset = $this->assetMapper->getAsset($object->src->src);
$url = $asset?->publicPath;
$format = $this->getFormat($object, $asset);
}
if ($url === null) {
$url = $object->src;
$url = $object->src->src;
}
['sizes' => $sizes, 'formFactor' => $formFactor] = $this->getSizes($object, $asset);
$format = $this->getFormat($object, $asset);

$result = [
'src' => $url,
Expand Down
2 changes: 0 additions & 2 deletions src/Resources/sw-skeleton.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// *** Workbox Bundle rules ***
//WORKBOX_IMPORT_PLACEHOLDER
//STANDARD_RULES_PLACEHOLDER
//PRECACHING_PLACEHOLDER
//WARM_CACHE_URLS_PLACEHOLDER
//OFFLINE_FALLBACK_PLACEHOLDER
//WIDGETS_PLACEHOLDER
Loading

0 comments on commit e5c94c5

Please sign in to comment.