Merge pull request #761 from trheyi/main
fix sui dynamic components render bug
This commit is contained in:
commit
39cec07c66
4 changed files with 18 additions and 5 deletions
|
|
@ -731,7 +731,7 @@ func (page *Page) BuildScripts(ctx *BuildContext, option *BuildOption, component
|
||||||
}
|
}
|
||||||
|
|
||||||
arguments := "document.body"
|
arguments := "document.body"
|
||||||
if !ispage {
|
if !ispage || option.JitMode {
|
||||||
arguments = "component"
|
arguments = "component"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"hash/fnv"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
|
@ -220,12 +221,21 @@ func (page *Page) CompileHTML(source []byte, minify bool) ([]byte, error) {
|
||||||
return source, nil
|
return source, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Hash return the hash of the script
|
||||||
|
func (script ScriptNode) Hash() string {
|
||||||
|
raw := fmt.Sprintf("%s|%v|%s", script.Component, script.Attrs, script.Parent)
|
||||||
|
h := fnv.New64a()
|
||||||
|
h.Write([]byte(raw))
|
||||||
|
return fmt.Sprintf("script_%x", h.Sum64())
|
||||||
|
}
|
||||||
|
|
||||||
// HTML return the html of the script
|
// HTML return the html of the script
|
||||||
func (script ScriptNode) HTML() string {
|
func (script ScriptNode) HTML() string {
|
||||||
|
|
||||||
attrs := []string{
|
attrs := []string{
|
||||||
"s:ns=\"" + script.Namespace + "\"",
|
"s:ns=\"" + script.Namespace + "\"",
|
||||||
"s:cn=\"" + script.Component + "\"",
|
"s:cn=\"" + script.Component + "\"",
|
||||||
|
"s:hash=\"" + script.Hash() + "\"",
|
||||||
}
|
}
|
||||||
if script.Attrs != nil {
|
if script.Attrs != nil {
|
||||||
for _, attr := range script.Attrs {
|
for _, attr := range script.Attrs {
|
||||||
|
|
@ -245,6 +255,7 @@ func (script ScriptNode) ComponentHTML(ns string) string {
|
||||||
attrs := []string{
|
attrs := []string{
|
||||||
"s:ns=\"" + ns + "\"",
|
"s:ns=\"" + ns + "\"",
|
||||||
"s:cn=\"" + script.Component + "\"",
|
"s:cn=\"" + script.Component + "\"",
|
||||||
|
"s:hash=\"" + script.Hash() + "\"",
|
||||||
}
|
}
|
||||||
if script.Attrs != nil {
|
if script.Attrs != nil {
|
||||||
for _, attr := range script.Attrs {
|
for _, attr := range script.Attrs {
|
||||||
|
|
@ -258,7 +269,7 @@ func (script ScriptNode) ComponentHTML(ns string) string {
|
||||||
|
|
||||||
source := script.Source
|
source := script.Source
|
||||||
if !strings.Contains(script.Source, "function "+script.Component) {
|
if !strings.Contains(script.Source, "function "+script.Component) {
|
||||||
source = fmt.Sprintf(`function %s(){%s};`, script.Component, script.Source)
|
source = fmt.Sprintf(`function %s( component ){%s};`, script.Component, script.Source)
|
||||||
}
|
}
|
||||||
|
|
||||||
if script.Component == "" {
|
if script.Component == "" {
|
||||||
|
|
|
||||||
|
|
@ -82,11 +82,12 @@ func (parser *TemplateParser) parseJitComponent(sel *goquery.Selection) {
|
||||||
// Add the scripts
|
// Add the scripts
|
||||||
if comp.scripts != nil {
|
if comp.scripts != nil {
|
||||||
for _, script := range comp.scripts {
|
for _, script := range comp.scripts {
|
||||||
if parser.context.scriptMaps[script.Component] {
|
hash := script.Hash()
|
||||||
|
if parser.context.scriptMaps[hash] {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
script.Parent = "head"
|
script.Parent = "head"
|
||||||
parser.context.scriptMaps[script.Component] = true
|
parser.context.scriptMaps[hash] = true
|
||||||
parser.context.scripts = append(parser.context.scripts, script)
|
parser.context.scripts = append(parser.context.scripts, script)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -356,7 +357,7 @@ func (parser *TemplateParser) filterScripts(parent string, scripts []ScriptNode)
|
||||||
func (parser *TemplateParser) addScripts(sel *goquery.Selection, scripts []ScriptNode) {
|
func (parser *TemplateParser) addScripts(sel *goquery.Selection, scripts []ScriptNode) {
|
||||||
for _, script := range scripts {
|
for _, script := range scripts {
|
||||||
if script.Component != "" {
|
if script.Component != "" {
|
||||||
query := fmt.Sprintf(`script[s\:cn="%s"]`, script.Component)
|
query := fmt.Sprintf(`script[s\:hash="%s"]`, script.Hash())
|
||||||
if sel.Find(query).Length() > 0 {
|
if sel.Find(query).Length() > 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -88,6 +88,7 @@ var allowUsePropAttrs = map[string]bool{
|
||||||
var keepAttrs = map[string]bool{
|
var keepAttrs = map[string]bool{
|
||||||
"s:ns": true,
|
"s:ns": true,
|
||||||
"s:cn": true,
|
"s:cn": true,
|
||||||
|
"s:hash": true,
|
||||||
"s:ready": true,
|
"s:ready": true,
|
||||||
"s:event": true,
|
"s:event": true,
|
||||||
"s:event-jit": true,
|
"s:event-jit": true,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue