Categorygithub.com/sanfrancesco/prerendercloud-golang
modulepackage
0.0.0-20220506201239-2d71e4916556
Repository: https://github.com/sanfrancesco/prerendercloud-golang.git
Documentation: pkg.go.dev

# README

prerendercloud-golang

negroni middleware, and a fasthttp handler for pre-rendering JavaScript single page apps with Headless-Render-API.com (formerly named prerender.cloud from 2016 - 2022)

Set your API token via env var

(get token after signing up at Headless-Render-API.com)

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)
}

# Packages

No description provided by the author
No description provided by the author

# Functions

NewOptions generates a default Options struct pointing to the Prerender.cloud service, obtaining a Token from the environment variable PRERENDER_TOKEN.

# Variables

No description provided by the author

# Structs

Options provides you with the ability to specify a custom Prerender.cloud URL as well as a Prerender.cloud Token to include as an X-Prerender-Token header to the upstream server.
Prerender exposes methods to validate and serve content from a Prerender.cloud upstream server.