wip: primary fragment and options

This commit is contained in:
Sebastian Döll 2021-07-12 20:19:12 +00:00 коммит произвёл GitHub
Родитель d4d2568944
Коммит 42d540e339
5 изменённых файлов: 58 добавлений и 8 удалений

Просмотреть файл

@ -20,10 +20,11 @@ A `fragment` will be hybrid-polymorphic (if this is a thing). On the server it i
* `src` The source to fetch for replacement in the DOM
* `method` can be of `GET` (default) or `POST`.
* `primary` denotes a fragment that sets the response code of the page
* `id` is an optional unique identifier (optional)
* `timeout` timeout of a fragement to receive in milliseconds (default is `300`)
* `deferred` is deferring the fetch to the browser
* `fallback` is deferring the fetch to the browser if failed (default)
* `fallback` is the fallback source in case of timeout/error on the current fragment
## Example

Просмотреть файл

@ -41,7 +41,9 @@ func (d *Document) Fragments() ([]*Fragment, error) {
fragments.Each(func(i int, s *goquery.Selection) {
f := FromSelection(s)
ff = append(ff, f)
if !f.deferred {
ff = append(ff, f)
}
})
return ff, nil

Просмотреть файл

@ -2,6 +2,7 @@ package fragments
import (
"strconv"
"strings"
"sync"
"time"
@ -10,9 +11,12 @@ import (
// Fragment ...
type Fragment struct {
src string
timeout int64
method string
src string
timeout int64
method string
fallback string
primary bool
deferred bool
once sync.Once
s *goquery.Selection
@ -27,6 +31,9 @@ func FromSelection(s *goquery.Selection) *Fragment {
src, _ := s.Attr("src")
f.src = src
fallback, _ := s.Attr("fallback")
f.fallback = fallback
method, _ := s.Attr("method")
f.method = method
@ -37,6 +44,12 @@ func FromSelection(s *goquery.Selection) *Fragment {
t, _ := strconv.ParseInt(timeout, 10, 64)
f.timeout = t
deferred, ok := s.Attr("deferred")
f.deferred = ok && strings.ToUpper(deferred) != "FALSE"
primary, ok := s.Attr("primary")
f.primary = ok && strings.ToUpper(primary) != "FALSE"
return f
}
@ -45,6 +58,11 @@ func (f *Fragment) Src() string {
return f.src
}
// Fallback ...
func (f *Fragment) Fallback() string {
return f.fallback
}
// Timeout ...
func (f *Fragment) Timeout() time.Duration {
return time.Duration(f.timeout)
@ -59,3 +77,13 @@ func (f *Fragment) Method() string {
func (f *Fragment) Element() *goquery.Selection {
return f.s
}
// Deferred ...
func (f *Fragment) Deferred() bool {
return f.deferred
}
// Primary ...
func (f *Fragment) Primary() bool {
return f.primary
}

Просмотреть файл

@ -29,6 +29,14 @@ type Config struct {
// Optional. Default: nil
Filter func(*fiber.Ctx) bool
// FilterResponse defines a function to filter the responses
// from the fragment sources.
FilterResponse func(*fasthttp.Response) *fasthttp.Response
// FilterRequest defines a function to filter the request
// to the fragment sources.
FilterRequest func(*fasthttp.Request) *fasthttp.Request
// ErrorHandler defines a function which is executed
// It may be used to define a custom error.
// Optional. Default: 401 Invalid or expired key
@ -134,6 +142,8 @@ func Do(c *fiber.Ctx, cfg Config, doc *Document) error {
return err
}
res = cfg.FilterResponse(res)
if res.StatusCode() != http.StatusOK {
// TODO: wrap in custom error
return fmt.Errorf("resolve: could not resolve fragment at %s", f.Src())
@ -204,6 +214,18 @@ func configDefault(config ...Config) Config {
}
}
if cfg.FilterResponse == nil {
cfg.FilterResponse = func(res *fasthttp.Response) *fasthttp.Response {
return res
}
}
if cfg.FilterRequest == nil {
cfg.FilterRequest = func(req *fasthttp.Request) *fasthttp.Request {
return req
}
}
if cfg.DefaultHost == "" {
cfg.DefaultHost = "localhost:3000"
}

Просмотреть файл

@ -7,9 +7,6 @@ import (
"golang.org/x/net/html/atom"
)
// Rel ...
type Rel string
const (
StyleSheet = "stylesheet"
Script = "script"