SvelteKit + Vercel 断路

标签 svelte vercel sveltekit

我在使用 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$1 (/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)
    };

关于SvelteKit + Vercel 断路,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70425327/

相关文章:

javascript - 如何在Rollup.js编译时仅获取一个文件夹的文件列表?

javascript - 在 Vercel 上部署 React 应用程序后出现空白屏幕

node.js - Vercel next.js 部署功能日志,无需第三方

cypress - 您的页面仅在 Github Actions 上未在 `load` 内触发其 `60000ms` 事件

node.js - 当后端位于 Docker 容器中时,SvelteKit SSR fetch()

node.js - Sveltekit 端点返回文件共享网站的文件

sveltekit 端点从页面发布错误

javascript - Tailwind 类在 Svelte 应用程序中不起作用

javascript - Vercel Next.js 在预渲染页面时生成错误

javascript - 没有 context=module 脚本标签的预加载函数