prerender

package module
v0.0.0-...-04e913a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 9, 2018 License: MIT Imports: 14 Imported by: 0

README

Prerender Go

Bots are constantly hitting your site, and a lot of times they're unable to render javascript. Prerender.io is awesome, and allows a headless browser to render you page.

This middleware allows you to intercept requests from crawlers and route them to an external Prerender Service to retrieve the static HTML for the requested page.

Prerender adheres to google's _escaped_fragment_ proposal, which we recommend you use. It's easy:

  • Just add <meta name="fragment" content="!"> to the <head> of all of your pages
  • If you use hash urls (#), change them to the hash-bang (#!)
  • That's it! Perfect SEO on javascript pages.

Features

I tried to replicate the features found in the Prerender-node middleware.

Using it in negroni

package main

import (
  "net/http"
  "net/url"

  "github.com/codegangsta/negroni"
  "github.com/tampajohn/prerender"
  )

  func main() {
    n := negroni.New()
    n.Use(negroni.NewLogger())
    n.Use(prerender.NewOptions().NewPrerender())
    n.Use(negroni.NewStatic(http.Dir(".")))
    n.Run(":80")
  }


... or if you want to use a custom prerender server

package main

import (
  "net/http"
  "net/url"

  "github.com/codegangsta/negroni"
  "github.com/tampajohn/prerender"
  )

  func main() {
    n := negroni.New()
    n.Use(negroni.NewLogger())
    o := prerender.NewOptions()
    o.PrerenderURL, _ = url.Parse("http://prerender.powerchord.io/")
    n.Use(o.NewPrerender())
    n.Use(negroni.NewStatic(http.Dir(".")))
    n.Run(":80")
  }


... or if you want to use it without negroni

package main

import (
  "net/http"

  "github.com/tampajohn/prerender"
  )

  func main() {
    m := http.NewServeMux()
    m.HandleFunc("/", prerender.NewOptions().NewPrerender().PreRenderHandler)
    http.ListenAndServe(":80", m)
  }

Special Thanks

I stole almost all of the logic from prerender-node (thanks prerender guys :))

I also want to thank CodeGangsta for creating Negroni and making it so freaking awesome to use.

Documentation

Overview

Package prerender provides a Prerender.io handler implementation and a Negroni middleware.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options struct {
	PrerenderURL   *url.URL
	Token          string
	BlackList      []regexp.Regexp
	WhiteList      []regexp.Regexp
	UsingAppEngine bool
}

Options provides you with the ability to specify a custom Prerender.io URL as well as a Prerender.io Token to include as an X-Prerender-Token header to the upstream server.

func NewOptions

func NewOptions() *Options

NewOptions generates a default Options struct pointing to the Prerender.io service, obtaining a Token from the environment variable PRERENDER_TOKEN. No blacklist/whitelist is created.

func (*Options) NewPrerender

func (o *Options) NewPrerender() *Prerender

NewPrerender generates a new Prerender instance.

type Prerender

type Prerender struct {
	Options *Options
	// contains filtered or unexported fields
}

Prerender exposes methods to validate and serve content from a Prerender.io upstream server.

func (*Prerender) PreRenderHandler

func (p *Prerender) PreRenderHandler(rw http.ResponseWriter, or *http.Request)

PreRenderHandler is a net/http compatible handler that proxies a request to the configured Prerender.io URL. All upstream requests are made with an Accept-Encoding=gzip header. Responses are provided either uncompressed or gzip compressed based on the downstream requests Accept-Encoding header

func (*Prerender) ServeHTTP

func (p *Prerender) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)

ServeHTTP allows Prerender to act as a Negroni middleware.

func (*Prerender) ShouldPrerender

func (p *Prerender) ShouldPrerender(or *http.Request) bool

ShouldPrerender analyzes the request to determine whether it should be routed to a Prerender.io upstream server.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL