fix sui dynamic components render bug

This commit is contained in:
Max 2024-09-30 18:42:42 +08:00
parent 371490360e
commit 574c2aad82
4 changed files with 18 additions and 5 deletions

View file

@ -731,7 +731,7 @@ func (page *Page) BuildScripts(ctx *BuildContext, option *BuildOption, component
}
arguments := "document.body"
if !ispage {
if !ispage || option.JitMode {
arguments = "component"
}

View file

@ -2,6 +2,7 @@ package core
import (
"fmt"
"hash/fnv"
"regexp"
"strings"
@ -220,12 +221,21 @@ func (page *Page) CompileHTML(source []byte, minify bool) ([]byte, error) {
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
func (script ScriptNode) HTML() string {
attrs := []string{
"s:ns=\"" + script.Namespace + "\"",
"s:cn=\"" + script.Component + "\"",
"s:hash=\"" + script.Hash() + "\"",
}
if script.Attrs != nil {
for _, attr := range script.Attrs {
@ -245,6 +255,7 @@ func (script ScriptNode) ComponentHTML(ns string) string {
attrs := []string{
"s:ns=\"" + ns + "\"",
"s:cn=\"" + script.Component + "\"",
"s:hash=\"" + script.Hash() + "\"",
}
if script.Attrs != nil {
for _, attr := range script.Attrs {
@ -258,7 +269,7 @@ func (script ScriptNode) ComponentHTML(ns string) string {
source := script.Source
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 == "" {

View file

@ -82,11 +82,12 @@ func (parser *TemplateParser) parseJitComponent(sel *goquery.Selection) {
// Add the scripts
if comp.scripts != nil {
for _, script := range comp.scripts {
if parser.context.scriptMaps[script.Component] {
hash := script.Hash()
if parser.context.scriptMaps[hash] {
continue
}
script.Parent = "head"
parser.context.scriptMaps[script.Component] = true
parser.context.scriptMaps[hash] = true
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) {
for _, script := range scripts {
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 {
continue
}

View file

@ -88,6 +88,7 @@ var allowUsePropAttrs = map[string]bool{
var keepAttrs = map[string]bool{
"s:ns": true,
"s:cn": true,
"s:hash": true,
"s:ready": true,
"s:event": true,
"s:event-jit": true,