Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<div id=\"{$id}\" {$classes} data-page=\"{$page}\"></div>";
echo "<div id=\"{$id}\" {$classes} data-page=\"{$page}\">$content</div>";
}
}

Expand All @@ -37,4 +68,4 @@ function getallheaders()

return $headers;
}
}
}