vuestorefront / vue-storefront

Alokai is a Frontend as a Service solution that simplifies composable commerce. It connects all the technologies needed to build and deploy fast & scalable ecommerce frontends. It guides merchants to deliver exceptional customer experiences quickly and easily.
https://www.alokai.com
MIT License
10.59k stars 2.08k forks source link

[Bug]: Default Invalidation Handler does not work correctly #6884

Open samberrry opened 1 year ago

samberrry commented 1 year ago

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?

Code of Conduct

samberrry commented 1 year ago

two people reported on discord with same error on discord: https://discord.com/channels/770285988244750366/770285988244750370/1068531011668938752

https://discord.com/channels/770285988244750366/770285988244750370/1023931660774932500

jeffpdotone commented 1 year ago

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

samberrry commented 1 year ago

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

samberrry commented 1 year ago
  plugins: [
    '~/plugins/body-parser.server.js'
  ],
samberrry commented 1 year ago

content:

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

app.use(bodyParserServer.json());
module.exports = app;
odranoelBR commented 1 year ago

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.

odranoelBR commented 1 year ago

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

odranoelBR commented 1 year ago

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

The invalidation cache endpoint works!

samberrry commented 1 year ago

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

tbanys commented 1 year ago

works for me to. Thanks @odranoelBR

skirianov commented 1 year ago

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

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

samberrry commented 1 year ago

@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.

skirianov commented 1 year ago

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