ubsicap / sltt-app

0 stars 0 forks source link

fix(sltt-app): runtimeCaching of requests absent #39

Open ericpyle opened 3 days ago

ericpyle commented 3 days ago

The PWA has this runtimeCaching of requests that is absent from sltt-app because it does not run the service worker (primarily because it handles its own updates, and already has its own static assets installed).

One approach would be to try to get vitePWA service worker logic working on sltt-app (electron). See my outstanding question about this here: https://discord.com/channels/937808017016119440/937973377883336704/1298750422575480864

For electron, we don't need caching for static assets and we don't need to check for service worker updates (since in both cases the latest should be in the installed package, and sltt-app has its own auto-update mechanism)

If we aren't able to easily get the vitePWA service worker to do the same thing for us in sltt-app (electron), we may consider other approaches: 1) uses caches directly for each of these requests so both PWA and sltt-app work the same way 2) do something different in sltt-app with its own proxy.

I (eric) am leaning toward 1. (See ApiDotBible.ts putToCache() as an example where the PWA is doing this)

        runtimeCaching: [
          /* port from sw-template.js */
          {
            /*
             * NOTE: This probably has no effect, because it needs a full s3 path to work 
             * (see slttresources note below) but we're using a localStorage.setItem('projects') for now
             */
            urlPattern: /projects$/,
            handler: 'CacheFirst',
            options: {
              cacheName: 'projects',
            },
          },
          // /* woff, woff2, ttf already covered by globPatterns */
          // {
          //   urlPattern: /\.(?:woff|woff2|ttf)$/,
          //   handler: 'CacheFirst',
          //   options: {
          //     cacheName: 'fonts',
          //   },
          // },
          {
            urlPattern: /sdb[gh]s\.json$/,
            handler: 'CacheFirst',
            options: {
              cacheName: 'sdbghs',
              expiration: {
                // These are biggish. Update once a week.
                maxAgeSeconds: 7 * 24 * 60 * 60, // 1 week
              },
            },
          },
          {
            /**
             * Because we are fetching things from a bucket that is not the same as the application
             * bucket we must write a regular expression that matches from the start of the url.
             * See: https://developers.google.com/web/tools/workbox/modules/workbox-routing
             * (Matching and Handling in Routes)
             */
            urlPattern: /https:\/\/s3.amazonaws.com\/sltt-resources.*\.json$/,
            handler: 'CacheFirst',
            options: {
              cacheName: 'slttresources',
              expiration: {
                maxAgeSeconds: 24 * 60 * 60, // 1 day
              },
            },
          },
        ],
ericpyle commented 3 days ago

For electron I need to set the start_url like so:

manifest: {
        start_url: './',

And to make sure <ServiceWorkerWrapper> is called even for sltt-app builds.