Skip to content
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

Merged
merged 1 commit into from
Apr 7, 2024

Conversation

Spomky
Copy link
Member

@Spomky Spomky commented Apr 5, 2024

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

  • It is a Bug fix
  • It is a New feature
  • Breaks BC
  • Includes Deprecations

Sorry, something went wrong.

@Spomky Spomky added the enhancement New feature or request label Apr 5, 2024
@Spomky Spomky added this to the 1.2.0 milestone Apr 5, 2024
@Spomky Spomky self-assigned this Apr 5, 2024
@Spomky Spomky linked an issue Apr 5, 2024 that may be closed by this pull request
@Spomky
Copy link
Member Author

Spomky commented Apr 5, 2024

How to use it?

  1. Create your generator
<?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'

@tacman
Copy link
Contributor

tacman commented Apr 5, 2024

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'

@Spomky
Copy link
Member Author

Spomky commented Apr 7, 2024

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

@Spomky Spomky force-pushed the features/preload-urls-generator branch 2 times, most recently from 5dc1306 to aec12b5 Compare April 7, 2024 16:01

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
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.
@Spomky Spomky force-pushed the features/preload-urls-generator branch from aec12b5 to 3e3ca4a Compare April 7, 2024 16:02
@Spomky Spomky merged commit dc34dc1 into 1.2.x Apr 7, 2024
7 checks passed
@Spomky Spomky deleted the features/preload-urls-generator branch April 7, 2024 16:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Preloaded URLs generator
2 participants