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 comments to sw.js showing relationship between routingStrategy and pwa.yaml #143

Closed
tacman opened this issue Mar 19, 2024 · 4 comments · Fixed by #149
Closed

Add comments to sw.js showing relationship between routingStrategy and pwa.yaml #143

tacman opened this issue Mar 19, 2024 · 4 comments · Fixed by #149
Assignees
Labels
good first issue Good for newcomers
Milestone

Comments

@tacman
Copy link
Contributor

tacman commented Mar 19, 2024

Description

When debugging, I look at sw.js to understand what's going on

});
workbox.routing.registerRoute(
  new RegExp('{/items/.*$}'),
  cache_1_0
);
const cache_1_1 = new workbox.strategies.NetworkOnly({
  plugins: [new workbox.backgroundSync.BackgroundSyncPlugin('add',{
    "maxRetentionTime": 1440,
    "forceSyncFallback": false, "onSync": async ({queue}) => {
    try {
        await queue.replayRequests();
    } catch (error) {
        // Failed to replay one or more requests
    } finally {
        remainingRequests = await queue.getAll();
        const bc = new BroadcastChannel('add-item');
        bc.postMessage({name: 'add', remaining: remainingRequests.length});
        bc.close();
    }
}
})]
});
workbox.routing.registerRoute(
  new RegExp('|/add|'),
  cache_1_1
);
const cache_2_0 = new workbox.strategies.CacheFirst({
  cacheName: 'fonts',plugins: [new workbox.expiration.ExpirationPlugin({
    "maxEntries": 75,
    "maxAgeSeconds": 31536000
}), new workbox.cacheableResponse.CacheableResponsePlugin({
    "statuses": [
        0,
        200
    ]
})]

Example

What I'd like to see is more comments in the sw.js that show why the code was generated.

            background_sync:
                - queue_name: 'items'
                  match_callback: 'regex: {/items/.*$}'
                  broadcast_channel: 'items-sync'
// pwa.yaml service_worker/workbox/background_sync: {queue_name: 'items', match_callback: "'regex: {/items/.*$}'", broadcast_channel: 'items-sync'"
workbox.routing.registerRoute(
  new RegExp('{/items/.*$}'),
  cache_1_0
);
const cache_1_1 = new workbox.strategies.NetworkOnly({
  plugins: [new workbox.backgroundSync.BackgroundSyncPlugin('add',{
    "maxRetentionTime": 1440,
    "forceSyncFallback": false, "onSync": async ({queue}) => {
    try {
        await queue.replayRequests();
    } catch (error) {
        // Failed to replay one or more requests
    } finally {
        remainingRequests = await queue.getAll();
        const bc = new BroadcastChannel('add-item');
        bc.postMessage({name: 'add', remaining: remainingRequests.length});
        bc.close();
    }
}
})]
});


// another comment that describes where the comes from
workbox.routing.registerRoute(
  new RegExp('|/add|'),
  cache_1_1
);
const cache_2_0 = new workbox.strategies.CacheFirst({
  cacheName: 'fonts',plugins: [new workbox.expiration.ExpirationPlugin({
    "maxEntries": 75,
    "maxAgeSeconds": 31536000
}), new workbox.cacheableResponse.CacheableResponsePlugin({
    "statuses": [
        0,
        200
    ]
})]

I think I can do this, as I'm poking around that section of the code to understand RegExp(). WDYT?

@Spomky
Copy link
Member

Spomky commented Mar 19, 2024

Yes, that's a good idea. As the debug variable is available, it will automatically adapt depending on the env

@Spomky Spomky self-assigned this Mar 19, 2024
@Spomky Spomky added the good first issue Good for newcomers label Mar 19, 2024
@Spomky Spomky added this to the 1.1.0 milestone Mar 19, 2024
@Spomky Spomky linked a pull request Mar 21, 2024 that will close this issue
4 tasks
@Spomky
Copy link
Member

Spomky commented Mar 21, 2024

Comments are now visible with debug mode enabled

@tacman
Copy link
Contributor Author

tacman commented Mar 21, 2024

It's great, thanks! May I make some tweaks? Or should I list my suggestions here?

Copy link

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 Apr 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants