╭ changed ──╮ │src/web.zig│ ╰───────────╯ ╭───────────╮ │← all files│ ╰───────────╯ ╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ 98 const content = try ui.generateHtml(allocator, &root); │ │ 99 defer allocator.free(content); │ │100 │ │101 - const needle = "{{{ HAXY_CONTENT }}}"; │ │102 - const index = std.mem.indexOf(u8, template, needle) orelse return error.MissingTemplateToken; │ │101 + // serialize the page so the wasm side can parse it back without making │ │102 + // a second request. base64-encoded so the json can be embedded in html. │ │103 + var json: std.Io.Writer.Allocating = .init(allocator); │ │104 + defer json.deinit(); │ │105 + try std.json.Stringify.value(page, .{}, &json.writer); │ │106 │ │107 + const b64 = std.base64.standard.Encoder; │ │108 + const json_b64 = try allocator.alloc(u8, b64.calcSize(json.written().len)); │ │109 + defer allocator.free(json_b64); │ │110 + _ = b64.encode(json_b64, json.written()); │ │111 + │ │112 var out: std.ArrayList(u8) = .empty; │ │113 errdefer out.deinit(allocator); │ │106 - try out.appendSlice(allocator, template[0..index]); │ │107 - try out.appendSlice(allocator, content); │ │108 - try out.appendSlice(allocator, template[index + needle.len ..]); │ │114 + { │ │115 + var cursor: usize = 0; │ │116 + for (&[_]struct { needle: []const u8, replacement: []const u8 }{ │ │117 + .{ .needle = "{{{ HAXY_HTML }}}", .replacement = content }, │ │118 + .{ .needle = "{{{ HAXY_JSON }}}", .replacement = json_b64 }, │ │119 + }) |sub| { │ │120 + const idx = std.mem.indexOfPos(u8, template, cursor, sub.needle) orelse return error.MissingTemplateToken;│ │121 + try out.appendSlice(allocator, template[cursor..idx]); │ │122 + try out.appendSlice(allocator, sub.replacement); │ │123 + cursor = idx + sub.needle.len; │ │124 + } │ │125 + try out.appendSlice(allocator, template[cursor..]); │ │126 + } │ │127 return try out.toOwnedSlice(allocator); │ │128 } │ │129 │ ╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯