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

Preloaded URLs generator #154

Closed
Spomky opened this issue Apr 4, 2024 · 12 comments · Fixed by #163
Closed

Preloaded URLs generator #154

Spomky opened this issue Apr 4, 2024 · 12 comments · Fixed by #163
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@Spomky
Copy link
Member

Spomky commented Apr 4, 2024

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
            }
        }
    }
}
@Spomky Spomky added the enhancement New feature or request label Apr 4, 2024
@Spomky Spomky added this to the 1.2.0 milestone Apr 4, 2024
@Spomky Spomky self-assigned this Apr 4, 2024
@tacman
Copy link
Contributor

tacman commented Apr 4, 2024

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
Copy link
Contributor

tacman commented Apr 4, 2024

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

@Spomky
Copy link
Member Author

Spomky commented Apr 4, 2024

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

This will be donne soon. See #155

@Spomky
Copy link
Member Author

Spomky commented Apr 4, 2024

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
Copy link
Contributor

tacman commented Apr 4, 2024

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
Copy link
Member Author

Spomky commented Apr 4, 2024

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
Copy link
Contributor

tacman commented Apr 7, 2024

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
Copy link
Member Author

Spomky commented Apr 7, 2024

I've just updated the phpwa-demo repo.

@tacman
Copy link
Contributor

tacman commented Apr 7, 2024

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
Copy link
Member Author

Spomky commented Apr 7, 2024

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
Copy link
Contributor

tacman commented Apr 7, 2024

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

Copy link

github-actions bot commented May 9, 2024

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants