Skip to content

Commit

Permalink
Smart bypass of the dev server SPA
Browse files Browse the repository at this point in the history
  • Loading branch information
ia3andy committed Dec 18, 2023
1 parent cfb32d5 commit 61bf103
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,5 @@ public interface DevServerConfig {
*/
@ConfigDocDefault("auto-detected falling back to the quinoa.index-page")
Optional<String> indexPage();

}
15 changes: 9 additions & 6 deletions integration-tests/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
%root-path.quarkus.http.root-path=/foo/bar
%root-path.quarkus.quinoa.enable-spa-routing=true
%lit-root-path.quarkus.http.root-path=/foo/bar
%lit-root-path.quarkus.quinoa.enable-spa-routing=true
%lit-root-path.quarkus.quinoa.package-manager-command.build-env.ROOT_PATH=/foo/bar/

%react.quarkus.quinoa.ui-dir=src/main/ui-react
%vue.quarkus.quinoa.ui-dir=src/main/ui-vue
Expand All @@ -11,12 +12,14 @@
%angular.quarkus.quinoa.package-manager-install.install-dir=target/
%angular.quarkus.quinoa.package-manager-install.node-version=20.9.0
%angular.quarkus.quinoa.package-manager-install.yarn-version=1.22.19
%lit.quarkus.quinoa.ui-dir=src/main/ui-lit
%lit.quarkus.quinoa.build-dir=dist
%lit.quarkus.quinoa.index-page=app.html
%lit.quarkus.quinoa.package-manager-command.build=run build-per-env
%lit,lit-root-path.quarkus.quinoa.ui-dir=src/main/ui-lit
%lit,lit-root-path.quarkus.quinoa.build-dir=dist
%lit,lit-root-path.quarkus.quinoa.index-page=app.html
%lit,lit-root-path.quarkus.quinoa.package-manager-command.build=run build-per-env
%lit-root-path.quarkus.quinoa.package-manager-command.build-env.FOO=bar
%lit.quarkus.quinoa.package-manager-command.build-env.FOO=bar
%lit.quarkus.quinoa.package-manager-command.build-env.ROOT_PATH=/

%yarn.quarkus.quinoa.package-manager=yarn
%just-build.quarkus.quinoa.just-build=true

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.net.URL;

import com.microsoft.playwright.ElementHandle;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -39,6 +40,11 @@ public void testUIIndex() {

String title = page.title();
Assertions.assertEquals("Quinoa Lit App", title);

// Make sure the component loaded and hits the backend
final ElementHandle quinoaEl = page.waitForSelector(".greeting");
String greeting = quinoaEl.innerText();
Assertions.assertEquals("Hello Quinoa and World and bar", greeting);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public String getConfigProfile() {
public static class RootPathTests extends QuinoaTestProfiles.Enable {
@Override
public String getConfigProfile() {
return "root-path,lit";
return "lit-root-path,lit";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,6 @@ private void handleHttpRequest(final RoutingContext ctx, final String resourcePa
final MultiMap headers = request.headers();
final String uri = computeResourceURI(resourcePath, request);

// Workaround for issue https://github.com/quarkiverse/quarkus-quinoa/issues/91
// See
// https://www.npmjs.com/package/connect-history-api-fallback#htmlacceptheaders
// When no Accept header is provided, the historyApiFallback is disabled
headers.remove("Accept");
// Disable compression in the forwarded request
headers.remove("Accept-Encoding");
client.request(request.method(), port, host, uri)
Expand All @@ -99,7 +94,12 @@ private void handleHttpRequest(final RoutingContext ctx, final String resourcePa
final int statusCode = event.result().statusCode();
switch (statusCode) {
case 200:
forwardResponse(event, request, ctx, resourcePath);
if (shouldForward(ctx, event.result())) {
forwardResponse(event, request, ctx, resourcePath);
} else {
next(currentClassLoader, ctx);
}

break;
case 404:
next(currentClassLoader, ctx);
Expand All @@ -113,6 +113,18 @@ private void handleHttpRequest(final RoutingContext ctx, final String resourcePa
});
}

private boolean shouldForward(RoutingContext ctx, HttpResponse<Buffer> result) {
final List<String> contentType = result.headers().getAll(HttpHeaders.CONTENT_TYPE);
if (contentType != null && contentType.stream().anyMatch(s -> s.contains("text/html"))) {
final String path = QuinoaRecorder.resolvePath(ctx);
// We forward if the server returns a html, and it was intended:
// - if the path ends with .html
// - if the path is empty (root)
return path.endsWith(".html") || path.equals("/") || path.isEmpty();
}
return true;
}

private String computeResourceURI(String path, HttpServerRequest request) {
String uri = path;
final String query = request.query();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass())
return false;
QuinoaHandlerConfig that = (QuinoaHandlerConfig) o;
return Objects.equals(ignoredPathPrefixes, that.ignoredPathPrefixes) && Objects.equals(indexPage, that.indexPage);
return prodMode == that.prodMode && enableCompression == that.enableCompression
&& Objects.equals(ignoredPathPrefixes, that.ignoredPathPrefixes) && Objects.equals(indexPage, that.indexPage)
&& Objects.equals(compressMediaTypes, that.compressMediaTypes);
}

@Override
public int hashCode() {
return Objects.hash(ignoredPathPrefixes, indexPage);
return Objects.hash(ignoredPathPrefixes, indexPage, prodMode, enableCompression, compressMediaTypes);
}
}

0 comments on commit 61bf103

Please sign in to comment.