Skip to content

Commit

Permalink
fix: add redirect filter for React deeplinks to workaround 404 errors…
Browse files Browse the repository at this point in the history
… on reload
  • Loading branch information
stoerti committed Sep 24, 2024
1 parent 85b68fc commit a48362e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
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)
}
}
2 changes: 1 addition & 1 deletion frontend/src/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ const routes: RouteObject[] = [
}
]

export const router = createHashRouter(routes);
export const router = createBrowserRouter(routes);

0 comments on commit a48362e

Please sign in to comment.