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 can I cache post requests? #73

Open
bbhxwl opened this issue Oct 7, 2023 · 8 comments
Open

How can I cache post requests? #73

bbhxwl opened this issue Oct 7, 2023 · 8 comments

Comments

@bbhxwl
Copy link

bbhxwl commented Oct 7, 2023

How can I cache post requests?

@userquin
Copy link
Member

userquin commented Oct 7, 2023

You need to add the same regex (/^\/api\// in the example) to workbox.navigateFallbackDenylist array, and you should add also purge on quota error or play with max/min in the cache, check the workbox-build generateSW docs:

workbox: {
  navigateFallbackDenylist: [/^\/api\//],
  runtimeCaching: [{
    urlPattern: ({
      url, sameOrigin, request: { method }
    }) => sameOrigin && method === 'POST' && url.pathname.match(/^\/api\//),
    handler: 'NetworkFirst',
    cacheName: 'my-post-cache', // <== change the name
    options: {
      cacheableResponse: {
        statuses: [200]
      },
      matchOptions: {
        ignoreVary: true,
        ignoreSearch: true
      },
      plugins: [{
        // handlerDidError: async () => Response.redirect('/error', 302),
        cacheWillUpdate: async ({ response }) => response.status === 200 ? response : null
      }]
    }
  }]
}

@userquin
Copy link
Member

userquin commented Oct 7, 2023

if you're using injectManifest strategy, you can add the same logic via workbox modules.

@bbhxwl
Copy link
Author

bbhxwl commented Oct 8, 2023

You need to add the same regex (/^\/api\// in the example) to workbox.navigateFallbackDenylist array, and you should add also purge on quota error or play with max/min in the cache, check the workbox-build generateSW docs:

workbox: {
  navigateFallbackDenylist: [/^\/api\//],
  runtimeCaching: [{
    urlPattern: ({
      url, sameOrigin, request: { method }
    }) => sameOrigin && method === 'POST' && url.pathname.match(/^\/api\//),
    handler: 'NetworkFirst',
    cacheName: 'my-post-cache', // <== change the name
    options: {
      cacheableResponse: {
        statuses: [200]
      },
      matchOptions: {
        ignoreVary: true,
        ignoreSearch: true
      },
      plugins: [{
        // handlerDidError: async () => Response.redirect('/error', 302),
        cacheWillUpdate: async ({ response }) => response.status === 200 ? response : null
      }]
    }
  }]
}

Does this apply to nuxt2?

@userquin
Copy link
Member

userquin commented Oct 8, 2023

This module only supports nuxt 3 with Vite, I guess you can use the same runtimeCaching configuration in old nuxt 2 pwa module

@bbhxwl
Copy link
Author

bbhxwl commented Oct 8, 2023

This module only supports nuxt 3 with Vite, I guess you can use the same runtimeCaching configuration in old nuxt 2 pwa module

May I ask if nuxt2 was also developed by you? I can't find a solution for nuxt2 because my methods are all based on POST requests. However, I have a rule, such as the method of obtaining a list. The requests in the list will have a PageIndex parameter, or the returned data will have a Model or List attribute, which requires caching. Can you teach me how to implement it? If you don't know nuxt2, can you help me implement nuxt3? Can I try Nuxt2 for universal use?

@userquin
Copy link
Member

userquin commented Oct 8, 2023

Nuxt 2 pwa module developed by nuxt: https://nuxt.com/modules?version=2.x&q=Pwa

Read the docs, you need to add the configuration via some template IIRC.

@bbhxwl
Copy link
Author

bbhxwl commented Oct 8, 2023

Nuxt 2 pwa module developed by nuxt: https://nuxt.com/modules?version=2.x&q=Pwa

Read the docs, you need to add the configuration via some template IIRC.

How to implement it in nuxt3? The documentation for nuxt2 is not detailed, and I am considering upgrading if it is not possible.

@userquin
Copy link
Member

userquin commented Nov 8, 2023

IIRC you need to include a template for workbox.

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

2 participants