vuestorefront/vue-storefront

[Bug]: Default Invalidation Handler does not work correctly

samberrry opened this issue ยท 13 comments

Describe the Bug

We used redis cache driver, and caching is working very well.

When I call the invalidation URL I get error (default invalidation handler):

[dev:theme]  ERROR  [VSF][error]:  Cache driver thrown an error when invalidating cache! Operation skipped.

[dev:theme]  ERROR  [VSF][error]:  response.status is not a function

[dev:theme]   at handler (/Users/Documents/projects/binshops/vuestorefront/prestashop/node_modules/@vue-storefront/cache/nuxt/index.js:32:7)

[dev:theme]  ERROR  response.status is not a function

[dev:theme]   at handler (/Users/Documents/projects/binshops/vuestorefront/prestashop/node_modules/@vue-storefront/cache/nuxt/index.js:39:7)

My cache config:

    ['@vue-storefront/cache/nuxt', {
      enabled: true,
      invalidation: {
        endpoint: '/cache-invalidate',
        key: 'uniqueKey',
        handlers: [
          '@vue-storefront/cache/defaultHandler'
        ]
      },
      driver: [
        '@vue-storefront/redis-cache',
        {
          defaultTimeout: 86400,
          redis: {
            host: 'localhost',
            port: 6379
          }
        }
      ]
    }]

I use this url to clear redis:
http://localhost:3000/cache-invalidate?key=uniqueKey&tags=*

Current behavior

No response

Expected behavior

cache should be cleared after calling this url.

Steps to reproduce

No response

What version of Vue Storefront are you using?

any

What version of Node.js are you using?

any

What browser (and version) are you using?

any

What operating system (and version) are you using?

any

Relevant log output

No response

Able to fix / change the documentation?

  • Yes
  • No

Code of Conduct

  • I agree to follow this project's Code of Conduct

Our team suggests doing the following.

Add the file below with the content.

plugins/body-parser.js

const bodyParser = require('body-parser')
const app = require('express')()

app.use(bodyParser.json())
module. Exports = app

Remember to add them in your nuxt.config.js

Hey @jeffpdotone ,
first I tried it with body-parser.js and yarn build ran with error: many Module not found: Error: Can't resolve fs. I thought the problem is that it should be bound to server-side only, so I changed it to body-parser.server.js. Code built and ran successfully, however still same error.

[dev:theme]  ERROR  [VSF][error]:  Cache driver thrown an error when invalidating cache! Operation skipped.
[dev:theme] 
[dev:theme] 
[dev:theme]  ERROR  [VSF][error]:  response.status is not a function
[dev:theme] 
[dev:theme]   at handler (/Users/Documents/projects/binshops/vuestorefront/prestashop/node_modules/@vue-storefront/cache/nuxt/index.js:32:7)
[dev:theme] 
[dev:theme] 
[dev:theme]  ERROR  response.status is not a function
[dev:theme] 
[dev:theme]   at handler (/Users/Documents/projects/binshops/vuestorefront/prestashop/node_modules/@vue-storefront/cache/nuxt/index.js:39:7)
[dev:theme] 

thanks

  plugins: [
    '~/plugins/body-parser.server.js'
  ],

content:

const bodyParserServer = require('body-parser');
const app = require('express')();

app.use(bodyParserServer.json());
module.exports = app;

Same problem here using node v14 LTS.
We used the library whitout this problem for a long time and didn't have the problem.

Checking node HTTP Response object, the versions 12 14 16 don't have the method .status
https://nodejs.org/docs/latest-v14.x/api/http.html

Very strange, probably have plugins or something wrapping the object.

I added the bodyparse on the middleware without errors, but still not solve the .status problem.
Screenshot from 2023-02-28 18-37-02

Info: The .status come from express https://expressjs.com/pt-br/4x/api.html#res.status
.status is a wrapper of .statusCode from HTTP Node response

@samberrry Solved addyng the body parser to server middleware.
Screenshot from 2023-02-28 18-53-45

The invalidation cache endpoint works!

many thanks @odranoelBR , I will try it and I will update here, hope it solves the issue.

tbanys commented

works for me to. Thanks @odranoelBR

@samberrry if you're fine I will close this issue.

Do you think this could be useful to add to docs?

@skirianov
I guess a fix in validation handler is necessary,

I do not know how it should be handled, adding the fix snippet in docs or in package.

@samberrry let me add our TL, let's see what they think. @WojtekTheWebDev any ideas what would be the best solution here?