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,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