-
Notifications
You must be signed in to change notification settings - Fork 0
/
fasthttp.go
37 lines (29 loc) · 1.03 KB
/
fasthttp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main
import (
"fmt"
prerendercloud "github.com/sanfrancesco/prerendercloud-golang"
"github.com/valyala/fasthttp"
)
func main() {
// set the PRERENDER_TOKEN env var when starting this golang binary/executable
prerenderCloudOptions := prerendercloud.NewOptions()
// not recommended, but if you must, uncomment this to
// restrict prerendering to bots and the _escaped_fragment_ query param
// prerenderCloudOptions.BotsOnly = true
// prerendercloud.CrawlerUserAgents = append(prerendercloud.CrawlerUserAgents, "googlebot")
prerenderCloud := prerenderCloudOptions.NewPrerender()
requestHandler := func(ctx *fasthttp.RequestCtx) {
if prerenderCloud.ShouldPrerenderFastHttp(ctx) && prerenderCloud.PreRenderHandlerFastHttp(ctx) == nil {
return
} else {
ctx.SetContentType("text/html")
fmt.Fprintf(ctx, `
<div id='root'></div>
<script type='text/javascript'>
document.getElementById('root').innerHTML = "hello";
</script>
`)
}
}
fasthttp.ListenAndServe(":8080", requestHandler)
}