2

currently am working in a Golang Site, as frontend I am using the go templates. In order to separate the different parts of the sites I created this templates:

  1. Head: containing the <head> tag, and some imports of css
  2. Header: defining the navbar of the site, logo, etc. It's only HTML
  3. Footer: defining the HTML of the footer of the site.
  4. SomePage: defining the body tag, this template calls Head, Header and footer. This file can be for example the index, the login, etc.

I added the Javascript imports just after I nest the footer template, but still inside the body tag. As example, I added them like:

<script src="public/js/SomeJS.js"></script>
<link href="public/css/SomeCSS.css" type="text/css" rel="stylesheet"/>

In the routes of my project I have defined public to serve those static files, this is working pretty nice. Now, I have some routes of the project, lets say:

router.GET("/",controllers.Index)
router.GET("/login",controllers.Login)

With the first route, the libraries are being loaded pretty well. Then, with routes as the second, I am not able to load them because the browser tries to locate that files as:

http://myserver/login/public/css/someCSS.css  instead of
http://myserver/public/css/someCSS.css

In this case, I understand why it's adding the login to the URL. So, my question is, how it's normally handled?

By now I added the domain and folder to the imports, example:

<script src="MyDomain.com/public/js/SomeJS.js"></script>

But I really don't want to do it in this way, because anytime that the domain change I should be editing the code and it's not really pratical. Also, I don't want to add the imports of the libraries in every view that I create. I have some files (CSS and JS) that are common for all the project and I just want writte it once.

Thank you!

1 Answer 1

2

Add / in the being of the path, it is called relative path from domain root.

<script src="/public/js/SomeJS.js"></script>
<link href="/public/css/SomeCSS.css" type="text/css" rel="stylesheet"/>

Without /, it is called as relative path from current domain directory. This is the behavior you have right now.

Output:

http://myserver/public/css/someCSS.css
http://myserver/public/js/SomeJS.js
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.