From 574c2aad82b8596c990e4ebb8138cfd9f3c7d602 Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 30 Sep 2024 18:42:42 +0800 Subject: [PATCH] fix sui dynamic components render bug --- sui/core/build.go | 2 +- sui/core/compile.go | 13 ++++++++++++- sui/core/jit.go | 7 ++++--- sui/core/parser.go | 1 + 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/sui/core/build.go b/sui/core/build.go index 1f5530ef..f3955309 100644 --- a/sui/core/build.go +++ b/sui/core/build.go @@ -731,7 +731,7 @@ func (page *Page) BuildScripts(ctx *BuildContext, option *BuildOption, component } arguments := "document.body" - if !ispage { + if !ispage || option.JitMode { arguments = "component" } diff --git a/sui/core/compile.go b/sui/core/compile.go index 68f62c56..9e847486 100644 --- a/sui/core/compile.go +++ b/sui/core/compile.go @@ -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 == "" { diff --git a/sui/core/jit.go b/sui/core/jit.go index 066700ec..7533897b 100644 --- a/sui/core/jit.go +++ b/sui/core/jit.go @@ -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 } diff --git a/sui/core/parser.go b/sui/core/parser.go index d7532834..f4ff30da 100644 --- a/sui/core/parser.go +++ b/sui/core/parser.go @@ -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,