Skip to content
master
Switch branches/tags
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

image

prerendercloud-golang

CircleCI

Includes negroni middleware, and a fasthttp handler for prerendering javascript web pages/apps (single page apps or SPA) with https://www.prerender.cloud/

Set your API token via env var

(get token after signing up at prerender.cloud)

PRERENDER_TOKEN="mySecretTokenFromPrerenderCloud" go run main.go

Using it in negroni

package main

import (
	"net/http"

	"github.com/codegangsta/negroni"
	prerendercloud "github.com/sanfrancesco/prerendercloud-golang"
)

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
	// with BotsOnly enabled, we don't include googlebot by default (to reduce cloaking penality risk), this is how you could enable it
	// prerendercloud.CrawlerUserAgents = append(prerendercloud.CrawlerUserAgents, "googlebot")

	prerenderCloud := prerenderCloudOptions.NewPrerender()

	n := negroni.New()
	n.Use(negroni.NewLogger())
	n.Use(prerenderCloud)
	n.Use(negroni.NewStatic(http.Dir(".")))
	n.Run(":8080")
}

Using it in fasthttp

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
	// with BotsOnly enabled, we don't include googlebot by default (to reduce cloaking penality risk), this is how you could enable it
	// 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)
}

About

negroni and fasthttp handlers for prerendering javascript-rendered pages with https://www.prerender.cloud (isomorphic/universal server side rendering)

Topics

Resources

License

Releases

No releases published

Packages

No packages published

Languages