From 98062f9c2fee2f8925761eec21dd6fcaf373df77 Mon Sep 17 00:00:00 2001 From: ChinSheng Date: Mon, 2 Mar 2020 16:06:10 +0800 Subject: [PATCH 1/2] Support laravel 6 --- composer.json | 2 +- src/PrerenderMiddleware.php | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index 0f472aa..c219a63 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ ], "require": { "php": ">=5.5.9", - "illuminate/support": "~5.0.14|~5.1", + "illuminate/support": "~5.0.14|~5.1|^6.0", "guzzlehttp/guzzle": "^6.0", "symfony/psr-http-message-bridge": "^1.0" }, diff --git a/src/PrerenderMiddleware.php b/src/PrerenderMiddleware.php index 04f6ac5..475fa69 100644 --- a/src/PrerenderMiddleware.php +++ b/src/PrerenderMiddleware.php @@ -5,11 +5,12 @@ use Closure; -use Redirect; +use GuzzleHttp\Client as Guzzle; use GuzzleHttp\Exception\RequestException; use Illuminate\Contracts\Foundation\Application; -use GuzzleHttp\Client as Guzzle; +use Illuminate\Support\Str; use Psr\Http\Message\ResponseInterface; +use Redirect; use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory; use Symfony\Component\HttpFoundation\Response; @@ -151,7 +152,7 @@ private function shouldShowPrerenderedPage($request) // prerender if a crawler is detected foreach ($this->crawlerUserAgents as $crawlerUserAgent) { - if (str_contains($userAgent, strtolower($crawlerUserAgent))) { + if (Str::contains($userAgent, strtolower($crawlerUserAgent))) { $isRequestingPrerenderedPage = true; } } @@ -245,7 +246,7 @@ private function isListed($needles, $list) foreach ($list as $pattern) { foreach ($needles as $needle) { - if (str_is($pattern, $needle)) { + if (Str::is($pattern, $needle)) { return true; } } From bdbe612c225e2b4fffcde8a9a4d9358374b6bd5f Mon Sep 17 00:00:00 2001 From: ChinSheng Date: Tue, 3 Mar 2020 00:13:15 +0800 Subject: [PATCH 2/2] Replaced request data with request uri --- src/PrerenderMiddleware.php | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/PrerenderMiddleware.php b/src/PrerenderMiddleware.php index 475fa69..6b68d37 100644 --- a/src/PrerenderMiddleware.php +++ b/src/PrerenderMiddleware.php @@ -197,17 +197,10 @@ private function getPrerenderedPageResponse($request) $headers['X-Prerender-Token'] = $this->prerenderToken; } - $protocol = $request->isSecure() ? 'https' : 'http'; - try { // Return the Guzzle Response - $host = $request->getHost(); - $path = $request->Path(); - // Fix "//" 404 error - if ($path == "/") { - $path = ""; - } - return $this->client->get($this->prerenderUri . '/' . urlencode($protocol.'://'.$host.'/'.$path), compact('headers')); + $uri = $request->getUri(); + return $this->client->get($this->prerenderUri . '/' . urlencode($uri), compact('headers')); } catch (RequestException $exception) { if(!$this->returnSoftHttpCodes && !empty($exception->getResponse()) && $exception->getResponse()->getStatusCode() == 404) { \App::abort(404);