How do I include files to my vercel app runtime?

I am wondering how do I include a folder from the root directory of my project (not src) specifically this one: blog/posts at master · MordechaiHadad/blog · GitHub

I have tried many different ways but not a single one worked, all I want is to simply be able to access this folder inside var/task

FIXED

I fixed this issue nice

It’s great to hear you fixed it, @mordechaihadad! What did you end up doing? May be helpful to share for other community members. :smile:

Okay so in essence I changed from reading directly from the filesystem to reading from vite’s bundled imported modules/files in build time which was used with import.meta.glob

Previous Implementation, reading directly from filesystem (matter.read uses readFileSync):

function processPost(path: string) {
  const { data, content } = matter.read(path); // Read with fs module
  ....
}

function getPosts() {
  const postFiles = import.meta.glob('/src/lib/posts/*.svx');
  const baseDir = path.resolve();


  const posts = Object.keys(postFiles).map((filePath) => {
    const absolutePath = path.resolve(baseDir + filePath);
    return processPost(absolutePath);
  });
....
}

Working Implementation:

export const processPost = async (path: string, slug: string): Promise<IPost> => {
    const asset = read(path); // read with svelte's read function
    const text = await asset.text();
    const { data, content } = matter(text);

....
};

export const getPosts = async (): Promise<Posts> => {
    const postFiles = import.meta.glob<string>('../../posts/*.svx', {
        query: '?url',
        eager: true,
        import: "default"
    });

    let posts: Posts = [];
    for (const key in postFiles) {
        const post = await processPost(postFiles[key], slug);
        posts.push(post);
    }

....
};
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.