This commit is contained in:
Jorijn van der Graaf 2025-11-14 23:56:39 +01:00
commit e3bc669118
7 changed files with 46 additions and 17 deletions

6
favicon.svg Normal file
View file

@ -0,0 +1,6 @@
<!-- save this as emoji-favicon.svg -->
<svg xmlns="http://www.w3.org/2000/svg" width="64" height="64">
<text x="50%" y="50%" font-size="48" text-anchor="middle" dominant-baseline="central">
🐱
</text>
</svg>

After

Width:  |  Height:  |  Size: 219 B

View file

@ -15,13 +15,13 @@ import std;
using namespace Crafter::CppDOMBindings; using namespace Crafter::CppDOMBindings;
export namespace Catcrafts { export namespace Catcrafts {
std::vector<HtmlElementView>* blogButtons = new std::vector<HtmlElementView>(); std::vector<HtmlElementView> blogButtons;
void RenderBlog() { void RenderBlog() {
delete blogButtons; blogButtons.clear();
blogButtons = new std::vector<HtmlElementView>();
std::string html = ""; std::string html = "";
for(const BlogPost& post : *posts) { for(const BlogPost& post : posts) {
std::cout << "test" << std::endl;
// For preview, we'll limit the content to first 200 characters // For preview, we'll limit the content to first 200 characters
std::string previewContent = post.content; std::string previewContent = post.content;
if(previewContent.length() > 200) { if(previewContent.length() > 200) {
@ -47,11 +47,11 @@ export namespace Catcrafts {
</div> </div>
</div>)", post.slug, post.name, post.date, previewContent); </div>)", post.slug, post.name, post.date, previewContent);
} }
main->SetInnerHTML(html); main.SetInnerHTML(std::format(R"(<div class="blog-posts">{}</div>)", html));
for(const BlogPost& post : *posts) { for(const BlogPost& post : posts) {
// Add click listener to the entire post card // Add click listener to the entire post card
HtmlElementView& cardView = blogButtons->emplace_back(std::format("blog-post-{}", post.slug)); HtmlElementView& cardView = blogButtons.emplace_back(std::format("blog-post-{}", post.slug));
cardView.AddClickListener([slug = post.slug](Crafter::MouseEvent e) { cardView.AddClickListener([slug = post.slug](Crafter::MouseEvent e) {
PushState("{}", "", std::format("/blog/{}", slug)); PushState("{}", "", std::format("/blog/{}", slug));
RenderRoot(std::format("/blog/{}", slug)); RenderRoot(std::format("/blog/{}", slug));
@ -62,9 +62,9 @@ export namespace Catcrafts {
void RenderBlogPost(const std::string_view slug) { void RenderBlogPost(const std::string_view slug) {
std::cout << "render "<< std::endl; std::cout << "render "<< std::endl;
for(const BlogPost& post : *posts) { for(const BlogPost& post : posts) {
if(post.slug == slug) { if(post.slug == slug) {
main->SetInnerHTML(std::format(R"( main.SetInnerHTML(std::format(R"(
<div class="blog-post-page"> <div class="blog-post-page">
<div class="post-header"> <div class="post-header">
<h1 class="post-title">{}</h1> <h1 class="post-title">{}</h1>
@ -78,6 +78,6 @@ export namespace Catcrafts {
} }
} }
main->SetInnerHTML("<h1>Post Not Found</h1><p>The requested blog post could not be found.</p>"); main.SetInnerHTML("<h1>Post Not Found</h1><p>The requested blog post could not be found.</p>");
} }
} }

View file

@ -29,7 +29,7 @@ namespace Catcrafts {
std::cout << postSlug << std::endl; std::cout << postSlug << std::endl;
RenderBlogPost(postSlug); RenderBlogPost(postSlug);
} else { } else {
main->SetInnerHTML("<h1>Post Not Found</h1><p>The requested blog post could not be found.</p>"); main.SetInnerHTML("<h1>Post Not Found</h1><p>The requested blog post could not be found.</p>");
} }
} else { } else {
RenderBlog(); //default route RenderBlog(); //default route

View file

@ -18,7 +18,25 @@ export namespace Catcrafts {
std::string date; std::string date;
std::string content; std::string content;
}; };
std::vector<BlogPost>* posts = new std::vector<BlogPost>{ std::vector<BlogPost> posts {
{
"In WASM, Exit doesn't mean done.",
"in-wasm-exit-doesnt-mean-done",
"2025-11-14",
R"(So if anyone looked at the source code for this website pefore this post you would have seen that every is allocated with new, for example this very blog was defined as <code>std::vector<BlogPost> posts = new std::vector<BlogPost>{...</code><br><br>
Reason for this was that everything became corrupted when callbacking from JS, not knowing sure as to why this was the fastest solution, but after debugging the problem became clear.<br><br>
When we reach the end of <code>int main()</code> from the eyes of C++ we're finished, deconstruct everything and wrap it up. Unkowing that we just registered a bunch of event handlers with JS.<br><br>
This caused all the memory corruption errors since everything was destructed.<br><br>
Luckily this is a very simple fix of adding <code>-fno-c++-static-destructors</code>, and things like this are is also the reason i use clang instead of gcc.<br><br>
So now all examples and this site have been updated to use normal variables again.<br><br>
Stick around for the next post for the <strong>CI/CD nightmare</strong> (not for the faint of heart))"
},
{ {
"Hello World!", "Hello World!",
"hello-world", "hello-world",

View file

@ -11,7 +11,7 @@ import Crafter.CppDOM;
using namespace Crafter; using namespace Crafter;
export namespace Catcrafts { export namespace Catcrafts {
HtmlElementView* body = new HtmlElementView("body", R"( HtmlElementPtr body("body", R"(
<header> <header>
<div class="nav-container"> <div class="nav-container">
<a href="/" class="logo">🐱 Catcrafts</a> <a href="/" class="logo">🐱 Catcrafts</a>
@ -36,12 +36,13 @@ export namespace Catcrafts {
</div> </div>
</footer>)"); </footer>)");
HtmlElementView* head = new HtmlElementView("head", R"( HtmlElementPtr head("head", R"(
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Catcrafts.net</title> <title>Catcrafts.net</title>
<link rel="stylesheet" href="/styles.css"> <link rel="stylesheet" href="/styles.css">
<link rel="icon" href="/favicon.svg" type="image/svg+xml">
)"); )");
HtmlElementView* main = new HtmlElementView("main"); HtmlElementPtr main("main");
} }

View file

@ -6,7 +6,7 @@
"interfaces": ["interfaces/Catcrafts", "interfaces/Catcrafts-Views", "interfaces/Catcrafts-Blog", "interfaces/Catcrafts-Root"], "interfaces": ["interfaces/Catcrafts", "interfaces/Catcrafts-Views", "interfaces/Catcrafts-Blog", "interfaces/Catcrafts-Root"],
"implementations": ["implementations/main", "implementations/Catcrafts-Blog", "implementations/Catcrafts-Root"], "implementations": ["implementations/main", "implementations/Catcrafts-Blog", "implementations/Catcrafts-Root"],
"target": "wasm32-wasi", "target": "wasm32-wasi",
"additional_files": ["styles/styles.css", "robots.txt", "sitemap.xml"], "additional_files": ["styles/styles.css", "robots.txt", "sitemap.xml", "favicon.svg"],
"dependencies": [ "dependencies": [
{ {
"path":"https://forgejo.catcrafts.net/Catcrafts/Crafter.CppDOM.git", "path":"https://forgejo.catcrafts.net/Catcrafts/Crafter.CppDOM.git",

View file

@ -1,4 +1,5 @@
<urlset> <?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url> <url>
<loc>https://catcrafts.net/</loc> <loc>https://catcrafts.net/</loc>
</url> </url>
@ -8,4 +9,7 @@
<url> <url>
<loc>https://catcrafts.net/blog/hello-world</loc> <loc>https://catcrafts.net/blog/hello-world</loc>
</url> </url>
<url>
<loc>https://catcrafts.net/blog/in-wasm-exit-doesnt-mean-done</loc>
</url>
</urlset> </urlset>