Skip to content

Commit

Permalink
FFWEB-3130: SSR for category pages
Browse files Browse the repository at this point in the history
  • Loading branch information
Rayn93 committed Jul 31, 2024
1 parent ae62439 commit cd8ab3e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Resources/views/storefront/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
{% endif %}
document.addEventListener('ffCoreReady', ({ factfinder }) => {
{% if page.extensions.factfinder.ssr == 'ssr' %}
{% if page.extensions.factfinder.ssr == 'ssr' and app.request.attributes.get('_route') == 'frontend.factfinder.result' %}
const searchResult = {FF_SEARCH_RESULT};
if (searchResult.hasOwnProperty('records')) {
Expand Down
12 changes: 10 additions & 2 deletions src/Resources/views/storefront/block/cms-block-listing.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% set columns = 1 %}
{% set element = block.slots.getSlot('toolbarFilters') %}

<!-- Category page -->
{# <!-- Category page -->#}
<script>
document.addEventListener(`ffCoreReady`, ({ factfinder }) => {
factfinder.config.setAppConfig({
Expand All @@ -11,7 +11,15 @@
],
});
factfinder.request.navigation({'filters': factfinder.config.get().appConfig.categoryPage}, { requestOptions: { origin: `searchImmediately` } });
{% if page.extensions.factfinder.ssr == 'ssr' %}
const searchResult = {FF_SEARCH_RESULT};
if (searchResult.hasOwnProperty('records')) {
factfinder.response.dispatch.ssrNavigation(searchResult);
}
{% else %}
factfinder.request.navigation({'filters': factfinder.config.get().appConfig.categoryPage}, { requestOptions: { origin: `searchImmediately` } });
{% endif %}
});
</script>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% block component_factfinder_paging %}
<ff-paging class="pagination" unresolved {% if showOnly %} show-only {% endif %}>
<ff-paging class="pagination" unresolved >
<ff-paging-item type="firstLink" class="page-item page-first">
<div class="page-link">
{% sw_icon 'arrow-medium-double-left' style { size: 'fluid', pack: 'solid'} %}
Expand Down
9 changes: 5 additions & 4 deletions src/Subscriber/CategoryPageResponseSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Event\BeforeSendResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
Expand Down Expand Up @@ -50,11 +51,11 @@ public function __construct(
public static function getSubscribedEvents()
{
return [
BeforeSendResponseEvent::class => 'onPageRendered',
KernelEvents::RESPONSE => 'onPageRendered',
];
}

public function onPageRendered(BeforeSendResponseEvent $event): void
public function onPageRendered(ResponseEvent $event): void
{
$request = $event->getRequest();
$response = $event->getResponse();
Expand Down Expand Up @@ -122,7 +123,7 @@ private function getCategoryPath(Request $request): string
$criteria->addFilter(new EqualsFilter('id', $categoryId));
$category = $this->categoryRepository->search($criteria, Context::createDefaultContext())->first();

return $category !== null ? $this->categoryPath->getValue($category) : '';
return $category !== null ? $this->categoryPath->getSsrValue($category) : '';
}

private function getCategoryId(Request $request): string
Expand Down
15 changes: 15 additions & 0 deletions src/Utilites/Ssr/Field/CategoryPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,19 @@ public function getValue(CategoryEntity $categoryEntity): string

return implode(',', $categories);
}

public function getSsrValue(CategoryEntity $categoryEntity): string
{
$categories = array_slice($categoryEntity->getBreadcrumb(), 1);
$categoryPath = implode('/', array_map(fn ($category): string => $this->encodeCategoryName($category), $categories));

return sprintf('filter=%s', urlencode($this->fieldName . ':' . $categoryPath));
}

private function encodeCategoryName(string $path): string
{
// important! do not modify this method
return preg_replace('/\+/', '%2B', preg_replace('/\//', '%2F',
preg_replace('/%/', '%25', $path)));
}
}

0 comments on commit cd8ab3e

Please sign in to comment.