Should You Use fetch() To Read Embedded Files?
#15004
-
|
Is it OK to use This is my simple example code to reading the Is this a good idea to use However this code does work in development and production (at least on Linux). And when examining further, Tauri in development runs on |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
|
sure, that should be fine. I'd just recommend to not do the same for very large files. But that's more about bundling itself, not fetch because when you use https://v2.tauri.app/develop/resources/ for the large files you could still use fetch() to get them, just with a different url. |
Beta Was this translation helpful? Give feedback.
-
What would be considered a large file in this case? I was planning on using it for EJS template files, CSS stylesheets, and JS scripts file mostly. Is there a way to achieve this without using the To make this work, I made a small HTML file named And these are the console logs I get from this... And if I cannot use the |
Beta Was this translation helpful? Give feedback.
-
|
To answer your follow-up about using The CORS error with The Fix for dev mode: use the Tauri FS API instead of fetch For reading resource files as strings, skip <script>
async function loadTemplate() {
const contents = await __TAURI__.fs.readTextFile('hello.html', {
baseDir: __TAURI__.path.BaseDirectory.Resource
});
alert(contents);
}
</script>Make sure you have the {
"identifier": "fs:allow-read-text-file",
"allow": [{ "path": "$RESOURCE/**" }]
}And add your resource files in {
"bundle": {
"resources": ["hello.html"]
}
}Using The For your use case of loading EJS templates, CSS, and JS files — if they are part of your frontend source ( |
Beta Was this translation helpful? Give feedback.
sure, that should be fine. I'd just recommend to not do the same for very large files. But that's more about bundling itself, not fetch because when you use https://v2.tauri.app/develop/resources/ for the large files you could still use fetch() to get them, just with a different url.