What is the correct path for arbitrary server side static files?


I am using Svelte.

Where should I read/write files for server static side ?
I generate some files in my +page.server.ts using a lib tool i have in src/lib/utils/foo.ts. Where should the file be written and read from ?

Thanks !

I wrote and read from /tmp/.... and that works but the issue is that i cannot push the file there from github .

The /tmp directory in a serverless environment like Netlify is ephemeral, meaning it’s not persistent storage and files stored there will be deleted after the function execution. Therefore, you cannot push files to this directory from GitHub.

If you need to use certain files in your serverless functions, you have a couple of options:

  1. Include the files in your repository: You can include the files you need in your Git repository, and they will be available in your serverless functions. You can access these files relative to the current working directory.
  2. Fetch the files at runtime: If the files are not too large, you could store them somewhere accessible via HTTP(S) and fetch them at runtime.
  3. Use an external storage service: If the files are large or if you need persistent storage, you might need to use an external storage service like AWS S3.

The file is pre-computed when I push the code to github, i just need to access it during server side rendering.
But at the moment it is not working because the file path relative to the script changes in local vs netlify environment. is there a stable directory i can put the file in for a svelte project?

You should generally try to avoid writing files during your server-side computing. But on production, you can write data to /tmp only. That’s an AWS limitation. On local, you can write data anywhere your user has access to. You can use process.env['NETLIFY_DEV'] === 'true' to set the correct path to read/write data from.