diff --git a/src/functions.php b/src/functions.php index 52c705d..6541e06 100644 --- a/src/functions.php +++ b/src/functions.php @@ -13,14 +13,45 @@ 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); + + if($content) { + 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 +68,4 @@ function getallheaders() return $headers; } -} +} \ No newline at end of file