Skip to content

Commit

Permalink
Merge pull request #32 from scandipwa/slider-prealod
Browse files Browse the repository at this point in the history
Adjust preload for cms pages
  • Loading branch information
AleksandrsKondratjevs authored Feb 14, 2023
2 parents b052b06 + 36c2355 commit 308c2af
Showing 1 changed file with 40 additions and 10 deletions.
50 changes: 40 additions & 10 deletions src/Controller/Router.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/**
* @category ScandiPWA
* @package ScandiPWA\Router
Expand Down Expand Up @@ -41,6 +40,7 @@
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use ScandiPWA\CmsGraphQl\Model\Resolver\DataProvider\Page as PageProvider;
use Magento\Cms\Api\GetPageByIdentifierInterface;
use Magento\Cms\Api\PageRepositoryInterface;
use Magento\Framework\Filter\Template\Tokenizer\Parameter as TokenizerParameter;
use ScandiPWA\SliderGraphQl\Model\Resolver\Slider as SliderResolver;

Expand Down Expand Up @@ -129,6 +129,16 @@ class Router extends BaseRouter
*/
protected $sliderResolver;

/**
* @var PageRepositoryInterface
*/
protected $pageRepository;

/**
* @var GetPageByIdentifierInterface
*/
protected $pageByIdentifier;

/**
* Router constructor.
* @param ActionList $actionList
Expand Down Expand Up @@ -171,8 +181,11 @@ public function __construct(
PageProvider $pageProvider,
TokenizerParameter $tokenizerParameter,
SliderResolver $sliderResolver,
PageRepositoryInterface $pageRepository,
GetPageByIdentifierInterface $getPageByIdentifier,
array $ignoredURLs = []
) {
)
{
$this->scopeConfig = $scopeConfig;
$this->themeProvider = $themeProvider;
$this->validationManager = $validationManager;
Expand All @@ -186,6 +199,8 @@ public function __construct(
$this->storeId = $this->storeManager->getStore()->getId();
$this->tokenizerParameter = $tokenizerParameter;
$this->sliderResolver = $sliderResolver;
$this->pageRepository = $pageRepository;
$this->pageByIdentifier = $getPageByIdentifier;

parent::__construct(
$actionList,
Expand Down Expand Up @@ -313,9 +328,14 @@ protected function setResponseHomePage(ActionInterface $action)
try {
$page = $this->getCmsPageFromGraphqlArea(null, $homePageIdentifier);

if (!isset($page['cmsPage'])) {
$action->setType('PWA_ROUTER');
return;
}

$action->setType(self::PAGE_TYPE_CMS_PAGE);
$action->setId($page['page_id'] ?? '');
$action->setCmsPage($page);
$action->setId($page['cmsPage']['page_id'] ?? '');
$action->setCmsPage($page['cmsPage']);
$action->setSlider($this->getSliderInformation($page['content'] ?? ''));
} catch (\Throwable $th) {
$action->setType('PWA_ROUTER');
Expand Down Expand Up @@ -351,10 +371,15 @@ protected function getSliderInformation($content)
protected function setResponseCmsPage($id, ActionInterface $action)
{
try {
$page = $this->getCmsPageFromGraphqlArea((int) $id);
$page = $this->getCmsPageFromGraphqlArea((int)$id);

if (!isset($page['cmsPage'])) {
$this->setNotFound($action);
return;
}

$action->setId($page['page_id'] ?? '');
$action->setCmsPage($page);
$action->setId($page['cmsPage']['page_id'] ?? '');
$action->setCmsPage($page['cmsPage']);
$action->setSlider($this->getSliderInformation($page['content'] ?? ''));
} catch (\Throwable $th) {
$this->setNotFound($action);
Expand Down Expand Up @@ -444,19 +469,23 @@ protected function getCmsPageFromGraphqlArea(int $id = null, string $identifier

$pageProvider = $objectManager->create(PageProvider::class);
$page = null;
$result = [];

if ($id) {
$page = $pageProvider->getDataByPageId($id, (int)$this->storeId);
$page = $this->pageRepository->getById($id);
}

if ($identifier) {
$page = $pageProvider->getDataByPageIdentifier($identifier, (int)$this->storeId);
$page = $this->pageByIdentifier->execute($identifier, (int)$this->storeId);
}

$result['cmsPage'] = $pageProvider->convertPageData($page);
$result['content'] = $page->getContent();

// vvv reset config area back to initial
$objectManager->configure($configLoader->load($currentConfigArea));

return $page;
return $result;
} catch (\Exception $e) {
return null;
}
Expand Down Expand Up @@ -631,3 +660,4 @@ protected function _checkShouldBeSecure(RequestInterface $request, $path = '')
}
}
}

0 comments on commit 308c2af

Please sign in to comment.