Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

🐛 template/html: Cannot Clone "layouts/main" after it has executed #818

Closed
suntong opened this issue Sep 20, 2020 · 16 comments
Closed

🐛 template/html: Cannot Clone "layouts/main" after it has executed #818

suntong opened this issue Sep 20, 2020 · 16 comments

Comments

@suntong
Copy link

suntong commented Sep 20, 2020

Fiber version

v2.0.2

Issue description

Continue from #814, which were having the problem of

html/template: cannot Clone "layouts/main" after it has executed

  • it seems only occurs to html/template, and
  • the solution was to reload the template engine.

However, the reload was not necessary before, and it only works for simple demo examples. I.e., my demo is working after the fix, while my real application is still suffering from the issue.

Here are the details:

Code snippet

package main

import "github.com/gofiber/fiber/v2"

func main() {
  app := fiber.New()

  // Steps to reproduce
	app.Get("/layout", func(c *fiber.Ctx) error {
		// Render index within layouts/main
		return c.Render("index", fiber.Map{
			"Title": "Hello, World!",
		}, "layouts/main")
	})

  log.Fatal(app.Listen(":3000"))
}

I've setup a new demo at https://gitlab.com/suntong/template-html/,

  • everything about the html template handing is the same as my real application, and even the middlewares
  • the only thing I see different from the two is there are more business logic in my real application
  • yet my real application can render the html template only once, then it get to the same error again, while the above published demo can sustain the repeated requests.

Here are the test logs:

$ cat go.mod 
module template-html

go 1.14

require (
        github.com/gofiber/fiber/v2 v2.0.2
        github.com/gofiber/template v1.6.2
)

# Start my real application

$ curl http://localhost:5858/layout
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  
  <link href="https://fonts.googleapis.com/css?family=Montserrat:400,500|Poppins:400,500,600,700|Roboto:400,500"
  rel="stylesheet" />
  <link href="https://cdn.materialdesignicons.com/3.0.39/css/materialdesignicons.min.css" rel="stylesheet" />
  
  <link id="sleek-css" rel="stylesheet" href="assets/css/sleek.min.css" />

  <title>Hello, World! - </title>
</head>

<body id="body">

<h1>Hello, World!</h1>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="assets/plugins/slimscrollbar/jquery.slimscroll.min.js"></script>
  <script src="assets/js/sleek.bundle.js"></script>
</body>

</html>

# Start my demo
$ curl http://localhost:3000/layout
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  
  <link href="https://fonts.googleapis.com/css?family=Montserrat:400,500|Poppins:400,500,600,700|Roboto:400,500"
  rel="stylesheet" />
  <link href="https://cdn.materialdesignicons.com/3.0.39/css/materialdesignicons.min.css" rel="stylesheet" />
  
  <link id="sleek-css" rel="stylesheet" href="assets/css/sleek.min.css" />

  <title>Hello, World! - </title>
</head>

<body id="body">

<h1>Hello, World!</h1>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="assets/plugins/slimscrollbar/jquery.slimscroll.min.js"></script>
  <script src="assets/js/sleek.bundle.js"></script>
</body>

</html>

# repeat
$ curl http://localhost:5858/layout
html/template: cannot Clone "layouts/main" after it has executed

$ curl -s http://localhost:3000/layout | tail -6
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="assets/plugins/slimscrollbar/jquery.slimscroll.min.js"></script>
  <script src="assets/js/sleek.bundle.js"></script>
</body>

</html>

. . .

$ curl -s http://localhost:3000/layout | tail -6
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="assets/plugins/slimscrollbar/jquery.slimscroll.min.js"></script>
  <script src="assets/js/sleek.bundle.js"></script>
</body>

</html>

i.e., my demo can sustain the repeated requests while my real application cannot.

Again, everything about the html template handing is the same between my demo and my real application, and even the middlewares. So I'm suspecting that any other mildly complicated template/html based application would suffer from the same issue, caused by the same problem that needs to reload the template engine. If someone has to have my real application to duplicate the issue, please give me your gitlab id so that I can add you to my private project.

Thanks

@anton7r
Copy link

anton7r commented Sep 20, 2020

i have the same issue

@anton7r
Copy link

anton7r commented Sep 21, 2020

im looking into it!

@anton7r
Copy link

anton7r commented Sep 21, 2020

from my observations the issue does not seem to be with gofiber/template/html.

i went to take a deeper dive into the html/template package

in the template.go file after i removed the check for that highlighted src.EscapeErr it started to work expectedly.
kuva

so i believe this issue should be moved to golang/go/issues

@anton7r
Copy link

anton7r commented Sep 21, 2020

kuva
after i further modified the template.go file and got it to print out the new error message it says:
html/template: cannot Clone "layouts/main" because of an escape error: "template escaped correctly"

@anton7r
Copy link

anton7r commented Sep 21, 2020

i think removing this from the golang/go/html/template/template.go and refactoring the code would fix the issue
kuva

@Fenny
Copy link
Member

Fenny commented Sep 24, 2020

The question is why is an escapeErr set in the first place, maybe this is something we can solve on our end.

@suntong
Copy link
Author

suntong commented Sep 24, 2020

The question is why is an escapeErr set in the first place, maybe this is something we can solve on our end.

Yep, totally agree.

Giving the fact that V1 has no problems at all all the time and V2 suddenly has such problem, I'd highly suspect that the issue is V2 related, not the Go html/template package.

@anton7r
Copy link

anton7r commented Sep 25, 2020

The question is why is an escapeErr set in the first place, maybe this is something we can solve on our end.

Yep, totally agree.

Giving the fact that V1 has no problems at all all the time and V2 suddenly has such problem, I'd highly suspect that the issue is V2 related, not the Go html/template package.

Yes it seems like it is actually an issue with V2, because the the template.go file has not been drastically modified in the past 2 years.

But maybe someone should refactor that file?

@Fenny
Copy link
Member

Fenny commented Sep 25, 2020

I have some time tomorrow to debug this issue, I will post updates here 👍

@john-gold
Copy link

Hello, so far I'm only getting this error if

*Engine.Reload == false

@anton7r
Copy link

anton7r commented Sep 28, 2020

i propose that we have a fork of the orginal golang/go project where we try to improve it and then later on when it is stable we will merge it back in to the go's official repo.

@suntong
Copy link
Author

suntong commented Sep 28, 2020

Have you tried turn on *Engine.Reload and leave Go html/template package unchanged @anton7r?

@anton7r
Copy link

anton7r commented Sep 29, 2020

Have you tried turn on *Engine.Reload and leave Go html/template package unchanged @anton7r?

yes, but thats not really viable to use on production because it will slow down rendering performance quite a bit

@suntong
Copy link
Author

suntong commented Sep 29, 2020

Ah, gotya. yeah, make sense.

@Fenny
Copy link
Member

Fenny commented Sep 29, 2020

I'm still trying to fix the issue, but no luck so far 😕😵

edit: found the problem, expect a PR soon. ( keep you updated )

@Fenny
Copy link
Member

Fenny commented Sep 29, 2020

@suntong, @anton7r this problem is fixed in v1.6.3, feel free to re-open if you encounter problems 👍

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

4 participants