Skip to content

Commit a01d18a

Browse files
committed
Fix module caching
In #798 module caching was added. This was necessary as the same module loaded multiple time should result in the same v8 module instance. To make this work, modules became cached by their full URL. The full URL of one module was also used to determine the full URL of nested modules (full url + specifier). With inline scripts, the page URL was used as the full URL. While this is correct when resolving nested modules, it's incorrect for caching the module itself. Two inline modules on a page share the same URL, but they aren't the same and should be cached. To fix this, inline modules still inherit the page URL, in order to resolve the correct URL for nested modules, but are themselves never cached.
1 parent bdb2338 commit a01d18a

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/browser/page.zig

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -986,12 +986,19 @@ const Script = struct {
986986
defer try_catch.deinit();
987987

988988
const src = self.src orelse page.url.raw;
989+
// if self.src is null, then this is an inline script, and it should
990+
// not be cached.
991+
const cacheable = self.src != null;
989992

990-
log.debug(.browser, "executing script", .{ .src = src, .kind = self.kind });
993+
log.debug(.browser, "executing script", .{
994+
.src = src,
995+
.kind = self.kind,
996+
.cacheable = cacheable,
997+
});
991998

992999
const result = switch (self.kind) {
9931000
.javascript => page.main_context.eval(body, src),
994-
.module => page.main_context.module(body, src),
1001+
.module => page.main_context.module(body, src, cacheable),
9951002
};
9961003

9971004
result catch {
@@ -1003,6 +1010,7 @@ const Script = struct {
10031010
log.warn(.user_script, "eval script", .{
10041011
.src = src,
10051012
.err = msg,
1013+
.cacheable = cacheable,
10061014
});
10071015
}
10081016

src/runtime/js.zig

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,11 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
691691

692692
// compile and eval a JS module
693693
// It doesn't wait for callbacks execution
694-
pub fn module(self: *JsContext, src: []const u8, url: []const u8) !void {
694+
pub fn module(self: *JsContext, src: []const u8, url: []const u8, cacheable: bool) !void {
695+
if (!cacheable) {
696+
return self.moduleNoCache(src, url);
697+
}
698+
695699
const arena = self.context_arena;
696700

697701
const gop = try self.module_cache.getOrPut(arena, url);
@@ -718,6 +722,16 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
718722
_ = try m.evaluate(v8_context);
719723
}
720724

725+
fn moduleNoCache(self: *JsContext, src: []const u8, url: []const u8) !void {
726+
const m = try compileModule(self.isolate, src, url);
727+
const v8_context = self.v8_context;
728+
if (try m.instantiate(v8_context, resolveModuleCallback) == false) {
729+
return error.ModuleInstantiationError;
730+
}
731+
732+
_ = try m.evaluate(v8_context);
733+
}
734+
721735
// Wrap a v8.Exception
722736
fn createException(self: *const JsContext, e: v8.Value) Exception {
723737
return .{

0 commit comments

Comments
 (0)