Skip to content

Commit

Permalink
Fix cache symfony response instead of guzzle response
Browse files Browse the repository at this point in the history
  • Loading branch information
tsekka committed Jan 7, 2022
1 parent 258b45a commit 6d2b36b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 31 deletions.
6 changes: 5 additions & 1 deletion src/Actions/GetPrerenderedPageResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Illuminate\Support\Facades\App;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\RequestException;
use Symfony\Contracts\HttpClient\ResponseInterface;

class GetPrerenderedPageResponse
{
Expand Down Expand Up @@ -58,6 +57,11 @@ public function __construct(
$this->throwExceptions = config('prerender.throw_exceptions');
}

/**
* Prerender the page and return the Guzzle Response
*
* @return null|\GuzzleHttp\Psr7\Response
*/
public function handle(): ?Response
{
$headers = [
Expand Down
3 changes: 2 additions & 1 deletion src/Console/Commands/CachePagesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ private function prerenderAndCache(string $url): string
return 'SKIPPED_REDIRECT_STATUS_CODE:' . $httpStatusCode;
}

$cached = Prerender::cacheTheResponse($url, $prerenderedResponse);
$symfonyResponse = Prerender::buildSymfonyResponseFromGuzzleResponse($prerenderedResponse);
$cached = Prerender::cacheTheResponse($url, $symfonyResponse);
return $cached ? 'CACHED' : 'NOT_CACHED';
}
/**
Expand Down
30 changes: 4 additions & 26 deletions src/Http/Middleware/PrerenderMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function handle(Request $request, Closure $next): RedirectResponse|Symfon
return $next($request);
}

$prerenderedResponse = $this->getPrerenderedPageResponse($request);
$prerenderedResponse = (new GetPrerenderedPageResponse($request))->handle();
if (!$prerenderedResponse) {
$this->status = 'PRERENDER_RESPONSE_MISSING';
$this->recordCrawlerVisit();
Expand All @@ -107,11 +107,11 @@ public function handle(Request $request, Closure $next): RedirectResponse|Symfon
return Redirect::to(array_change_key_case($headers, CASE_LOWER)["location"][0], $this->httpStatusCode);
}

$response = $this->buildSymfonyResponseFromGuzzleResponse($prerenderedResponse);
Prerender::cacheTheResponse($this->fullUrl, $response);
$symfonyResponse = Prerender::buildSymfonyResponseFromGuzzleResponse($prerenderedResponse);
Prerender::cacheTheResponse($this->fullUrl, $symfonyResponse);
$this->status = 'PRERENDERED';
$this->recordCrawlerVisit();
return $response;
return $symfonyResponse;
}


Expand Down Expand Up @@ -140,26 +140,4 @@ private function shouldShowPrerenderedPage(Request $request): bool

return $shouldShow;
}

/**
* Prerender the page and return the Guzzle Response
*
* @param Request $request
* @return ResponseInterface|null
*/
private function getPrerenderedPageResponse(Request $request): ?ResponseInterface
{
return (new GetPrerenderedPageResponse($request))->handle();
}

/**
* Convert a Guzzle Response to a Symfony Response
*
* @param GuzzleResponse $prerenderedResponse
* @return SymfonyResponse
*/
private function buildSymfonyResponseFromGuzzleResponse(GuzzleResponse $prerenderedResponse): SymfonyResponse
{
return (new HttpFoundationFactory)->createResponse($prerenderedResponse);
}
}
19 changes: 16 additions & 3 deletions src/Prerender.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Tsekka\Prerender;

use GuzzleHttp\Psr7\Response;
use Illuminate\Support\Facades\Cache;
use GuzzleHttp\Psr7\Response as GuzzleResponse;
use Illuminate\Cache\Repository as CacheRepository;
use Tsekka\Prerender\Actions\RecordOrTouchPrerenderedPage;
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;

class Prerender
{
Expand Down Expand Up @@ -42,7 +44,7 @@ public function cacheEnabled()
return true;
}

public function cacheTheResponse(string $url, Response $response): bool
public function cacheTheResponse(string $url, SymfonyResponse $response): bool
{
if (!$this->cacheEnabled()) return false;

Expand All @@ -52,7 +54,7 @@ public function cacheTheResponse(string $url, Response $response): bool
return $this->cache->put($cacheKey, $response, $this->cacheTtl);
}

public function getCachedResponse(string $url): Response|null
public function getCachedResponse(string $url): SymfonyResponse|null
{
if (
$this->cacheEnabled()
Expand All @@ -62,4 +64,15 @@ public function getCachedResponse(string $url): Response|null

return null;
}

/**
* Convert a Guzzle Response to a Symfony Response
*
* @param GuzzleResponse $prerenderedResponse
* @return SymfonyResponse
*/
public function buildSymfonyResponseFromGuzzleResponse(GuzzleResponse $prerenderedResponse): SymfonyResponse
{
return (new HttpFoundationFactory)->createResponse($prerenderedResponse);
}
}

0 comments on commit 6d2b36b

Please sign in to comment.