11const std = @import ("std" );
2+ const builtin = @import ("builtin" );
23
34const 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