Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

115
Visualizações
How to include Javascript files in a hugo project

I am currently starting to work with hugo SSG. My goal is to have a reliable, yet uncomplicated application to centralize some components in otherwise vanilla html,css,js website projects. I want to utilize hugo to maintain head, header and footer for me.

Now I migrated all of the html and css of my current project to hugo, which worked fine. It's average Landing Pages with header/footer and a couple of sections. However I seem to be unable to include my Javascript files.

I have started with two first scripts to try out the setup, one navBar.js and one headerShadow.js (simple UI tricks).

I have included those two files in {projectName}/themes/{themeName}/layouts/partials into the footer.html with the following tags:

<script defer language="javascript" type="text/javascript"  src="{{ "/js/navBar.js" | urlize | relURL }}"></script>
<script defer language="javascript" type="text/javascript"  src="{{ "/js/headerShadow.js" | urlize | relURL }}"></script>

First question: Is that even the best practice? I found this on another post on stackoverflow, which I'm unable to reproduce.

Second question: My visual studio code already flags exactly the part /js/navBar.js" in both tags as faulty. What am I doing wrong here?

I tried:

  • connecting as "usual" <script src="js/headerShadow.js"></script>

I would be very thankful for advice from someone more experienced with hugo!

Thanks alot in advance :)

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I would suggest using hugo pipes and assets as a best practices. In the docs under:
ASSET MANAGEMENT
JavaScript Building

https://gohugo.io/hugo-pipes/js/

It gives the exact syntax and what to do so I won't bother copy pasting it here. This includes Tree Shaking, minify, etc. etc. Things which are "best practices".

I would suggest of the options (like importing from your node_modules or etc.) that you just keep your JS in the assets folder for simplicity and use that option in the above docs.

Specifically: https://gohugo.io/hugo-pipes/js/#import-js-code-from-assets

This would be, in my opinion, for a simple site, best practice.

about 4 years ago · Juan Pablo Isaza Relatório

0

If you don't want to use Hugo Pipes (it's a bit advanced for what you need), here's a simple way to approach it that worked fine in my project.

<script src="{{ .Site.BaseURL }}js/navBar.js"></script>

In development (hugo server command), .Site.BaseURL will be overridden to http://localhost:1313/. In production (hugo command) it will be the value of baseURL in config.toml, such as https://www.example.com/.

Files in the static directory are built to your site root, so the file would be located at http://localhost:1313/js/navBar.js. That's why I believe the relative path isn't working.

If that doesn't work, there's likely some other issue in your project that needs addressing, or try clearing your browser cache.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda