diff --git a/src/HTMLScanner.zig b/src/HTMLScanner.zig index 4a69da3574..ad93c49d0b 100644 --- a/src/HTMLScanner.zig +++ b/src/HTMLScanner.zig @@ -79,7 +79,11 @@ pub fn scan(this: *HTMLScanner, input: []const u8) !void { try processor.run(this, input); } -pub fn HTMLProcessor(comptime T: type, comptime visit_head_and_body: bool) type { +pub fn HTMLProcessor( + comptime T: type, + /// If the visitor should visit html, head, body + comptime visit_document_tags: bool, +) type { return struct { const TagHandler = struct { /// CSS selector to match elements @@ -151,12 +155,6 @@ pub fn HTMLProcessor(comptime T: type, comptime visit_head_and_body: bool) type .url_attribute = "href", .kind = .url, }, - // Catch-all for other links with href - .{ - .selector = "link:not([rel~='stylesheet']):not([rel~='modulepreload']):not([rel~='manifest']):not([rel~='icon']):not([rel~='apple-touch-icon'])[href]", - .url_attribute = "href", - .kind = .url, - }, // Images with src .{ .selector = "img[src]", @@ -231,7 +229,7 @@ pub fn HTMLProcessor(comptime T: type, comptime visit_head_and_body: bool) type var builder = lol.HTMLRewriter.Builder.init(); defer builder.deinit(); - var selectors: std.BoundedArray(*lol.HTMLSelector, tag_handlers.len + if (visit_head_and_body) 2 else 0) = .{}; + var selectors: std.BoundedArray(*lol.HTMLSelector, tag_handlers.len + if (visit_document_tags) 3 else 0) = .{}; defer for (selectors.slice()) |selector| { selector.deinit(); }; @@ -254,36 +252,23 @@ pub fn HTMLProcessor(comptime T: type, comptime visit_head_and_body: bool) type ); } - if (visit_head_and_body) { - const head_selector = try lol.HTMLSelector.parse("head"); - selectors.appendAssumeCapacity(head_selector); - try builder.addElementContentHandlers( - head_selector, - T, - T.onHeadTag, - this, - void, - null, - null, - void, - null, - null, - ); - - const body_selector = try lol.HTMLSelector.parse("body"); - selectors.appendAssumeCapacity(body_selector); - try builder.addElementContentHandlers( - body_selector, - T, - T.onBodyTag, - this, - void, - null, - null, - void, - null, - null, - ); + if (visit_document_tags) { + inline for (.{ "body", "head", "html" }, &.{ T.onBodyTag, T.onHeadTag, T.onHtmlTag }) |tag, cb| { + const head_selector = try lol.HTMLSelector.parse(tag); + selectors.appendAssumeCapacity(head_selector); + try builder.addElementContentHandlers( + head_selector, + T, + cb, + this, + void, + null, + null, + void, + null, + null, + ); + } } const memory_settings = lol.MemorySettings{ diff --git a/src/bake/DevServer.zig b/src/bake/DevServer.zig index ece4835cd1..78a268397c 100644 --- a/src/bake/DevServer.zig +++ b/src/bake/DevServer.zig @@ -253,10 +253,7 @@ pub const RouteBundle = struct { bundled_file: IncrementalGraph(.client).FileIndex, /// Invalidated when the HTML file is modified, but not it's imports. /// The style tag is injected here. - head_end_tag_index: ByteOffset.Optional, - /// Invalidated when the HTML file is modified, but not it's imports. - /// The script tag is injected here. - body_end_tag_index: ByteOffset.Optional, + script_injection_offset: ByteOffset.Optional, /// The HTML file bundled, from the bundler. bundled_html_text: ?[]const u8, /// Derived from `bundled_html_text` + `client_script_generation` @@ -1421,12 +1418,11 @@ fn generateHTMLPayload(dev: *DevServer, route_bundle_index: RouteBundle.Index, r assert(route_bundle.server_state == .loaded); // if not loaded, following values wont be initialized assert(html.html_bundle.dev_server_id.unwrap() == route_bundle_index); assert(html.cached_response == null); - const head_end_tag_index = (html.head_end_tag_index.unwrap() orelse unreachable).get(); - const body_end_tag_index = (html.body_end_tag_index.unwrap() orelse unreachable).get(); + const script_injection_offset = (html.script_injection_offset.unwrap() orelse unreachable).get(); const bundled_html = html.bundled_html_text orelse unreachable; - // The bundler records two offsets in development mode, splitting the HTML - // file into three chunks. DevServer is able to insert style/script tags + // The bundler records an offsets in development mode, splitting the HTML + // file into two chunks. DevServer is able to insert style/script tags // using the information available in IncrementalGraph. This approach // allows downstream files to update without re-bundling the HTML file. // @@ -1434,14 +1430,13 @@ fn generateHTMLPayload(dev: *DevServer, route_bundle_index: RouteBundle.Index, r // //
//