Skip to content

Commit

Permalink
set request headers
Browse files Browse the repository at this point in the history
  • Loading branch information
krishanthisera committed Nov 16, 2023
1 parent fb4729d commit dd737f0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
10 changes: 9 additions & 1 deletion grender.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
version: v1
renderingConfig:
pageWaitTime: 1000
pageWaitTime: 5
pageWaitCondition: '(function() { return window.prerenderReady === true })()'
requestHeaders:
- name: "X-Grender-Request"
value: "1"
server:
port: "8080"
headers:
- name: "X-Prerender"
value: "1"
backend:
fileSystem:
baseDir: "./tmp"
s3:
bucketName: "grender.io"
region: "ap-southeast-2"
4 changes: 4 additions & 0 deletions render/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ package render
type Config struct {
PageWaitTime float32 `yaml:"pageWaitTime"` // Seconds
PageWailCondition string `yaml:"pageWaitCondition"`
RequestHeaders []struct {
Name string `yaml:"name"`
Value string `yaml:"value"`
} `yaml:"requestHeaders"`
}
23 changes: 18 additions & 5 deletions render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ import (
"fmt"
"log"
"net/url"
"strings"
"time"

"github.com/chromedp/cdproto/dom"
"github.com/chromedp/cdproto/network"
"github.com/chromedp/chromedp"
"github.com/google/uuid"
)

func (config Config) Render(webAddr string) (*string, error) {
// Validate the URL
webAddr = func(val string) string {
u, err := url.Parse(val)
u, err := url.Parse(strings.TrimLeft(val, "/"))
if err != nil {
panic(err)
}
Expand All @@ -32,19 +35,29 @@ func (config Config) Render(webAddr string) (*string, error) {
defer cancel()

var html string
if err := chromedp.Run(ctx, pageRender(webAddr, config.PageWailCondition, time.Duration(config.PageWaitTime*float32(time.Second)), &html)); err != nil {
// Set request headers
headers := func() network.Headers {
h := network.Headers{"X-Grender-Request-Id": fmt.Sprintf("%v", uuid.New())}
for _, v := range config.RequestHeaders {
h[v.Name] = v.Value
}
fmt.Println(h)
return h
}()

if err := chromedp.Run(ctx, pageRender(webAddr, config.PageWailCondition, time.Duration(config.PageWaitTime*float32(time.Second)), &headers, &html)); err != nil {
return nil, err
}
return &html, nil
}

// This is the function that does the actual rendering
func pageRender(webAddr string, waitCondition string, pageWaitTime time.Duration, html *string) chromedp.Tasks {
func pageRender(webAddr string, waitCondition string, pageWaitTime time.Duration, headers *network.Headers, html *string) chromedp.Tasks {
return chromedp.Tasks{
network.Enable(),
network.SetExtraHTTPHeaders(*headers),
chromedp.Navigate(webAddr),
chromedp.ActionFunc(func(ctx context.Context) error {
// Wait Condition

var result bool
startTime := time.Now()
for time.Since(startTime) < pageWaitTime {
Expand Down

0 comments on commit dd737f0

Please sign in to comment.