I am trying to import a txt file and print the lines in my SvelteKit app. I tried to fetch it with the server and also I tried to import it straight into the component but I cannot get either to work. I have tried adding the txt file to both the static folder and public. This is how I tried to import it on the component.
onMount(() => {
loadItems();
});
const loadItems = async () => {
const response = await fetch('public/safety.txt');
const text = await response.text();
items = text.split('\n\n'); // split on double newlines
};
let currentIndex = 0;
const interval = setInterval(() => {
currentIndex = Math.floor(Math.random() * items.length);
}, 5000);
$: currentItem = items[currentIndex];
onDestroy(() => {
clearInterval(interval);
});
And this is how I tried on the server:
const safety = await fetch('/public/safety.txt')
You need to put you static files under "static" folder.
For example:
yourapp/static/safety.txt
Then it will be:
const response = await fetch('safety.txt');
That's it