Spomky-Labs / pwa-bundle

PHP library for generating a full featured PWA manifest
https://pwa.spomky-labs.com
MIT License
29 stars 1 forks source link

Making CachingStrategy available in Cache #118

Closed tacman closed 2 months ago

tacman commented 2 months ago

Description

I'd like to add the caching strategy to the debug toolbar, I think it's one of the most critical pieces of information. Related to #117 and #113

image

Example

Perhaps when we implement the CacheInterface with the common methods, we could add a getCacheStrategy(). But I could be completely wrong about where the strategy is defined.

Spomky commented 2 months ago

Hi @tacman,

I've just push an update that may solve your problem. The way the SW Compiler works has been reviewed. Now it iterates over a list of rules. It will be simpler to add rules in the future.

Also, I created a new interface SpomkyLabs\PwaBundle\Service\HasCacheStrategies and service that have this interface have the tag spomky_labs_pwa.cache_strategy. This means you can iterate over this tag to get all cache strategies used in an application.

<?php

declare(strict_types=1);

namespace Acme;

use SpomkyLabs\PwaBundle\Service\HasCacheStrategies;

final readonly class ListCacheStrategies
{
    /**
     * @param iterable<HasCacheStrategies> $cacheStrategyServices
     */
    public function __construct(
        #[TaggedIterator('spomky_labs_pwa.cache_strategy')]
        private iterable $cacheStrategyServices,
    ) {
    }

    public function doWhatYouWant(): array
    {
        foreach($this->cacheStrategyServices as $service) {
            $strategies = $service->getCacheStrategies();
            ///
        }
    }
}
Spomky commented 2 months ago

Note that warm cache Urls are not yet managed. Let me know if you see other fields to add to the options property.

Spomky commented 2 months ago

I created a command to demonstrate how it works:

symfony console pwa:cache:list-strategies
tacman commented 2 months ago

Awesome! I'll integrate it now.

Spomky commented 2 months ago

Hi,

The URLs and plugins are now part of the returned data. I think you will be able to build a very detailed table now. Just ask if you need other data.

Regards.

tacman commented 2 months ago

Thanks!

I hope to have something soon, this is more complicated than I'd hoped, but I think is going to be helpful to anyone working with the bundle.

The debug toolbar is already pointing out an issue. The planet pages never changes, but the voyages do. So I want to serve the cached planet page, and make a turbo/stimulus/ajax call to /planet/3/_voyages to get the html snippet showing the voyage, which has a different cache strategy.

Or I could make an ajax call to get json or jsonld data, which might not have the appropriate extension, but will have a mime-type in the Response and of course will match /api.

I should have something to show soon. What do you think about an EventListener to populate the cacheStrategy and preload pages?

Spomky commented 2 months ago

Yesterday I was thinking about a way to serve pages with different caching strategies. I implement that only for the purge_cache that is intended to by used when navigating. I will change that soon to all different caching type depending on a regex

tacman commented 2 months ago

I've updated the toolbar in pwa-extra to show all the caches, not just the ones with warmUrls. There's too many tabs, though, I'm not sure how to best organize these.

image

cF=cacheFirst, nO=networkOnly, etc. The number in the badge is the warm cache count.

github-actions[bot] commented 1 month ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.