Skip to content

Commit

Permalink
Move metatag filtering to html
Browse files Browse the repository at this point in the history
  • Loading branch information
natewong1313 committed Oct 20, 2023
1 parent 7af2049 commit 1f23f4b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
23 changes: 23 additions & 0 deletions internal/html/render_html.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"html/template"
"os"
"runtime"
"strings"
)

type Params struct {
Expand All @@ -30,6 +31,8 @@ type Params struct {
// RenderHTMLString Renders the HTML template in internal/html with the given parameters
func RenderHTMLString(params Params) []byte {
params.IsDev = os.Getenv("APP_ENV") != "production"
params.OGMetaTags = getOGMetaTags(params.MetaTags)
params.MetaTags = getMetaTags(params.MetaTags)
t := template.Must(template.New("").Parse(BaseTemplate))
var output bytes.Buffer
err := t.Execute(&output, params)
Expand All @@ -39,6 +42,26 @@ func RenderHTMLString(params Params) []byte {
return output.Bytes()
}

func getMetaTags(metaTags map[string]string) map[string]string {
newMetaTags := make(map[string]string)
for key, value := range metaTags {
if !strings.HasPrefix(key, "og:") {
newMetaTags[key] = value
}
}
return newMetaTags
}

func getOGMetaTags(metaTags map[string]string) map[string]string {
newMetaTags := make(map[string]string)
for key, value := range metaTags {
if strings.HasPrefix(key, "og:") {
newMetaTags[key] = value
}
}
return newMetaTags
}

type ErrorParams struct {
Error string
RouteID string
Expand Down
31 changes: 3 additions & 28 deletions render.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ type renderTask struct {
}

func (engine *Engine) RenderRoute(renderConfig RenderConfig) []byte {
pc, _, _, _ := runtime.Caller(1)
task := renderTask{
engine: engine,
logger: zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr}).With().Timestamp().Logger(),
routeID: getRouteID(),
routeID: fmt.Sprint(pc),
filePath: filepath.ToSlash(utils.GetFullFilePath(engine.Config.FrontendDir + "/" + renderConfig.File)),
config: renderConfig,
serverRenderResult: make(chan ServerRenderResult),
Expand All @@ -50,40 +51,14 @@ func (engine *Engine) RenderRoute(renderConfig RenderConfig) []byte {
}
return html.RenderHTMLString(html.Params{
Title: renderConfig.Title,
MetaTags: getMetaTags(renderConfig.MetaTags),
OGMetaTags: getOGMetaTags(renderConfig.MetaTags),
MetaTags: renderConfig.MetaTags,
JS: template.JS(clientRenderResult.js),
CSS: template.CSS(serverRenderResult.css),
RouteID: task.routeID,
ServerHTML: template.HTML(serverRenderResult.html),
})
}

func getRouteID() string {
pc, _, _, _ := runtime.Caller(2)
return fmt.Sprint(pc)
}

func getMetaTags(metaTags map[string]string) map[string]string {
newMetaTags := make(map[string]string)
for key, value := range metaTags {
if !strings.HasPrefix(key, "og:") {
newMetaTags[key] = value
}
}
return newMetaTags
}

func getOGMetaTags(metaTags map[string]string) map[string]string {
newMetaTags := make(map[string]string)
for key, value := range metaTags {
if strings.HasPrefix(key, "og:") {
newMetaTags[key] = value
}
}
return newMetaTags
}

func (rt *renderTask) Render() (ServerRenderResult, ClientRenderResult, error) {
props, err := propsToString(rt.config.Props)
if err != nil {
Expand Down

0 comments on commit 1f23f4b

Please sign in to comment.