Skip to content

Commit

Permalink
FFWEB-3313: Remove unnecessary star (*) search requests on dev env mode
Browse files Browse the repository at this point in the history
For dev env fields roles are not cached because cache is cleared with each request. So for dev env it sent a lot of star(*) search requests to update fields roles. Now for dev env fields roles are fetched from services.xml
  • Loading branch information
Rayn93 authored Feb 10, 2025
1 parent cdfca88 commit dea20c9
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Fix
- Remove CategoryPath filter in URL and ASN on category page
- Fix add to cart tracking issue for shopware listing products
- Remove unnecessary star (*) search requests on dev env mode

## [v6.1.0] - 2025.01.28
### Change
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ Without SSR enabled, web crawlers could not have a chance to scan the element re

**Note:** More information about SSR concept you can find in the article [Server Side Rendering](https://web-components.fact-finder.de/documentation/4.x/server-side-rendering) from Web Components documentation.

**Note:** If you have a problem with displaying product images or prices correctly, you probably have Field Roles set incorrectly. You can easily fix this by setting [custom fields roles](#set-custom-field-roles)

## Advanced Settings

![Advanced Settings](docs/assets/advanced-settings.png "Advanced Settings")
Expand Down
25 changes: 21 additions & 4 deletions src/Config/CachedFieldRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,27 @@ class CachedFieldRoles implements FieldRolesInterface
{
private FieldRolesInterface $decorated;
private AdapterInterface $cache;
private string $kernelEnv;
private array $defaultFieldRoles;

public function __construct(FieldRolesInterface $decorated, AdapterInterface $cache)
{
$this->decorated = $decorated;
$this->cache = $cache;
public function __construct(
FieldRolesInterface $decorated,
AdapterInterface $cache,
string $kernelEnv,
array $fieldRoles,
) {
$this->decorated = $decorated;
$this->cache = $cache;
$this->kernelEnv = $kernelEnv;
$this->defaultFieldRoles = $fieldRoles;
}

public function getRoles(?string $salesChannelId): array
{
if ($this->isDevEnv()) {
return $this->defaultFieldRoles;
}

$salesChannelId = $salesChannelId ?? '';
$cacheKey = $this->getCacheKey($salesChannelId);
$item = $this->cache->getItem($cacheKey);
Expand Down Expand Up @@ -49,4 +61,9 @@ private function getCacheKey(string $salesChannelId): string
{
return sprintf('factfinder-field-roles-%s', $salesChannelId);
}

private function isDevEnv(): bool
{
return $this->kernelEnv === 'dev';
}
}
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<bind key="$factfinderLogger" type="service" id="monolog.logger.factfinder_channel" />
<bind key="$productsColumnsBase">%factfinder.export.products.columns.base%</bind>
<bind key="$kernelProjectDir">%kernel.project_dir%</bind>
<bind key="$kernelEnv">%kernel.environment%</bind>
</defaults>

<service id="Omikron\FactFinder\Shopware6\Communication\AdapterFactory" />
Expand Down

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions src/Utilites/Ssr/PriceFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PriceFormatter
private SalesChannelService $channelService;
private SalesChannelContext $context;
private CurrencyFormatter $currencyFormatter;
private array $fieldRoles;
private CachedFieldRoles $fieldRolesService;
private array $defaultFieldRoles;

public function __construct(
Expand All @@ -26,7 +26,7 @@ public function __construct(
$this->channelService = $channelService;
$this->currencyFormatter = $currencyFormatter;
$this->context = $this->channelService->getSalesChannelContext();
$this->fieldRoles = $fieldRolesService->getRoles($this->context->getSalesChannelId());
$this->fieldRolesService = $fieldRolesService;
$this->defaultFieldRoles = $fieldRoles;
}

Expand Down Expand Up @@ -71,7 +71,9 @@ private function getFormattedPrice(float $price): string

private function getPriceField(): string
{
return $this->fieldRoles['price'] ?? $this->defaultFieldRoles['price'] ?? 'Price';
$fieldRoles = $this->fieldRolesService->getRoles($this->context->getSalesChannelId());

return $fieldRoles['price'] ?? $this->defaultFieldRoles['price'] ?? 'Price';
}

private function convertRecord(array $record): array
Expand Down

0 comments on commit dea20c9

Please sign in to comment.