44
55use Closure ;
66use GuzzleHttp \Client as Guzzle ;
7+ use GuzzleHttp \Exception \ConnectException ;
78use GuzzleHttp \Exception \RequestException ;
89use Illuminate \Http \Request ;
910use Illuminate \Support \Arr ;
@@ -71,15 +72,15 @@ public function __construct(Guzzle $client)
7172 {
7273 $ this ->returnSoftHttpCodes = config ('prerender.prerender_soft_http_codes ' );
7374
74- if ($ this ->returnSoftHttpCodes ) {
75- $ this ->client = $ client ;
76- } else {
77- // Workaround to avoid following redirects
78- $ config = $ client ->getConfig ();
79- $ config ['allow_redirects ' ] = false ;
80- $ this ->client = new Guzzle ($ config );
75+ $ guzzleConfig = $ client ->getConfig ();
76+ $ guzzleConfig ['timeout ' ] = config ('prerender.timeout ' );
77+
78+ if (!$ this ->returnSoftHttpCodes ) {
79+ $ guzzleConfig ['allow_redirects ' ] = false ;
8180 }
8281
82+ $ this ->client = new Guzzle ($ guzzleConfig );
83+
8384 $ config = config ('prerender ' );
8485
8586 $ this ->prerenderUri = $ config ['prerender_url ' ];
@@ -102,7 +103,7 @@ public function handle(Request $request, Closure $next)
102103 if ($ prerenderedResponse ) {
103104 $ statusCode = $ prerenderedResponse ->getStatusCode ();
104105
105- if (! $ this ->returnSoftHttpCodes && $ statusCode >= 300 && $ statusCode < 400 ) {
106+ if (!$ this ->returnSoftHttpCodes && $ statusCode >= 300 && $ statusCode < 400 ) {
106107 $ headers = $ prerenderedResponse ->getHeaders ();
107108
108109 return Redirect::to (array_change_key_case ($ headers , CASE_LOWER )['location ' ][0 ], $ statusCode );
@@ -128,11 +129,11 @@ private function shouldShowPrerenderedPage(Request $request): bool
128129
129130 $ isRequestingPrerenderedPage = false ;
130131
131- if (! $ userAgent ) {
132+ if (!$ userAgent ) {
132133 return false ;
133134 }
134135
135- if (! $ request ->isMethod ('GET ' )) {
136+ if (!$ request ->isMethod ('GET ' )) {
136137 return false ;
137138 }
138139
@@ -152,13 +153,13 @@ private function shouldShowPrerenderedPage(Request $request): bool
152153 $ isRequestingPrerenderedPage = true ;
153154 }
154155
155- if (! $ isRequestingPrerenderedPage ) {
156+ if (!$ isRequestingPrerenderedPage ) {
156157 return false ;
157158 }
158159
159160 // only check whitelist if it is not empty
160161 if ($ this ->whitelist ) {
161- if (! $ this ->isListed ($ requestUri , $ this ->whitelist )) {
162+ if (!$ this ->isListed ($ requestUri , $ this ->whitelist )) {
162163 return false ;
163164 }
164165 }
@@ -207,19 +208,21 @@ private function getPrerenderedPageResponse(Request $request): ?ResponseInterfac
207208
208209 return $ this ->client ->get ($ this ->prerenderUri .'/ ' .urlencode ($ protocol .':// ' .$ host .'/ ' .$ path ), compact ('headers ' ));
209210 } catch (RequestException $ exception ) {
210- if (! $ this ->returnSoftHttpCodes && ! empty ($ exception ->getResponse ()) && $ exception ->getResponse ()->getStatusCode () === 404 ) {
211+ if (!$ this ->returnSoftHttpCodes && !empty ($ exception ->getResponse ()) && $ exception ->getResponse ()->getStatusCode () === 404 ) {
211212 abort (404 );
212213 }
214+ } catch (ConnectException $ exception ) {
215+ //
216+ }
213217
214- // In case of an exception, we only throw the exception if we are in debug mode. Otherwise,
215- // we return null and the handle() method will just pass the request to the next middleware
216- // and we do not show a prerendered page.
217- if (config ('app.debug ' )) {
218- throw $ exception ;
219- }
220-
221- return null ;
218+ // In case of an exception, we only throw the exception if we are in debug mode. Otherwise,
219+ // we return null and the handle() method will just pass the request to the next middleware
220+ // and we do not show a prerendered page.
221+ if (config ('app.debug ' )) {
222+ throw $ exception ;
222223 }
224+
225+ return null ;
223226 }
224227
225228 /**
0 commit comments