Skip to content
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ zig-out
/vendor/netsurf/lib/
/vendor/netsurf/include/
/vendor/libiconv/
/*.cache
29 changes: 29 additions & 0 deletions src/browser/browser.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const std = @import("std");
const builtin = @import("builtin");

const Types = @import("root").Types;

Expand Down Expand Up @@ -430,6 +431,13 @@ pub const Page = struct {
if (res.success) {
log.debug("eval remote {s}: {s}", .{ src, res.result });
} else {
// In debug mode only, save the file in a temp file.
if (comptime builtin.mode == .Debug) {
writeCache(alloc, u.path, fetchres.body.?) catch |e| {
log.debug("cache: {any}", .{e});
};
}

log.info("eval remote {s}: {s}", .{ src, res.result });
return FetchError.JsErr;
}
Expand All @@ -448,3 +456,24 @@ pub const Page = struct {
return false;
}
};

// writeCache write a cache file in the current dir with the given data.
// Alloc is used to create a temp filename, cleared before returning.
fn writeCache(alloc: std.mem.Allocator, name: []const u8, data: []const u8) !void {
const fname = try std.mem.concat(alloc, u8, &.{ name, ".cache" });
defer alloc.free(fname);

// clear invalid char.
for (fname, 0..) |c, i| {
if (!std.ascii.isPrint(c) or std.ascii.isWhitespace(c) or c == '/') {
fname[i] = '_';
}
}

log.debug("cache {s}", .{fname});

const f = try std.fs.cwd().createFile(fname, .{ .read = false, .truncate = true });
defer f.close();

try f.writeAll(data);
}