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

How to exclude a backend route ? #218

Closed
alkanna opened this issue Jan 28, 2022 · 10 comments
Closed

How to exclude a backend route ? #218

alkanna opened this issue Jan 28, 2022 · 10 comments

Comments

@alkanna
Copy link

alkanna commented Jan 28, 2022

I have been successfully using this very basic approach : https://vite-plugin-pwa.netlify.app/guide/generate.html
However, since I have deployed this to production, I have an issue with a route that is supposed to be handled by the load balancer, and then distributed to another service than my SPA.
Currently, if I navigate to /backoffice on my SPA, the service worker seems to be picking up the GET request :
CleanShot 2022-01-28 at 16 55 34

Is there an option to exclude some routes and make then network only ?
I searched a bit on other issues here and found this option, but I don't know if this would be relevant to my case :

workbox: {
        runtimeCaching: [
          {
            handler: 'NetworkOnly',
            urlPattern: /\/backoffice\/.*\/*/,
            method: 'GET',
          },
        ],
      },
@userquin
Copy link
Member

You can try adding that route to the navigateFallbackDenylist on workbox plugin option (I test it on vue-router examples with the hi route, the route is not being intercepted by the sw):

// vite.config.ts
VitePWA({
  // other options
  workbox: {
    navigateFallbackDenylist: [/^\/backoffice/]
  }
}),

Previous option will generate this entry on your sw (you must deal with offline support for excluded routes):

  workbox.registerRoute(new workbox.NavigationRoute(workbox.createHandlerBoundToURL("index.html"), {
    denylist: [/^\/backoffice/]
  }));

@userquin
Copy link
Member

If I just refresh the page, the sw is not there:

imagen

If I go offline, then the sw will intercept the call when navigating but will not work on F5:

imagen

@userquin
Copy link
Member

imagen

@alkanna
Copy link
Author

alkanna commented Jan 29, 2022

Thanks, this seems to do the trick !

@jlabresh1
Copy link

You can try adding that route to the navigateFallbackDenylist on workbox plugin option (I test it on vue-router examples with the hi route, the route is not being intercepted by the sw):

// vite.config.ts
VitePWA({
  // other options
  workbox: {
    navigateFallbackDenylist: [/^\/backoffice/]
  }
}),

Previous option will generate this entry on your sw (you must deal with offline support for excluded routes):

  workbox.registerRoute(new workbox.NavigationRoute(workbox.createHandlerBoundToURL("index.html"), {
    denylist: [/^\/backoffice/]
  }));

This was very helpful, thank you

@mashpie
Copy link

mashpie commented May 30, 2022

yes, very helpful!

@userquin may I propose to add a section to docs, like "How to connect pwa to your backend api". I'd guess this is a very common use case for a lot of us, that didn't study workbox docs.

I don't remember any situation where I had to configure denylist in vue2 projects using @vue/cli-plugin-pwa. So actually, with that background, the implicit fallback to /index.html was new to me. To be honest I still don't know whether this is a workbox or vite-pwa default. Please feel free to say "RTFM", if I missed that... :)

Thanks for your work!

@userquin
Copy link
Member

userquin commented May 30, 2022

@mashpie PR welcome

Should also include the option when using custom sw, using the denylist entry on the navigation route.

I use it a few days ago on vitest docs to exclude the New action for stackblitz

@mashpie
Copy link

mashpie commented May 30, 2022

@userquin I'd like to, but I am afraid, that my knowledge of assumptions and internals of vite-plugin-pwa is far too limited.

Still playing with runtimeCaching options, which I can't get to work the way I understand docs (https://developer.chrome.com/docs/workbox/modules/workbox-strategies/ & https://vite-plugin-pwa.netlify.app/workbox/generate-sw.html#background-sync)

workbox: {
  // navigateFallbackDenylist: [/^\/api/]

  runtimeCaching: [
    {
      handler: 'NetworkFirst',
      urlPattern: /^\/api/,
      // method: 'POST'
      // options: {
      //   backgroundSync: {
      //     name: 'myQueueName',
      //     options: {
      //       maxRetentionTime: 24 * 60
      //     }
      //   }
      // }
    }
  ]
}

but this never hits my backend... not per GET nor per POST - shouldn't it?

@userquin
Copy link
Member

userquin commented Jun 17, 2022

@mashpie @jlabresh1 @alkanna will be included on the new docs on Workbox: generateSW and Workbox: injectManifest entries:

EDIT: also included on FAQ https://deploy-preview-304--vite-plugin-pwa.netlify.app/guide/faq.html#exclude-routes

@userquin
Copy link
Member

userquin commented Jun 20, 2022

@mashpie sorry for late response, you should include the method, if still not working file an issue on workbox repo, this plugin is just a wrapper for workbox

https://stackoverflow.com/a/56449689

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants