Skip to content

Commit

Permalink
Merge pull request #761 from trheyi/main
Browse files Browse the repository at this point in the history
fix  sui dynamic components render bug
  • Loading branch information
trheyi authored Sep 30, 2024
2 parents 6656e18 + 574c2aa commit 39cec07
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion sui/core/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ func (page *Page) BuildScripts(ctx *BuildContext, option *BuildOption, component
}

arguments := "document.body"
if !ispage {
if !ispage || option.JitMode {
arguments = "component"
}

Expand Down
13 changes: 12 additions & 1 deletion sui/core/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package core

import (
"fmt"
"hash/fnv"
"regexp"
"strings"

Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 == "" {
Expand Down
7 changes: 4 additions & 3 deletions sui/core/jit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions sui/core/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 39cec07

Please sign in to comment.