Spomky-Labs / pwa-bundle

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

Preloaded URLs generator #154

Closed Spomky closed 1 month ago

Spomky commented 1 month ago

Description

The preloaded URLs for resource caching is very simple: it is a list of URLs or route names with/without params. It can be difficult or boring to list all possible values. For example, let's imagine an application with 4 features. The application is served in 3 differents languages. The configuraiton file will looks like as follows:

pwa:
    serviceworker:
        wokrbox:
            resource_caches:
                preload_urls:
                  - path: 'app_features'
                  - params:
                        _locale: 'en'
                        feature: 'feature1'
                  - path: 'app_features'
                  - params:
                        _locale: 'jp'
                        feature: 'feature1'
                  - path: 'app_features'
                  - params:
                        _locale: 'it'
                        feature: 'feature1'
...

We could imagine a service that will generate the URLs for us. Each application will be free to manipulate that list depending on its needs.

Example

pwa:
    serviceworker:
        wokrbox:
            resource_caches:
                preload_urls:
                  - id: "preloaded_urls_features_service"
...
interface UrlGenerator
{
    /**
     * @return iterable<Url>
     */
    public function generateUrls(): iterable;
}
final readonly class PreloadUrlsForFeatures implements UrlGenerator
{
    public function generateUrls(): iterable
    {
        foreach($locales as $locale) {
            foreach($features as $feature) {
                yield ... // Generate a SpomkyLabs\PwaBundle\Dto\Url here
            }
        }
    }
}
tacman commented 1 month ago

I was playing with a bundle recently https://github.com/Pierstoval/SmokeTesting that creates test routes for pages that had no parameters (e.g. home, contact, etc.).

This idea could be used to create a pre-load service as described above, that returns a list of all the routes with no parameters.

The downside is that inspecting all the routes this way on every request isn't very efficient, so better might be to cache those routes during the compiler pass. I had some code in pwa-exta to do this, but I never got it working quite right.

Regardless, that's an enhancement/optimization, if you implement this service it will make it much easier to add those preload urls, thanks!

tacman commented 1 month ago

Can you name the interface UrlGeneratorInterface? I like the suffix, I think it's pretty standard.

Spomky commented 1 month ago

Can you name the interface UrlGeneratorInterface? I like the suffix, I think it's pretty standard.

This will be donne soon. See #155

Spomky commented 1 month ago

This idea could be used to create a pre-load service as described above, that returns a list of all the routes with no parameters.

I am not sure it is correct to list all routes without parameters. Some of them are private and there is not reason to cache them. Also, not all the application is supposed to be cached. You may have a dashboard where actions are done online and another part of the app that is offline-capable.

tacman commented 1 month ago

Makes sense. I have an attribute in PwaExtraBundle that I'll use when this is implemented to define the routes from attributes, rather than all routes without parameters, as you say.

Spomky commented 1 month ago

That's also the idea behind these services. You can return URLs from static values, a list of entries in a database and even routes with an attribute.

tacman commented 1 month ago

Thanks!

Are there any demo repos that show this? Maybe QOTD? I know you haven't started the 1.2 branch of the docs yet, maybe a working demo would be easier while 1.2 is still in dev.

Spomky commented 1 month ago

I've just updated the phpwa-demo repo.

tacman commented 1 month ago

Instead of

    #[PreloadUrl(alias: 'homepage', params: ['_locale' => 'fr'])]
    #[PreloadUrl(alias: 'homepage', params: ['_locale' => 'en'])]
    #[PreloadUrl(alias: 'homepage', params: ['_locale' => 'it'])]

What about implementing the UrlGeneratorInterface and using the service? That would also allow you get get the acceptable languages from the Symfony services.

Coincidentally, I've been playing around with a bundle for storing translations in doctrine, since DoctrineExtensions, which I used to use, doesn't support Symfony 7.

Spomky commented 1 month ago

What about implementing the UrlGeneratorInterface and using the service?

I was just testing if multiple attributes works fine. But yes you are right in this case a service would be better.

I've been playing around with a bundle for storing translations in doctrine

I used to play with it in the past. But found it was hard and stopped.

tacman commented 1 month ago

polyglot-bundle looks pretty interesting, as soon as my PR that fixes https://github.com/webfactory/WebfactoryPolyglotBundle/issues/52 is accepted I'll put it in a PWA.

github-actions[bot] commented 1 week 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.