Skip to content

Commit 301a1c6

Browse files
committed
browser: write a cache file of failed fetched js
1 parent a81e10f commit 301a1c6

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-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: 29 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,24 @@ pub const Page = struct {
448456
return false;
449457
}
450458
};
459+
460+
// writeCache write a cache file in the current dir with the given data.
461+
// Alloc is used to create a temp filename, cleared before returning.
462+
fn writeCache(alloc: std.mem.Allocator, name: []const u8, data: []const u8) !void {
463+
const fname = try std.mem.concat(alloc, u8, &.{ name, ".cache" });
464+
defer alloc.free(fname);
465+
466+
// clear invalid char.
467+
for (fname, 0..) |c, i| {
468+
if (!std.ascii.isPrint(c) or std.ascii.isWhitespace(c) or c == '/') {
469+
fname[i] = '_';
470+
}
471+
}
472+
473+
log.debug("cache {s}", .{fname});
474+
475+
const f = try std.fs.cwd().createFile(fname, .{ .read = false, .truncate = true });
476+
defer f.close();
477+
478+
try f.writeAll(data);
479+
}

0 commit comments

Comments
 (0)