micronaut-guides icon indicating copy to clipboard operation
micronaut-guides copied to clipboard

Native serving of SPAs

Open morki opened this issue 4 years ago • 12 comments

I think we could make serving SPAs simpler as it is common pattern to use Micronaut for API and bundle SPA frontend application as static resource.

My proposed solution is to extend StaticResourceConfiguration class with something like

private boolean spa = false;

and extend StaticResourceResolver class with something like

if (config.isSpa()) {
    for (ResourceLoader loader : loaders) {
        Optional<URL> resource = loader.getResource(INDEX_PAGE);
        if (resource.isPresent()) {
            return resource;
        }
    }
}

under the end of loop finding available resource (for (ResourceLoader loader : loaders)) when no resource is found.

Usage in application.yml would be:

micronaut:
  router:
    static-resources:
      angular-frontend:
        paths: classpath:public/frontend
        mapping: /frontend/**
        spa: true
  1. Would you like me to create PR?
  2. Is naming it spa good? Maybe something like fallback-to-index?

morki avatar Jun 14 '20 18:06 morki