Skip to content

Commit 4a52eeb

Browse files
authored
FFWEB-3441: Support for WebComponents SSR
Support for WebComponents SSR
1 parent 15a8a3e commit 4a52eeb

10 files changed

Lines changed: 88 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### Add
44
- Add cart button for product list
55
- Implement proxy - more information in README file
6+
- Support for WebComponents SSR
67

78
### Change
89
- Support tab navigation for search, suggest and paging components

metadata.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@
9090
'value' => false,
9191
'position' => $settingPosition++,
9292
],
93+
[
94+
'group' => 'ffAdvanced',
95+
'name' => 'ffUseSsr',
96+
'type' => 'bool',
97+
'value' => false,
98+
'position' => $settingPosition++,
99+
],
93100
[
94101
'group' => 'ffAdvanced',
95102
'name' => 'ffSidAsUserId',

services.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,11 @@ services:
2121
# Twig Extensions
2222
Omikron\FactFinder\Oxid\Twig\Extensions\Filters\RecordDataJson:
2323
tags: [ 'twig.extension' ]
24+
25+
Omikron\FactFinder\Oxid\Utilities\Ssr\SearchAdapter:
26+
class: Omikron\FactFinder\Oxid\Utilities\Ssr\SearchAdapter
27+
public: true
28+
29+
Omikron\FactFinder\Communication\Client\ClientBuilder:
30+
class: Omikron\FactFinder\Communication\Client\ClientBuilder
31+
public: true

src/Controller/SearchResultController.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
use Omikron\FactFinder\Communication\Version;
99
use Omikron\FactFinder\Oxid\Event\EnrichProxyDataEvent;
1010
use Omikron\FactFinder\Oxid\Subscriber\EnrichProxyDataEventSubscriber;
11+
use Omikron\FactFinder\Oxid\Utilities\Ssr\SearchAdapter;
1112
use OxidEsales\Eshop\Application\Controller\FrontendController;
1213
use OxidEsales\Eshop\Core\Registry;
14+
use OxidEsales\EshopCommunity\Core\Di\ContainerFacade;
1315
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
1416
use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingServiceInterface;
1517
use Symfony\Component\EventDispatcher\EventDispatcher;
@@ -23,6 +25,14 @@ public function __construct()
2325
$this->_sThisTemplate = '@ffwebcomponents/webcomponents/blocks/page/result.html.twig';
2426
}
2527

28+
public function ssr(): void
29+
{
30+
$request = Registry::getRequest();
31+
$searchAdapter = ContainerFacade::get(SearchAdapter::class);
32+
$query = $this->removeOxidParams(parse_url($request->getRequestUrl(), PHP_URL_QUERY));
33+
$this->_aViewData['ssr_ff_response'] = $searchAdapter->search($query, false);
34+
}
35+
2636
public function proxy(): void
2737
{
2838
$currentUrl = Registry::getUtilsUrl()->getCurrentUrl();

src/Model/Config/Communication.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public function getParameters(): array
4242
'currency-country-code' => $this->getLocale($this->view->getActiveLangAbbr()),
4343
'search-immediate' => $this->isSearch() || $this->useForCategories() || $this->useProxy() ? 'true' : 'false',
4444
'category-page' => $this->useForCategories() ? $this->getCategoryPath($category) : null,
45+
'useSsr' => $this->useSsr(),
4546
];
4647

4748
return $params;
@@ -117,4 +118,9 @@ private function useProxy(): bool
117118
{
118119
return (bool) $this->moduleSettingService->getBoolean('ffUseProxy', 'ffwebcomponents');
119120
}
121+
122+
private function useSsr(): bool
123+
{
124+
return (bool) $this->moduleSettingService->getBoolean('ffUseSsr', 'ffwebcomponents');
125+
}
120126
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Omikron\FactFinder\Oxid\Utilities\Ssr;
6+
7+
use Omikron\FactFinder\Communication\Client\ClientBuilder;
8+
use Omikron\FactFinder\Communication\Version;
9+
use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingServiceInterface;
10+
11+
class SearchAdapter
12+
{
13+
public function __construct(
14+
private ClientBuilder $clientBuilder,
15+
private ModuleSettingServiceInterface $moduleSettingService,
16+
) {
17+
}
18+
19+
public function search(string $paramString, bool $navigationRequest): string
20+
{
21+
$client = $this->clientBuilder
22+
->withServerUrl((string) $this->moduleSettingService->getString('ffServerUrl', 'ffwebcomponents'))
23+
->withApiKey((string) $this->moduleSettingService->getString('ffApiKey', 'ffwebcomponents'))
24+
->withVersion(Version::NG)
25+
->build();
26+
27+
$endpoint = $this->createEndpoint($paramString, $navigationRequest);
28+
$response = $client->request('GET', $endpoint);
29+
30+
return $response->getBody()->getContents();
31+
}
32+
33+
private function createEndpoint(string $paramString, bool $navigationRequest): string
34+
{
35+
$channel = (string) $this->moduleSettingService->getCollection('ffChannel', 'ffwebcomponents')['en'];
36+
$endpoint = $navigationRequest ? 'navigation' : 'search';
37+
38+
return "rest/v5/{$endpoint}/{$channel}?{$paramString}";
39+
}
40+
}

views/admin_twig/de/ffwebcomponents_de_lang.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
'SHOP_MODULE_ffTrackingAddToCartCount_count_as_one' => 'track single click as one',
1717
'SHOP_MODULE_ffTrackingAddToCartCount_count_selected_amount' => 'track single click with selected amount',
1818
'SHOP_MODULE_ffUseProxy' => 'Proxy benutzen?',
19+
'SHOP_MODULE_ffUseSsr' => 'Verwenden Sie serverseitiges Rendering?',
1920
'SHOP_MODULE_ffSidAsUserId' => 'Die SID als userId senden, wenn der Benutzer nicht angemeldet ist?',
2021
'SHOP_MODULE_ffCartBtn' => 'Schaltfläche Warenkorb hinzufügen für die Produktliste',
2122
'SHOP_MODULE_GROUP_ffFeatures' => 'Features Settings',

views/admin_twig/en/ffwebcomponents_en_lang.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
'SHOP_MODULE_ffTrackingAddToCartCount_count_as_one' => 'track single click as one',
1717
'SHOP_MODULE_ffTrackingAddToCartCount_count_selected_amount' => 'track single click with selected amount',
1818
'SHOP_MODULE_ffUseProxy' => 'Use Proxy?',
19+
'SHOP_MODULE_ffUseSsr' => 'Use Server Side Rendering?',
1920
'SHOP_MODULE_ffSidAsUserId' => 'Send the SID as userId when user not logged in?',
2021
'SHOP_MODULE_ffCartBtn' => 'Add cart button for product list',
2122
'SHOP_MODULE_GROUP_ffFeatures' => 'Features Settings',

views/twig/extensions/themes/default/layout/base.html.twig

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@
4747
4848
factfinder.request.before.search(({ searchParams, searchOptions }) => {
4949
if (searchOptions?.requestOptions?.origin?.tagName === `FF-SEARCHBOX`) {
50-
window.location.href = `?query=${searchParams.query}&cl=search_result`;
50+
{% if communicationParams['useSsr'] %}
51+
window.location.href = `?query=${searchParams.query}&cl=search_result&fnc=ssr`;
52+
{% else %}
53+
window.location.href = `?query=${searchParams.query}&cl=search_result`;
54+
{% endif %}
5155
5256
// Cancel the pipeline to avoid sending a search request to FactFinder before the redirect is complete.
5357
return false;
@@ -71,7 +75,7 @@
7175
7276
{% endif %}
7377
74-
{% if oViewConf.getSearchImmediate()|escape("js") and oView.getClassKey() == "search_result" %}
78+
{% if oViewConf.getSearchImmediate()|escape("js") and oView.getClassKey() == "search_result" and not communicationParams['useSsr'] %}
7579
let searchParams = factfinder.utils.env.searchParamsFromUrl({ categoryFieldName: `{{oViewConf.getFFStringConfigParam('ffCategoryPathFieldName')}}` });
7680
initialSearch(searchParams, { requestOptions: { origin: `initialSearch` } });
7781
{% endif %}

views/twig/webcomponents/widget/record_list.html.twig

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<ff-record-list unresolved class="row gridView list-container" subscribe="{{ subscribe|default("true") }}">
1+
<ff-record-list {% if oViewConf.getFFBoolConfigParam("ffUseSsr") %} ssr {% endif%} unresolved class="row gridView list-container" subscribe="{{ subscribe|default("true") }}">
22
<template data-role="record">
33
<ff-record class="productData col-xs-12 col-sm-6 col-md-4 productBox">
44
<div class="card product-card">
@@ -42,6 +42,13 @@
4242
</template>
4343
</ff-record-list>
4444

45+
{% if oViewConf.getFFBoolConfigParam("ffUseSsr") and ssr_ff_response is defined and ssr_ff_response is not empty %}
46+
<script type="text/javascript">
47+
document.addEventListener(`ffCoreReady`, ({ factfinder }) => {
48+
factfinder.response.dispatch.ssrSearch({{ ssr_ff_response|raw }});
49+
});
50+
</script>
51+
{% endif %}
4552

4653

4754
{#Scrolling ?? #}

0 commit comments

Comments
 (0)