-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add redirect filter for React deeplinks to workaround 404 errors…
… on reload
- Loading branch information
Showing
2 changed files
with
37 additions
and
1 deletion.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
backend/src/main/kotlin/org/quizmania/rest/adapter/in/rest/RedirectToIndexFilter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.quizmania.rest.adapter.`in`.rest | ||
|
||
import jakarta.servlet.Filter | ||
import jakarta.servlet.FilterChain | ||
import jakarta.servlet.ServletRequest | ||
import jakarta.servlet.ServletResponse | ||
import jakarta.servlet.http.HttpServletRequest | ||
import mu.KLogging | ||
import org.springframework.stereotype.Component | ||
|
||
/** | ||
* Redirect all deep links used in react app to index page to no get 404 on reload | ||
*/ | ||
@Component | ||
class RedirectToIndexFilter : Filter { | ||
|
||
companion object : KLogging() | ||
|
||
override fun doFilter( | ||
request: ServletRequest, | ||
response: ServletResponse?, | ||
chain: FilterChain | ||
) { | ||
val req = request as HttpServletRequest | ||
val requestURI = req.requestURI | ||
|
||
if (requestURI.startsWith("/game") || requestURI.startsWith("/login") || requestURI.startsWith("/logout")) { | ||
// all requests not api or static will be forwarded to index page. | ||
logger.debug { "Forwarding $requestURI to index page" } | ||
request.getRequestDispatcher("/").forward(request, response) | ||
return | ||
} | ||
|
||
chain.doFilter(request, response) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters