Skip to content
This repository was archived by the owner on Jul 14, 2023. It is now read-only.

Commit 206e796

Browse files
authored
(chore) define fragment references
1 parent 0b7b1fd commit 206e796

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

example/views/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<fragment src="http://localhost:3000/fragment1"></fragment>
22
<fragment src="fragment2" primary></fragment>
3-
<fragment src="fragment3" timeout="10" fallback="fallback"></fragment>
3+
<fragment src="fragment3" timeout="1" fallback="fallback"></fragment>

fragment.go

+23
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/PuerkitoBio/goquery"
1212
"github.com/gofiber/fiber/v2"
13+
"github.com/google/uuid"
1314
"github.com/valyala/fasthttp"
1415
"golang.org/x/net/html"
1516
)
@@ -24,6 +25,9 @@ type Fragment struct {
2425
src string
2526
timeout int64
2627

28+
id string
29+
ref string
30+
2731
statusCode int
2832
head []*html.Node
2933

@@ -52,6 +56,15 @@ func FromSelection(s *goquery.Selection) *Fragment {
5256
t, _ := strconv.ParseInt(timeout, 10, 64)
5357
f.timeout = t
5458

59+
id, ok := s.Attr("id")
60+
if !ok {
61+
id = uuid.New().String()
62+
}
63+
f.id = id
64+
65+
ref, _ := s.Attr("ref")
66+
f.ref = ref
67+
5568
deferred, ok := s.Attr("deferred")
5669
f.deferred = ok && strings.ToUpper(deferred) != "FALSE"
5770

@@ -105,6 +118,16 @@ func (f *Fragment) Links() []*html.Node {
105118
return f.head
106119
}
107120

121+
// Ref represents the reference to another fragment
122+
func (f *Fragment) Ref() string {
123+
return f.ref
124+
}
125+
126+
// ID represents a unique id for the fragment
127+
func (f *Fragment) ID() string {
128+
return f.id
129+
}
130+
108131
// Resolve is resolving all needed data, setting headers
109132
// and the status code.
110133
func (f *Fragment) Resolve() ResolverFunc {

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/PuerkitoBio/goquery v1.7.0
77
github.com/gofiber/fiber/v2 v2.13.0
88
github.com/gofiber/template v1.6.12
9+
github.com/google/uuid v1.0.0
910
github.com/valyala/fasthttp v1.26.0
1011
golang.org/x/net v0.0.0-20210510120150-4163338589ed
1112
)

go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXi
129129
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
130130
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
131131
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
132+
github.com/google/uuid v1.0.0 h1:b4Gk+7WdP/d3HZH8EJsZpvV7EtDOgaZLtnaNGIu1adA=
132133
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
133134
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
134135
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=

0 commit comments

Comments
 (0)