Skip to content

Commit 7e7654d

Browse files
committed
browser: write a cahe of failed js script
1 parent a81e10f commit 7e7654d

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ zig-out
55
/vendor/netsurf/lib/
66
/vendor/netsurf/include/
77
/vendor/libiconv/
8+
/*.cache

src/browser/browser.zig

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const std = @import("std");
2+
const builtin = @import("builtin");
23

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

@@ -430,6 +431,13 @@ pub const Page = struct {
430431
if (res.success) {
431432
log.debug("eval remote {s}: {s}", .{ src, res.result });
432433
} else {
434+
// In debug mode only, save the file in a temp file.
435+
if (comptime builtin.mode == .Debug) {
436+
writeCache(alloc, u.path, fetchres.body.?) catch |e| {
437+
log.debug("cache: {any}", .{e});
438+
};
439+
}
440+
433441
log.info("eval remote {s}: {s}", .{ src, res.result });
434442
return FetchError.JsErr;
435443
}
@@ -448,3 +456,22 @@ pub const Page = struct {
448456
return false;
449457
}
450458
};
459+
460+
fn writeCache(alloc: std.mem.Allocator, name: []const u8, data: []const u8) !void {
461+
const fname = try std.mem.concat(alloc, u8, &.{ name, ".cache" });
462+
defer alloc.free(fname);
463+
464+
// clear invalid char.
465+
for (fname, 0..) |c, i| {
466+
if (!std.ascii.isPrint(c) or std.ascii.isWhitespace(c) or c == '/') {
467+
fname[i] = '_';
468+
}
469+
}
470+
471+
log.debug("cache {s}", .{fname});
472+
473+
const f = try std.fs.cwd().createFile(fname, .{ .read = false, .truncate = true });
474+
defer f.close();
475+
476+
try f.writeAll(data);
477+
}

0 commit comments

Comments
 (0)