Closed Spomky closed 1 month ago
How to use it?
<?php
declare(strict_types=1);
namespace Acme\Services;
use SpomkyLabs\PwaBundle\CachingStrategy\PreloadUrlsGeneratorInterface; use SpomkyLabs\PwaBundle\Dto\Url;
class MyCustomUrlsGenerator implements PreloadUrlsGeneratorInterface { /**
Declare this class as a service with a tag and unique alias
// config/services.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
return function(ContainerConfigurator $container): void {
$services = $container->services();
$services->set(\Acme\Services\MyCustomUrlsGenerator ::class)
->tag('spomky_labs_pwa.preload_urls_generator', ['alias' => 'my-generator'])
;
};
Use it in your configuration. The alias shall be prefixed with @
pwa:
serviceworker:
workbox:
resource_caches:
- match_callback: 'navigate'
preload_urls:
- '@my-generator'
Is it possible to skip the manual wiring of the class in services.php|yaml and simply reference the class in the pwa.yaml config?
pwa:
serviceworker:
workbox:
resource_caches:
- match_callback: 'navigate'
preload_urls:
- 'Acme\Services\MyCustomUrlsGenerator'
Is it possible to skip the manual wiring of the class in services.php|yaml and simply reference the class in the pwa.yaml config?
I changed the interface. It is now required to indicate the alias so that the service declaration is much simple. Also, I created an attribute to indicate the whole controller class or the method should be preloaded.
<?php
namespace App\Controller;
use App\Form\ItemHandler;
use App\Repository\ItemRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use SpomkyLabs\PwaBundle\Attribute\PreloadUrl;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
#[PreloadUrl(alias: 'fooBar', params: ['_locale' => 'fr'], pathTypeReference: UrlGeneratorInterface::ABSOLUTE_URL)]
class HomepageController extends AbstractController
{
public function __construct(
private readonly ItemHandler $itemHandler,
private readonly ItemRepository $itemRepository,
){}
#[PreloadUrl(alias: 'homepage', params: ['_locale' => 'fr'])]
#[PreloadUrl(alias: 'homepage', params: ['_locale' => 'en'])]
#[PreloadUrl(alias: 'homepage', params: ['_locale' => 'it'])]
#[Route('/', name: 'app_homepage', methods: [Request::METHOD_GET])]
public function homepage(): Response
{
return $this->render('homepage/index.html.twig');
}
#[Route('/terms-of-service', name: 'app_tos', methods: [Request::METHOD_GET])]
public function tos(): Response
{
return $this->render('tos/index.html.twig');
}
}
Aliases are managed automatically. You just need to add @homepage
or @fooBar
to have them listed in the resource cache.
@homepage
will contain /?_locale=en
, /?_locale=fr
and /?_locale=it
@fooBar
will contain https://example.com/?_locale=fr
and https://example.com/terms-of-service?_locale=fr
Introduced a preload URLs generator to efficiently handle URLs and refactored caching strategy to enhance efficiency. The changes include creating a preload URLs generator manager, implementing a dummy URLs generator for testing, and updating cache resource classes. Deprecated PageCache in favor of ResourceCache for better flexibility in handling resources.
Target branch: 1.2.x Resolves issue #154