Skip to content

Commit

Permalink
Update ReactController.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
SusanDoggie committed Feb 8, 2022
1 parent d40ab2d commit 13309e6
Showing 1 changed file with 40 additions and 56 deletions.
96 changes: 40 additions & 56 deletions Sources/ReactController/ReactController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,44 @@ extension ReactController {
}
}

extension ReactController {

private func html_template(
_ meta: [String],
_ css: String,
_ html: String,
_ preloadedState: String
) -> String {

return """
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover">
\(meta.joined(separator: "\n"))
<style>
html, body {
height: 100vh;
}
#\(self.root) {
display: flex;
position: fixed;
height: 100%;
}
</style>
\(css)
</head>
<body>
<div id="\(self.root)">\(html)</div>
\(preloadedState)
<script src="\(self.bundle)"></script>
</body>
</html>
"""
}
}

extension ReactController {

private func html(_ req: Request) -> EventLoopFuture<Response> {
Expand Down Expand Up @@ -156,35 +194,7 @@ extension ReactController {
headers.contentType = .html
headers.cacheControl = .init(noCache: true)

let body = Response.Body(string: """
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover">
\(meta_string.joined(separator: "\n"))
<style>
html,
body {
height: 100%;
}
body {
overflow: hidden;
}
#\(self.root) {
display: flex;
height: 100%;
}
</style>
\(css)
</head>
<body>
<div id="\(self.root)">\(html)</div>
\(_preloadedState)
<script src="\(self.bundle)"></script>
</body>
</html>
""")
let body = Response.Body(string: self.html_template(meta_string, css, html, _preloadedState))

return Response(status: .init(statusCode: statusCode), headers: headers, body: body)
}
Expand All @@ -200,33 +210,7 @@ extension ReactController {
headers.contentType = .html
headers.cacheControl = .init(noCache: true)

let body = Response.Body(string: """
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover">
<style>
html,
body {
height: 100%;
}
body {
overflow: hidden;
}
#\(self.root) {
display: flex;
height: 100%;
}
</style>
</head>
<body>
<div id="\(self.root)"></div>
\(_preloadedState)
<script src="\(self.bundle)"></script>
</body>
</html>
""")
let body = Response.Body(string: self.html_template([], "", "", _preloadedState))

return req.eventLoop.makeSucceededFuture(Response(status: .ok, headers: headers, body: body))
}
Expand Down

0 comments on commit 13309e6

Please sign in to comment.