From 3022db994ba816bba7282eef003d99e60184fd9b Mon Sep 17 00:00:00 2001 From: Matt Purland Date: Mon, 13 Feb 2023 13:51:25 +0000 Subject: [PATCH 1/2] Implemented SSR --- src/functions.php | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/src/functions.php b/src/functions.php index 52c705d..331529f 100644 --- a/src/functions.php +++ b/src/functions.php @@ -13,14 +13,43 @@ function bb_inject_inertia(string $id = 'app', string $classes = '') ? 'class="' . $classes . '"' : ''; + $page = json_encode($bb_inertia_page); + $content = ''; + + if( + // Not an AJAX request + !(defined('DOING_AJAX') && DOING_AJAX) + + // SSR entry point exists + && file_exists(get_template_directory().'/build/ssr/ssr.js')) + { + // Try to connect to the SSR server + try { + $curl = curl_init('http://127.0.0.1:13714/render'); + + curl_setopt($curl, CURLOPT_POST, true); + curl_setopt($curl, CURLOPT_POSTFIELDS, $page); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + + $content = json_decode(curl_exec($curl)); + + curl_close($curl); + + echo $content->body; + return; + } catch (Exception $e) { + throw new Exception("Couldn't contact the SSR server", $e->getMessage()); + } + } + $page = htmlspecialchars( - json_encode($bb_inertia_page), + $page, ENT_QUOTES, 'UTF-8', true - ); + ); - echo "
"; + echo "
$content
"; } } @@ -37,4 +66,4 @@ function getallheaders() return $headers; } -} +} \ No newline at end of file From 2868eb76ad755cc4247a9411e431d2d2c274213a Mon Sep 17 00:00:00 2001 From: Matt Purland Date: Mon, 13 Feb 2023 14:30:04 +0000 Subject: [PATCH 2/2] Forgot to test a page load with the SSR server turned off --- src/functions.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/functions.php b/src/functions.php index 331529f..6541e06 100644 --- a/src/functions.php +++ b/src/functions.php @@ -35,8 +35,10 @@ function bb_inject_inertia(string $id = 'app', string $classes = '') curl_close($curl); - echo $content->body; - return; + if($content) { + echo $content->body; + return; + } } catch (Exception $e) { throw new Exception("Couldn't contact the SSR server", $e->getMessage()); }