Skip to content

Commit

Permalink
Serve Workbox locally or CDN
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Jan 27, 2024
1 parent e492176 commit 16b8a0a
Show file tree
Hide file tree
Showing 77 changed files with 12,571 additions and 158 deletions.
67 changes: 51 additions & 16 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -235,28 +235,13 @@ parameters:
count: 1
path: src/Dto/ServiceWorker.php

-
message: "#^Class SpomkyLabs\\\\PwaBundle\\\\Dto\\\\ServiceWorker has an uninitialized property \\$offlineFallbackPlaceholder\\. Give it default value or assign it in the constructor\\.$#"
count: 1
path: src/Dto/ServiceWorker.php

-
message: "#^Class SpomkyLabs\\\\PwaBundle\\\\Dto\\\\ServiceWorker has an uninitialized property \\$precachingPlaceholder\\. Give it default value or assign it in the constructor\\.$#"
count: 1
path: src/Dto/ServiceWorker.php

-
message: "#^Class SpomkyLabs\\\\PwaBundle\\\\Dto\\\\ServiceWorker has an uninitialized property \\$src\\. Give it default value or assign it in the constructor\\.$#"
count: 1
path: src/Dto/ServiceWorker.php

-
message: "#^Class SpomkyLabs\\\\PwaBundle\\\\Dto\\\\ServiceWorker has an uninitialized property \\$warmCachePlaceholder\\. Give it default value or assign it in the constructor\\.$#"
count: 1
path: src/Dto/ServiceWorker.php

-
message: "#^Class SpomkyLabs\\\\PwaBundle\\\\Dto\\\\ServiceWorker has an uninitialized property \\$widgetsPlaceholder\\. Give it default value or assign it in the constructor\\.$#"
message: "#^Class SpomkyLabs\\\\PwaBundle\\\\Dto\\\\ServiceWorker has an uninitialized property \\$workbox\\. Give it default value or assign it in the constructor\\.$#"
count: 1
path: src/Dto/ServiceWorker.php

Expand Down Expand Up @@ -330,6 +315,51 @@ parameters:
count: 1
path: src/Dto/Widget.php

-
message: "#^Class SpomkyLabs\\\\PwaBundle\\\\Dto\\\\Workbox has an uninitialized property \\$enabled\\. Give it default value or assign it in the constructor\\.$#"
count: 1
path: src/Dto/Workbox.php

-
message: "#^Class SpomkyLabs\\\\PwaBundle\\\\Dto\\\\Workbox has an uninitialized property \\$offlineFallbackPlaceholder\\. Give it default value or assign it in the constructor\\.$#"
count: 1
path: src/Dto/Workbox.php

-
message: "#^Class SpomkyLabs\\\\PwaBundle\\\\Dto\\\\Workbox has an uninitialized property \\$precachingPlaceholder\\. Give it default value or assign it in the constructor\\.$#"
count: 1
path: src/Dto/Workbox.php

-
message: "#^Class SpomkyLabs\\\\PwaBundle\\\\Dto\\\\Workbox has an uninitialized property \\$useCDN\\. Give it default value or assign it in the constructor\\.$#"
count: 1
path: src/Dto/Workbox.php

-
message: "#^Class SpomkyLabs\\\\PwaBundle\\\\Dto\\\\Workbox has an uninitialized property \\$version\\. Give it default value or assign it in the constructor\\.$#"
count: 1
path: src/Dto/Workbox.php

-
message: "#^Class SpomkyLabs\\\\PwaBundle\\\\Dto\\\\Workbox has an uninitialized property \\$warmCachePlaceholder\\. Give it default value or assign it in the constructor\\.$#"
count: 1
path: src/Dto/Workbox.php

-
message: "#^Class SpomkyLabs\\\\PwaBundle\\\\Dto\\\\Workbox has an uninitialized property \\$widgetsPlaceholder\\. Give it default value or assign it in the constructor\\.$#"
count: 1
path: src/Dto/Workbox.php

-
message: "#^Class SpomkyLabs\\\\PwaBundle\\\\Dto\\\\Workbox has an uninitialized property \\$workboxImportPlaceholder\\. Give it default value or assign it in the constructor\\.$#"
count: 1
path: src/Dto/Workbox.php

-
message: "#^Class SpomkyLabs\\\\PwaBundle\\\\Dto\\\\Workbox has an uninitialized property \\$workboxPublicUrl\\. Give it default value or assign it in the constructor\\.$#"
count: 1
path: src/Dto/Workbox.php

-
message: "#^Method SpomkyLabs\\\\PwaBundle\\\\ImageProcessor\\\\GDImageProcessor\\:\\:process\\(\\) should return string but returns string\\|false\\.$#"
count: 1
Expand Down Expand Up @@ -444,3 +474,8 @@ parameters:
message: "#^Method SpomkyLabs\\\\PwaBundle\\\\Normalizer\\\\UrlNormalizer\\:\\:supportsNormalization\\(\\) has parameter \\$context with no value type specified in iterable type array\\.$#"
count: 1
path: src/Normalizer/UrlNormalizer.php

-
message: "#^Parameter \\#1 \\$string of function mb_strlen expects string, string\\|null given\\.$#"
count: 1
path: src/Subscriber/PwaDevServerSubscriber.php
2 changes: 1 addition & 1 deletion src/Command/CreateServiceWorkerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return self::SUCCESS;
}

$resourcePath = $this->fileLocator->locate('@SpomkyLabsPwaBundle/Resources/workbox.js', null, false);
$resourcePath = $this->fileLocator->locate('@SpomkyLabsPwaBundle/Resources/sw-skeleton.js', null, false);
if (count($resourcePath) !== 1) {
$io->error('Unable to find the Workbox resource.');
return Command::FAILURE;
Expand Down
135 changes: 82 additions & 53 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,32 +468,89 @@ private function setupServiceWorker(ArrayNodeDefinition $node): void
->info('The public URL to the service worker.')
->example('/sw.js')
->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.')
->example('//OFFLINE_FALLBACK_PLACEHOLDER')
->end()
->scalarNode('widgets_placeholder')
->defaultValue('//WIDGETS_PLACEHOLDER')
->info('The placeholder for the widgets. Will be replaced by the widgets management events.')
->example('//WIDGETS_PLACEHOLDER')
->arrayNode('workbox')
->info('The configuration of the workbox.')
->canBeDisabled()
->children()
->booleanNode('use_cdn')
->defaultFalse()
->info('Whether to use the local workbox or the CDN.')
->end()
->scalarNode('version')
->defaultValue('7.0.0')
->info('The version of the workbox. When using local files, the version is ignored shall be 7.0.0.')
->end()
->scalarNode('workbox_public_url')
->defaultValue('/workbox')
->info('The public path to the local workbox. Only used if use_cdn is false.')
->end()
->scalarNode('workbox_import_placeholder')
->defaultValue('//WORKBOX_IMPORT_PLACEHOLDER')
->info(
'The placeholder for the workbox import. Will be replaced by the workbox import.'
)
->example('//WORKBOX_IMPORT_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.')
->example('//OFFLINE_FALLBACK_PLACEHOLDER')
->end()
->scalarNode('widgets_placeholder')
->defaultValue('//WIDGETS_PLACEHOLDER')
->info(
'The placeholder for the widgets. Will be replaced by the widgets management events.'
)
->example('//WIDGETS_PLACEHOLDER')
->end()
->append(
$this->getUrlNode(
'offline_fallback',
'The URL of the offline fallback. If not set, the offline fallback will be disabled.'
)
)
->arrayNode('warm_cache_urls')
->treatNullLike([])
->treatFalseLike([])
->treatTrueLike([])
->info('The URLs to warm the cache. The URLs shall be served by the application.')
->arrayPrototype()
->beforeNormalization()
->ifString()
->then(static fn (string $v): array => [
'path' => $v,
])
->end()
->children()
->scalarNode('path')
->isRequired()
->info('The URL of the shortcut.')
->example('app_homepage')
->end()
->arrayNode('params')
->treatFalseLike([])
->treatTrueLike([])
->treatNullLike([])
->prototype('variable')->end()
->info('The parameters of the action.')
->end()
->end()
->end()
->end()
->end()
->end()
->append(
$this->getUrlNode(
'offline_fallback',
'The URL of the offline fallback. If not set, the offline fallback will be disabled.'
)
)
->scalarNode('scope')
->cannotBeEmpty()
->defaultValue('/')
Expand All @@ -504,34 +561,6 @@ private function setupServiceWorker(ArrayNodeDefinition $node): void
->defaultTrue()
->info('Whether the service worker should use the cache.')
->end()
->arrayNode('warm_cache_urls')
->treatNullLike([])
->treatFalseLike([])
->treatTrueLike([])
->info('The URLs to warm the cache. The URLs shall be served by the application.')
->arrayPrototype()
->beforeNormalization()
->ifString()
->then(static fn (string $v): array => [
'path' => $v,
])
->end()
->children()
->scalarNode('path')
->isRequired()
->info('The URL of the shortcut.')
->example('app_homepage')
->end()
->arrayNode('params')
->treatFalseLike([])
->treatTrueLike([])
->treatNullLike([])
->prototype('variable')->end()
->info('The parameters of the action.')
->end()
->end()
->end()
->end()
->end()
->end()
->end();
Expand Down
21 changes: 1 addition & 20 deletions src/Dto/ServiceWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,5 @@ final class ServiceWorker
#[SerializedName('use_cache')]
public null|bool $useCache = null;

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

#[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;

/**
* @var array<Url>
*/
#[SerializedName('warm_cache_urls')]
public array $warmCacheUrls = [];
public Workbox $workbox;
}
44 changes: 44 additions & 0 deletions src/Dto/Workbox.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace SpomkyLabs\PwaBundle\Dto;

use Symfony\Component\Serializer\Attribute\SerializedName;

final class Workbox
{
public bool $enabled;

#[SerializedName('use_cdn')]
public bool $useCDN;

public string $version;

#[SerializedName('workbox_public_url')]
public string $workboxPublicUrl;

#[SerializedName('workbox_import_placeholder')]
public string $workboxImportPlaceholder;

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

#[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;

/**
* @var array<Url>
*/
#[SerializedName('warm_cache_urls')]
public array $warmCacheUrls = [];
}
2 changes: 2 additions & 0 deletions src/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use SpomkyLabs\PwaBundle\Subscriber\AssetsCompileEventListener;
use SpomkyLabs\PwaBundle\Subscriber\PwaDevServerSubscriber;
use SpomkyLabs\PwaBundle\Subscriber\ServiceWorkerCompileEventListener;
use SpomkyLabs\PwaBundle\Subscriber\WorkboxCompileEventListener;
use SpomkyLabs\PwaBundle\Twig\PwaExtension;
use SpomkyLabs\PwaBundle\Twig\PwaRuntime;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
Expand Down Expand Up @@ -65,6 +66,7 @@
;
}

$container->set(WorkboxCompileEventListener::class);
$container->set(AssetsCompileEventListener::class);
$container->set(ServiceWorkerCompileEventListener::class);
$container->set(ServiceWorkerBuilder::class);
Expand Down
6 changes: 3 additions & 3 deletions src/Resources/workbox.js → src/Resources/sw-skeleton.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
importScripts(
'https://storage.googleapis.com/workbox-cdn/releases/7.0.0/workbox-sw.js'
);
// *** Workbox ***
// Workbox is imported from a CDN or local files.
//WORKBOX_IMPORT_PLACEHOLDER

// *** Recipes ***
// You are free to change or remove any of these presets as you wish.
Expand Down
Loading

0 comments on commit 16b8a0a

Please sign in to comment.