mirror of https://github.com/usbharu/Hideout.git
feat: クエリパラメータでSSRを使用するかを切り替えれるように
SSRを使用するかの設定はセッショに保存され、次のアクセスから設定する必要はなくなる サーバーはとりあえずSPA用のindexページを送信し、noscriptを検知してクエリパラメータ付きのページに遷移する
This commit is contained in:
parent
fec59ab622
commit
4f9c431d9b
|
@ -0,0 +1,13 @@
|
|||
package dev.usbharu.hideout.core.config
|
||||
|
||||
import dev.usbharu.hideout.core.infrastructure.springframework.SPAInterceptor
|
||||
import org.springframework.context.annotation.Configuration
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
|
||||
|
||||
@Configuration
|
||||
class WebMvcConfig(private val spaInterceptor: SPAInterceptor) : WebMvcConfigurer {
|
||||
override fun addInterceptors(registry: InterceptorRegistry) {
|
||||
registry.addInterceptor(spaInterceptor)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package dev.usbharu.hideout.core.infrastructure.springframework
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest
|
||||
import jakarta.servlet.http.HttpServletResponse
|
||||
import org.springframework.stereotype.Component
|
||||
import org.springframework.web.servlet.HandlerInterceptor
|
||||
import org.springframework.web.servlet.ModelAndView
|
||||
import org.springframework.web.servlet.support.ServletUriComponentsBuilder
|
||||
|
||||
@Component
|
||||
class SPAInterceptor : HandlerInterceptor {
|
||||
|
||||
override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any): Boolean {
|
||||
|
||||
if (request.getParameter("s") == "f") {
|
||||
request.session.setAttribute("s", "f")
|
||||
} else if (request.getParameter("s") == "t") {
|
||||
request.session.setAttribute("s", "t")
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
override fun postHandle(
|
||||
request: HttpServletRequest,
|
||||
response: HttpServletResponse,
|
||||
handler: Any,
|
||||
modelAndView: ModelAndView?
|
||||
) {
|
||||
if (request.session.getAttribute("s") == "f") {
|
||||
return
|
||||
}
|
||||
val title = modelAndView?.modelMap?.getOrDefault("title", "")
|
||||
val url = modelAndView?.modelMap?.getOrDefault(
|
||||
"url",
|
||||
ServletUriComponentsBuilder.fromCurrentRequestUri().toUriString() + '?' + request.queryString
|
||||
)
|
||||
val description = modelAndView?.modelMap?.getOrDefault("description", "")
|
||||
val image = modelAndView?.modelMap?.get("image")
|
||||
|
||||
modelAndView?.clear()
|
||||
modelAndView?.addObject("nsUrl", request.requestURI + "?s=f" + request.queryString?.let { "&$it" }.orEmpty())
|
||||
modelAndView?.addAllObjects(mapOf("title" to title, "url" to url, "description" to description))
|
||||
image?.let { modelAndView?.addObject("image", it) }
|
||||
modelAndView?.viewName = "index"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en" xmlns:th="http://www.thymeleaf.org">
|
||||
<head prefix="og: http://ogp.me/ns#">
|
||||
<meta charset="UTF-8">
|
||||
<title th:title="${title}">Title</title>
|
||||
<meta property="og:url" th:content="${url}">
|
||||
<meta property="og:title" th:content="${title}">
|
||||
<meta property="og:description" th:content="${description}">
|
||||
<th:block th:if="${image != null}">
|
||||
<meta property="og:image" th:content="${image}">
|
||||
</th:block>
|
||||
</head>
|
||||
<body>
|
||||
<noscript>
|
||||
<a th:href="${nsUrl}">No Script</a>
|
||||
</noscript>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue