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

urlPattern doesn't work as expected #22

Closed
artm4r opened this issue Feb 22, 2023 · 5 comments
Closed

urlPattern doesn't work as expected #22

artm4r opened this issue Feb 22, 2023 · 5 comments

Comments

@artm4r
Copy link

artm4r commented Feb 22, 2023

I have this rule for one of runtimeCaching rules in config. If request is without query all works, but if I add query to request string - caching isn't working
{ urlPattern: '${WPJsonUrl}/posts', handler: 'StaleWhileRevalidate', method: 'GET', options: { cacheName: 'test-categories', expiration: { maxEntries: 1, maxAgeSeconds: 10 * 24 * 60 * 60, }, cacheableResponse: { statuses: [0, 200] }, }, },
I tryed to write string like ${WPJsonUrl}/posts.*, but nothing changed.
If string in browser request will be like /posts?page=1&per_page=7&_embed=true caching will not work=(
I think problem is that regex isn't working with the urlPattern property.
In my nuxt2 project urlPattern: ${WPJsonUrl}/posts worked fine

@userquin
Copy link
Member

userquin commented Feb 22, 2023

@artm4r you can use callback approach instead regex (this is via custom sw not using generateSW and runtime caching plugin):

registerRoute(
    ({ url }) => url.pathname.startsWith(`${WPJsonUrl}/posts`),
    new StaleWhileRevalidate({
      cacheName: 'test-categories',
      plugins: [
        new CacheableResponsePlugin({ statuses: [0, 200] }),
        new ExpirationPlugin({ maxEntries: 1, maxAgeSeconds: 10 * 24 * 60 * 60 }),
      ],
    }),
  )

@artm4r
Copy link
Author

artm4r commented Feb 22, 2023

@userquin So I'll need to use InjectManifest?

@userquin
Copy link
Member

@artm4r no, wait preparing example...

@userquin
Copy link
Member

@artm4r try this one:

      {
        urlPattern: new RegExp(`^${WPJsonUrl}/posts.*`, 'i'),
        handler: 'StaleWhileRevalidate',
        options: {
          cacheName: 'test-categories',
          expiration: {
            maxEntries: 1,
            maxAgeSeconds: 10 * 24 * 60 * 60
          },
          cacheableResponse: {
            statuses: [0, 200],
          },
        },
      },

@artm4r
Copy link
Author

artm4r commented Feb 22, 2023

@userquin Thanks a lot! Now it works

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