I have a static website repo hosted with Github Pages. The javascript must load two text files (.yml
files more precisely) stored in that same repo in a data
folder. The functions used to load the two files are the following:
async function readTextFile(path) {
console.log("Path:", path);
const response = await fetch(path);
console.log("Response:", response);
const text = await response.text();
console.log("Text:", text);
return text;
}
export async function loadBodiesData() {
const content = await readTextFile("./data/kspbodies.yml");
/* Some post processing */
}
export async function loadConfig() {
const content = await readTextFile("./data/settings.yml");
/* Some post processing */
}
The kspbodies.yml
file is correctly loaded and its content displayed in the console (Text: ...
). The settings.yml
file however appears to be empty (Text: <empty string>
). The file exists as a direct look at this file from its URL doesn't throw a 404 error, but returns an empty file (0 byte size).
kspbodies.yml
and settings.yml
are located in /data
/dist/main/utilities/data.js
and are called from /dist/main/main.js
settings.yml
into something elsesettings.yml
, its URL returns an empty file as wellNote: The website works perfectly on a local server.
Does anyone know where this problem comes from and how to fix it ?
Thanks in advance.
Alright I found the problem. It actually comes from the syntax used in the yaml files. The first characters in the file seem to matter. I experimented with the content of a YAML file and here are the results when loaded through the hosted github page:
test: 42
---
and ...
as documented in the Wikipedia page for YAML: loads an empty file---
test: 42
...
---
: loads correctly# Test file
---
test: 42
...
So it seems that having a YAML file starting with ---
makes the content of the file being ignored by the server. There is probably a difference in syntax standards used between github servers and vscode live server for yaml files.