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

[Bug]: Inventory updates do not emit Events, preventing Cache Revalidation #11691

Open
RadzioN opened this issue Mar 3, 2025 · 5 comments
Open

Comments

@RadzioN
Copy link

RadzioN commented Mar 3, 2025

Package.json file

{
  "name": "my-medusa-store",
  "version": "0.0.1",
  "description": "A starter for Medusa projects.",
  "author": "Medusa (https://medusajs.com)",
  "license": "MIT",
  "keywords": [
    "sqlite",
    "postgres",
    "typescript",
    "ecommerce",
    "headless",
    "medusa"
  ],
  "scripts": {
    "build": "medusa build",
    "seed": "medusa exec ./src/scripts/seed.ts",
    "start": "medusa start",
    "dev": "medusa develop",
    "test:integration:http": "TEST_TYPE=integration:http NODE_OPTIONS=--experimental-vm-modules jest --silent=false --runInBand --forceExit",
    "test:integration:modules": "TEST_TYPE=integration:modules NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit",
    "test:unit": "TEST_TYPE=unit NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit"
  },
  "dependencies": {
    "@medusajs/admin-sdk": "2.5.1",
    "@medusajs/cli": "2.5.1",
    "@medusajs/framework": "2.5.1",
    "@medusajs/medusa": "2.5.1",
    "@mikro-orm/core": "6.4.3",
    "@mikro-orm/knex": "6.4.3",
    "@mikro-orm/migrations": "6.4.3",
    "@mikro-orm/postgresql": "6.4.3",
    "awilix": "^8.0.1",
    "pg": "^8.13.0"
  },
  "devDependencies": {
    "@medusajs/test-utils": "2.5.1",
    "@mikro-orm/cli": "6.4.3",
    "@swc/core": "1.5.7",
    "@swc/jest": "^0.2.36",
    "@types/jest": "^29.5.13",
    "@types/node": "^20.0.0",
    "@types/react": "^18.3.2",
    "@types/react-dom": "^18.2.25",
    "jest": "^29.7.0",
    "prop-types": "^15.8.1",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "ts-node": "^10.9.2",
    "typescript": "^5.6.2",
    "vite": "^5.2.11",
    "yalc": "^1.0.0-pre.53"
  },
  "engines": {
    "node": ">=20"
  }
}

Node.js version

v22.12.0

Database and its version

PostgreSQL 16

Operating system name and version

Windows 11

Browser name

Chrome

What happended?

Inventory updates (e.g., stock level changes) do not emit any events in Medusa. This prevents external services - such as a Next.js storefront using incremental static regeneration (ISR) - from revalidating product pages based on inventory changes.

Currently, Medusa emits events for updates to products and variants, but inventory workflows do not trigger any event emissions. As a result, any storefront relying on Medusa for inventory updates has no way to revalidate cache dynamically when stock levels change.

I’ve verified that:

  • The event reference does not include inventory - related events.
  • No events are emitted when inventory levels are updated via the admin panel or API.

This issue originates from #11679, where cache revalidation for inventory changes was discussed.

Expected behavior

When an inventory update occurs (e.g., stock level changes due to an order or manual adjustment in the admin panel), Medusa should emit an event such as inventory.updated or stock.level.changed. This would allow external services to listen for these changes and trigger necessary actions, such as cache revalidation.

Actual behavior

Currently, no events are emitted for inventory updates, preventing real-time cache updates and requiring workarounds like polling or no-store fetch strategies in Next.js storefronts.

Link to reproduction repo

npx create-medusa-app@latest

@riqwan
Copy link
Contributor

riqwan commented Mar 3, 2025

Hey @RadzioN, we seem to be emitting events here - https://github.com/medusajs/medusa/blob/develop/packages/modules/inventory/src/services/inventory-module.ts#L581-L590

General format is Module.Entity.Action like: inventory.inventory-level.updated

Does that not work for you?

@riqwan riqwan self-assigned this Mar 3, 2025
@RadzioN
Copy link
Author

RadzioN commented Mar 3, 2025

Hey @riqwan , unfortunately, it doesn’t work for me. I need the revalidation to trigger after updating stock levels via Product -> Variants -> Edit Stock Levels, but my subscriber isn’t receiving response from your suggestion event inventory.inventory-level.updated. Here’s my subscriber:


export default async function productChangedHandler({ container }: SubscriberArgs<{ id: string }>) {
  const logger = container.resolve("logger");
  const revalidateSecret = process.env.REVALIDATE_SECRET;
  const revalidateUrl = process.env.FRONTEND_REVALIDATE_URL;

  try {
    const response = await fetch(`${revalidateUrl}?secret=${revalidateSecret}`, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({ tag: "products" }),
    });

    if (!response.ok) {
      throw new Error(`Failed to revalidate: ${response.status}`);
    }

    const result = await response.json();
    logger.info(`Cache revalidated for tag: ${result.revalidated}`);
  } catch (error) {
    logger.error(`Error revalidating cache: ${error.message}`);
  }
}

export const config: SubscriberConfig = {
  event: [
    "product.created",
    "product.updated",
    "product.deleted",
    "product.status_change",
    "product-variant.created",
    "product-variant.updated",
    "product-variant.deleted",
    "product-option.created",
    "product-option.updated",
    "product-option.deleted",
    "inventory.inventory-level.updated",
  ],
};

Additionally:

There is no such event listed in the Medusa Events Reference.
I couldn’t find any event emissions related to this action in the Medusa Core Workflows Reference.

By the way, I’d really appreciate some help with issue #11635 . Even while testing your suggestion, I found this issue quite frustrating - it forces me to manually adjust CSS in Chrome DevTools to proceed with debugging.

Thanks in advance for any insights!

@riqwan
Copy link
Contributor

riqwan commented Mar 3, 2025

Let me take a look at these events. In the meantime, you want to give a shot opening a PR for the scroll bug?

@Edsparr
Copy link

Edsparr commented Mar 4, 2025

This might be related to #11704

I noticed this entity updated event is a general hook. Perhaps this is a more broad issue then just reservation-item.updated and stock-level.updated?

Let me take a look at these events. In the meantime, you want to give a shot opening a PR for the scroll bug?

@RadzioN
Copy link
Author

RadzioN commented Mar 4, 2025

Hey @riqwan , I’ve created a pull request to fix the scroll bug. I used the same approach you applied in product-prices.
Let me know when you have a solution for this issue!

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

No branches or pull requests

3 participants