vendure-ecommerce/vendure

slugs are globally unique, not channel unique

Opened this issue · 3 comments

Describe the bug
We wrote a deep channel copier that uses a channel as a template for other channels.
One of the issues we ran into is that the entity duplicators add -copy to slugs. Not a great key for a slug. So we figured, we'll copy the product to the target channel and then remove the -copy of the slug.

Unfortunately, the findOneBySlug function:

  • loads the slug independent of channel on a findOne basis (and therefore hits the slug from the source channel)
  • then loads the resulting product by id on a channel specific basis
  • resulting in a 404

Having globally unique slugs seems like a bug to me, especially within the context of a multi vendor marketplace
What imho should happen is load the product channel specifically left joining the translation slug. That would resolve the issue

@michaelbromley I'm willing to write a fix but since this is core functionality not sure if you agree or want me to touch it.

To Reproduce
Steps to reproduce the behavior:

  1. Copy a product to another channel
  2. Rename the slug back to the original slug
  3. load the product by slug on the shop-api using the token for the new channel
  4. 404

Expected behavior
Product slugs should be unique on a per channel basis.

Environment (please complete the following information):

  • @vendure/core version: 2.2.3
  • Nodejs version 20
  • Database (mysql/postgres etc): postgres

Additional context

oidt commented

Are you using the right approach? For what I know is that a product is assigned to a channel from the default channel and never is copied to a channel.

https://docs.vendure.io/guides/core-concepts/channels/#channels-currencies--prices

What I'm doing might be considered something that wasn't considered as a common scenario but that should be possible and highlights an unnecessary limitation. IMHO

@mschipperheyn I think this is a bug and I agree it should be fixed.

If we look at the product creation/update process, we see a step where we validate a slug for uniqueness here. In this code it can be seen that we do take the active channel into account when checking for conflicts.

Therefore we should also apply the active channel when looking up a product by slug. I'd be happy to accept a PR for this if you want to contribute.