SvelteKit + Vercel 断路

SvelteKit + Vercel Broken path

我在使用 SvelteKit + vercel 时使用 readdirSync 时遇到问题。站点部署成功,但其中一个服务器端加载函数在路径上失败

如果我使用 npm run dev,则该问题不会在本地出现,并且不会出现在通过 npm run build && npm run preview 进行的本地生产构建中。问题似乎与 readdirsync() 查找的路径有关。但我无法弄清楚如何解决这个问题

[索引].json.js

import fs from 'fs';
import dayjs from 'dayjs';

export function get() {
    let posts = fs
        .readdirSync(`src/posts`) // <-- this path breaks when deployed with vercel
        .filter((fileName) => /.+\.md$/.test(fileName))
        .map((fileName) => {
            const { metadata, content } = process(`src/posts/${fileName}`);
            return {
                content,
                metadata,
                slug: fileName.slice(0, -3)
            };
        });
    // sort the posts by create date.
    posts.sort(
        (a, b) => dayjs(a.metadata.date, 'MMM D, YYYY') - dayjs(b.metadata.date, 'MMM D, YYYY')
    );
    let body = JSON.stringify(posts);

    return {
        body
    };
}

这是我在 Vercel 中看到的日志。很明显路径被破坏可能是由于不正确的基本路径,但我该如何修复它?

[HEAD] /
11:50:50:04
2021-12-20T16:50:50.092Z    d7448652-906d-49d1-b9f0-938984ab2d18    ERROR   Error: ENOENT: no such file or directory, scandir 'src/posts'
    at Object.readdirSync (fs.js:1047:3)
    at get (/var/task/index.js:56304:33)
    at render_endpoint (/var/task/index.js:56582:26)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async resolve (/var/task/index.js:57661:56)
    at async Object.handle (/var/task/index.js:57999:24)
    at async respond (/var/task/index.js:57644:12)
    at async fetch (/var/task/index.js:57182:28)
    at async load2 (/var/task/index.js:56440:17)
    at async load_node (/var/task/index.js:57265:14)
2021-12-20T16:50:50.093Z    d7448652-906d-49d1-b9f0-938984ab2d18    ERROR   SyntaxError: Unexpected token E in JSON at position 0
    at JSON.parse (<anonymous>)
    at Proxy.<anonymous> (/var/task/index.js:57247:31)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async load2 (/var/task/index.js:56440:17)
    at async load_node (/var/task/index.js:57265:14)
    at async respond (/var/task/index.js:57387:22)
    at async render_page (/var/task/index.js:57516:20)
    at async resolve (/var/task/index.js:57661:104)
    at async Object.handle (/var/task/index.js:57999:24)
    at async respond (/var/task/index.js:57644:12)

最终,我选择不使用 readdirSync。我不完全确定为什么它不起作用,但由于我无论如何都在使用 Vite(来自 SvelteKit),我选择尝试使用 import.meta.glob 并且它按预期工作。 API 略有不同。工作代码:

import { slugFromPath } from '$lib/util';

/** @type {import('@sveltejs/kit').RequestHandler} */
export async function get({ query }) {
    const modules = import.meta.glob('./*.{md,svx,svelte.md}');

    const postPromises = [];
    const limit = Number(query.get('limit') ?? Infinity);

    if (Number.isNaN(limit)) {
        return {
            status: 400
        };
    }

    for (let [path, resolver] of Object.entries(modules)) {
        const slug = slugFromPath(path);
        const promise = resolver().then((post) => ({
            slug,
            ...post.metadata
        }));

        postPromises.push(promise);
    }

    const posts = await Promise.all(postPromises);
    const publishedPosts = posts.filter((post) => post.published).slice(0, limit);

    publishedPosts.sort((a, b) => (new Date(a.date) > new Date(b.date) ? -1 : 1));

    return {
        body: publishedPosts.slice(0, limit)
    };