From 3d4d77bc739b26850fd32e363aa907d562d8a7b5 Mon Sep 17 00:00:00 2001 From: Teriuihi Date: Sat, 19 Apr 2025 01:07:49 +0200 Subject: [PATCH] Add WebConfig for static resource handling Introduced WebConfig to configure resource handling in the backend. This ensures SPA fallback by serving `index.html` for non-existent paths, improving routing for client-side applications. --- .../alttd/altitudeweb/config/WebConfig.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 backend/src/main/java/com/alttd/altitudeweb/config/WebConfig.java diff --git a/backend/src/main/java/com/alttd/altitudeweb/config/WebConfig.java b/backend/src/main/java/com/alttd/altitudeweb/config/WebConfig.java new file mode 100644 index 0000000..cf01d79 --- /dev/null +++ b/backend/src/main/java/com/alttd/altitudeweb/config/WebConfig.java @@ -0,0 +1,33 @@ +package com.alttd.altitudeweb.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.springframework.web.servlet.resource.PathResourceResolver; + +import java.io.IOException; + +@Configuration +public class WebConfig implements WebMvcConfigurer { + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/**") + .addResourceLocations("classpath:/static/") + .resourceChain(true) + .addResolver(new PathResourceResolver() { + @Override + protected Resource getResource(String resourcePath, Resource location) throws IOException { + Resource requestedResource = location.createRelative(resourcePath); + + if (requestedResource.exists() && requestedResource.isReadable()) { + return requestedResource; + } + + return new ClassPathResource("/static/index.html"); + } + }); + } +}