Skip to content

Commit 3af0c91

Browse files
committed
check page module_map before fetching
1 parent b086337 commit 3af0c91

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/browser/browser.zig

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ pub const Page = struct {
241241

242242
scope: *Env.Scope,
243243

244+
// List of modules currently fetched/loaded.
245+
module_map: std.StringHashMapUnmanaged([]const u8),
246+
244247
// current_script is the script currently evaluated by the page.
245248
// current_script could by fetch module to resolve module's url to fetch.
246249
current_script: ?*const Script = null,
@@ -267,6 +270,7 @@ pub const Page = struct {
267270
.http_client = browser.http_client,
268271
},
269272
.scope = try session.executor.startScope(&self.window, &self.state, self, true),
273+
.module_map = .empty,
270274
};
271275

272276
// load polyfills
@@ -298,10 +302,23 @@ pub const Page = struct {
298302
const self: *Page = @ptrCast(@alignCast(ctx));
299303

300304
log.debug("fetch module: specifier: {s}", .{specifier});
301-
return try self.fetchData(
302-
specifier,
303-
if (self.current_script) |s| s.src else null,
304-
);
305+
306+
const base = if (self.current_script) |s| s.src else null;
307+
308+
const file_src = blk: {
309+
if (base) |_base| {
310+
break :blk try URL.stitch(self.arena, specifier, _base);
311+
} else break :blk specifier;
312+
};
313+
314+
const slot = try self.module_map.getOrPut(self.arena, file_src);
315+
if (slot.found_existing) {
316+
return slot.value_ptr.*;
317+
} else {
318+
const module = try self.fetchData(specifier, base);
319+
if (module) |_module| slot.value_ptr.* = _module;
320+
return module;
321+
}
305322
}
306323

307324
pub fn wait(self: *Page) !void {

0 commit comments

Comments
 (0)