Skip to content

Commit

Permalink
Speed up HTML import parsing by ignoring Polymer core files (#4554)
Browse files Browse the repository at this point in the history
Reduces the TTFB for the first time the Beverage Buddy edit dialog is
opened from around 2200 ms to 550 ms.

Related to #4532
  • Loading branch information
Legioth authored and mikotin committed Aug 30, 2018
1 parent f47daba commit 873c736
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Collection<String> parseDependencies(VaadinService service) {

private void parseDependencies(String path, Set<String> dependencies,
VaadinService service) {
if (dependencies.contains(path)) {
if (dependencies.contains(path) || isBlacklisted(path)) {
return;
}
dependencies.add(path);
Expand All @@ -97,6 +97,12 @@ private void parseDependencies(String path, Set<String> dependencies,
}
}

private static boolean isBlacklisted(String path) {
// Speed things up by not parsing files that are known to not use themes
return path.startsWith(ApplicationConstants.FRONTEND_PROTOCOL_PREFIX
+ "bower_components/polymer/");
}

private String resolveUri(String relative, String base) {
if (relative.startsWith("/")) {
return relative;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.net.URI;
import java.util.Collection;
import java.util.Collections;

import org.junit.After;
import org.junit.Assert;
Expand Down Expand Up @@ -176,6 +177,25 @@ public void dependenciesWithDifferentPathsIncludedOnlyOnce() {
dependencies.contains("frontend://relative.html"));
}

@Test
public void polymerDependency_ignored() {
String root = "foo.html";
HtmlDependencyParser parser = new HtmlDependencyParser(root);

String importContent = "<link rel='import' href='bower_components/polymer/polymer-element.html'>";

servlet.addServletContextResource("/frontend/foo.html", importContent);

Collection<String> dependencies = parser.parseDependencies(service);

servlet.verifyServletContextResourceLoadedOnce("/frontend/" + root);
servlet.verifyServletContextResourceNotLoaded(
"/frontend/bower_components/polymer/polymer-element.html");

Assert.assertEquals(Collections.singleton("frontend://" + root),
dependencies);
}

@Test
public void normalizeURI() throws Exception {
assertEquals("http://foo/bar", normalize("http://foo/bar"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ public void verifyServletContextResourceLoadedOnce(String resource) {
.getResourceAsStream(resource);

}

public void verifyServletContextResourceNotLoaded(String resource) {
Mockito.verify(servlet.getServletContext(), Mockito.never())
.getResourceAsStream(resource);
}
}

public static class TestVaadinServletResponse
Expand Down

0 comments on commit 873c736

Please sign in to comment.