Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

349
Vistas
Github pages, how to fetch file in js from repo

I have a github pages project that I am trying to create. I've got it working great on local, but of course when I publish it it fails.

The problem is in this bit of javascript, which is supposed to pull some data from a json file in the repo to build the contents of a certain page:

(async function(){
  const response = await fetch(`https://GITUSER.github.io/GITREPO/tree/gh-pages/data/file.json`);//Error gets thrown here, because the asset does not exist in the current code state.
  const docData = await response.json();
  const contentTarget = document.getElementById('doc-target');
  const tocTarget = document.getElementById('toc-target')
  createContent(tocTarget,contentTarget,docData);
})();

Now, the problem is that pages won't load the asset because it doesn't know that it needs it until the function is called. Is there a way to get this asset loaded by pages so it can be called by the fetch API? Or is this beyond the capabilities of github pages?

Edited: Added some additional code for context.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Try using raw.githubusercontent.com like this

(async function(){
  const response = await fetch('https://raw.githubusercontent.com/{username}/{repo}/{branch}/{file}')
  const docData = await response.json();
  const contentTarget = document.getElementById('doc-target');
  const tocTarget = document.getElementById('toc-target')
  createContent(tocTarget,contentTarget,docData);
})();

And it would work

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda