-
-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add preload URLs generator and refactored caching strategy #163
Conversation
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
{
/**
* @return iterable<Url|string>
*/
public function generateUrls(): iterable
{
yield '/dummy/1';
yield Url::create('app_page', ['id' => 1]);
yield Url::create('app_page', ['id' => 2]);
}
} 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' |
I changed the interface. It is now required to indicate the alias so that the service declaration is much simple. <?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
|
5dc1306
to
aec12b5
Compare
This change introduces dynamic URL preloading in cache strategy. It modifies the resource cache to accept string aliases representing predefined URL generators. These generators can then produce URLs to be included in caching. This enhancement allows for more flexible and dynamic URL caching for applications.
aec12b5
to
3e3ca4a
Compare
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