I can render the `index.html` when I visit `<http:...
# spring
k
I can render the
index.html
when I visit
<http://localhost:8080/index.html>
but what I would like to do is render the
index.html
when I visit
<http://localhost:8080/>
. Any insight would be helpful. Thanks!
s
Hey, glad you like it 🙂 You can try something like this https://github.com/sdeleuze/spring-kotlin-fullstack/blob/master/backend/src/main/kotlin/backend/Application.kt#L28 as a start.
If you want the redirection not to be visible on client side, I am not sure we support that yet. Let me check.
k
Thank you for the example! I am actually looking for the redirection not to be visible on the client side but if it’s not possible yet, that’s okay
s
Yeah I will have a deeper look and let you know.
k
👌 Thank you sir
s
Oh but wait here I think you just forgot to enable view rendering.
If you use Mustache or Thymeleaf you should configure it explicitly.
with
mustache()
for example.
Without that no view rendering is configured.
Since Spring Fu requires by design explicit configuration.
k
Hmm, if am not using a templating engine like Mustache or Thymeleaf, is the explicit configuration still necessary?
s
Client side rendering?
If client side rendering you should not use
ServerResponse.ok().render("index")
That's server-side rendering
k
Nope, not client side, I’d like to render my views server-side
s
So what engine do you want to use?
k
Sorry, let me provide a bit of context here. I’m building a React app and a Spring app in a single jar, where the React build file (
index.html
) is copied to the spring gradle
build/resources/static
folder. The goal is to use a single spring endpoint to render the react app server side
So maybe a new question, do I need to be using a templating to render the react app server-side?
s
If you are using a React app, it needs to be rendered client side since React is a JS framework. We don't support server-side rendering of React apps unless there is support for that in the community.
So I guess there is some confusion unrelated to Spring Fu here.
It is possible to serve
/index.html
when
/
is requested with redirect views, maybe that's what you try to achieve?
yes black 1
k
Yes that’s something I’d like to do, serve
index.html
when
/
is requested
Sorry for the confusion
s
Ok so not sure we expose that yet, I will discuss that within the team.
k
Wonderful! Again, thanks for answering my questions, really appreciate it and the work on spring-fu
Actually, for what I want to do, I think I got it to work
Using Spring annotations:
Copy code
@Configuration
class UiController {

    @Bean
    fun redirectToUiRoute() = router {
        GET("/") {
            ServerResponse.permanentRedirect(URI("/ui")).build()
        }
        GET("/ui") {
            ServerResponse.ok()
                .contentType(MediaType.TEXT_HTML)
                .body(ClassPathResource("static/index.html"))
        }
    }
}
When I visit, the
/ui
or
/
, it will render the react app server-side
Using Spring-fu:
Copy code
val app: KofuApplication = webApplication {
    webMvc {
        converters {
            resource()
        }

        router {
            GET("/") {
                ServerResponse.permanentRedirect(URI("/ui")).build()
            }
            GET("/ui") {
                ServerResponse.ok()
                    .contentType(MediaType.TEXT_HTML)
                    .body(ClassPathResource("static/index.html"))
            }
        }
    }
}