(chore) closes #1 by adding forward ref
This commit is contained in:
Родитель
eb7075e74a
Коммит
e2db30b67f
|
@ -22,13 +22,15 @@ A `fragment` will be hybrid-polymorphic (if this is a thing). On the server it i
|
||||||
* `method` can be of `GET` (default) or `POST`.
|
* `method` can be of `GET` (default) or `POST`.
|
||||||
* `primary` denotes a fragment that sets the response code of the page
|
* `primary` denotes a fragment that sets the response code of the page
|
||||||
* `id` is an optional unique identifier (optional)
|
* `id` is an optional unique identifier (optional)
|
||||||
|
* `ref`is an optional forwar reference to an `id` (optional)
|
||||||
* `timeout` timeout of a fragement to receive in milliseconds (default is `300`)
|
* `timeout` timeout of a fragement to receive in milliseconds (default is `300`)
|
||||||
* `deferred` is deferring the fetch to the browser
|
* `deferred` is deferring the fetch to the browser
|
||||||
* `fallback` is the fallback source in case of timeout/error on the current fragment
|
* `fallback` is the fallback source in case of timeout/error on the current fragment
|
||||||
|
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
|
||||||
Import the middleware package this is part of the Fiber web framework
|
Import the middleware package this is part of the Fiber web framework.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
|
@ -69,9 +71,10 @@ app.Listen(":8080")
|
||||||
<fragment src="fragment1.html"></fragment>
|
<fragment src="fragment1.html"></fragment>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
The `example` folder contains many examples. You can learn how to use a forward reference of content for fetching outer and inner content and replace either one.
|
||||||
|
|
||||||
## Benchmark(s)
|
## Benchmark(s)
|
||||||
|
|
||||||
This is run on a MacBook Pro 16 inch locally. It is the `example` run.
|
This is run on a MacBook Pro 16 inch locally. It is the `example` run.
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
<h1 class="text-2xl">{{ .Title }}</h1>
|
||||||
<div>
|
<div>
|
||||||
<fragment ref="content" />
|
<fragment ref="content" />
|
||||||
</div>
|
</div>
|
|
@ -47,20 +47,20 @@ func (h *HtmlFragment) Fragment() *goquery.Document {
|
||||||
|
|
||||||
// Fragments is returning the selection of fragments
|
// Fragments is returning the selection of fragments
|
||||||
// from an HTML page.
|
// from an HTML page.
|
||||||
func (h *HtmlFragment) Fragments() ([]*Fragment, error) {
|
func (h *HtmlFragment) Fragments() (map[string]*Fragment, error) {
|
||||||
h.RLock()
|
h.RLock()
|
||||||
defer h.RUnlock()
|
defer h.RUnlock()
|
||||||
|
|
||||||
scripts := h.doc.Find("head script[type=fragment]")
|
scripts := h.doc.Find("head script[type=fragment]")
|
||||||
fragments := h.doc.Find("fragment").AddSelection(scripts)
|
fragments := h.doc.Find("fragment").AddSelection(scripts)
|
||||||
|
|
||||||
ff := make([]*Fragment, 0, fragments.Length())
|
ff := make(map[string]*Fragment)
|
||||||
|
|
||||||
fragments.Each(func(i int, s *goquery.Selection) {
|
fragments.Each(func(i int, s *goquery.Selection) {
|
||||||
f := FromSelection(s)
|
f := FromSelection(s)
|
||||||
|
|
||||||
if !f.deferred {
|
if !f.deferred {
|
||||||
ff = append(ff, f)
|
ff[f.ID()] = f
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
23
resolver.go
23
resolver.go
|
@ -41,6 +41,29 @@ func (r *Resolver) Resolve(c *fiber.Ctx, cfg Config, doc *HtmlFragment) (int, []
|
||||||
statusCode = f.statusCode
|
statusCode = f.statusCode
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fw, err := f.HtmlFragment().Fragments()
|
||||||
|
if err != nil {
|
||||||
|
return statusCode, head, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, fwr := range fw {
|
||||||
|
if fwr.Ref() == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
ref, ok := ff[fwr.Ref()]
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
html, err := ref.HtmlFragment().Html()
|
||||||
|
if err != nil {
|
||||||
|
return statusCode, head, err
|
||||||
|
}
|
||||||
|
|
||||||
|
fwr.Element().ReplaceWithHtml(html)
|
||||||
|
}
|
||||||
|
|
||||||
html, err := f.HtmlFragment().Html()
|
html, err := f.HtmlFragment().Html()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return statusCode, head, err
|
return statusCode, head, err
|
||||||
|
|
Загрузка…
Ссылка в новой задаче