From ba0af1afd9184c4131ea4d4958cc51529d62d82b Mon Sep 17 00:00:00 2001 From: nobswebdev Date: Sat, 21 Nov 2026 23:34:44 +0000 Subject: [PATCH] Preserve product-list scroll when switching variants. Append #product-card-{variantId} to index selection links so the browser restores the card after the full-page reload, matching add-to-cart. --- .../StorefrontProductViewService.spec.ts | 44 +++++++++++++++++++ .../services/StorefrontProductViewService.ts | 8 +++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/backend/src/modules/storefrontProduct/services/StorefrontProductViewService.spec.ts b/backend/src/modules/storefrontProduct/services/StorefrontProductViewService.spec.ts index d358ecb..8e0b65a 100644 --- a/backend/src/modules/storefrontProduct/services/StorefrontProductViewService.spec.ts +++ b/backend/src/modules/storefrontProduct/services/StorefrontProductViewService.spec.ts @@ -1,4 +1,6 @@ +import { DeliveryMode } from '../../product/types/DeliveryMode'; import type { CategoriesService } from '../../product/services/CategoriesService'; +import type { StorefrontProduct } from '../types/StorefrontProduct'; import { StorefrontProductViewService } from './StorefrontProductViewService'; describe('StorefrontProductViewService', () => { @@ -19,4 +21,46 @@ describe('StorefrontProductViewService', () => { { id: 'category-2', name: 'Physical', href: '/shop/categories/category-2' } ]); }); + + it('appends product-card anchors to index variant selection hrefs', () => { + const product: StorefrontProduct = { + id: 'product-1', + title: 'Widget', + deliveryMode: DeliveryMode.Auto, + descriptionHtml: null, + variants: [ + { + id: 'variant-1', + productId: 'product-1', + productTitle: 'Widget', + title: 'Default', + price: 10, + deliveryMode: DeliveryMode.Auto, + stockAvailable: 1, + stockForSession: 1, + thumbnailUrl: null, + images: [] + }, + { + id: 'variant-2', + productId: 'product-1', + productTitle: 'Widget', + title: 'Alt', + price: 12, + deliveryMode: DeliveryMode.Auto, + stockAvailable: 1, + stockForSession: 1, + thumbnailUrl: null, + images: [] + } + ] + }; + + const [view] = service.toProductsIndexView([product], {}); + + expect(view.variants.map(variant => variant.selectionHref)).toEqual([ + '/#product-card-variant-1', + '/?sel=product-1%3Avariant-2#product-card-variant-2' + ]); + }); }); diff --git a/backend/src/modules/storefrontProduct/services/StorefrontProductViewService.ts b/backend/src/modules/storefrontProduct/services/StorefrontProductViewService.ts index bb3f57f..ffbba36 100644 --- a/backend/src/modules/storefrontProduct/services/StorefrontProductViewService.ts +++ b/backend/src/modules/storefrontProduct/services/StorefrontProductViewService.ts @@ -220,11 +220,15 @@ export class StorefrontProductViewService { } private buildIndexUrl(basePath: string, product: StorefrontProduct, variantId: string): string { + const anchor = `#product-card-${variantId}`; + if (variantId === product.variants[0].id) { - return basePath; + return `${basePath}${anchor}`; } - return `${basePath}?${new URLSearchParams({ sel: `${product.id}:${variantId}` }).toString()}`; + const params = new URLSearchParams({ sel: `${product.id}:${variantId}` }); + + return `${basePath}?${params.toString()}${anchor}`; } async resolveProductBackLink(categoryId?: string): Promise<{ backHref: string; backLabel: string }> {