我的 SvelteKit 项目中 "Serverless Function" 经常出现错误
I'm getting an error in "Serverless Function" in my SvelteKit Project often
我不知道为什么有时会收到此错误。
504: GATEWAY_TIMEOUT
Code: FUNCTION_INVOCATION_TIMEOUT
我正在使用加载函数来获取索引页上的帖子。
<script context="module">
export async function load({ page, fetch }) {
const pagination = page.query.get('page') ?? 1;
let PER_PAGE = 2;
// calculate start page
let startPage = +pagination === 1 ? 0 : (+pagination - 1) * PER_PAGE;
// fetch total/count
const totalUrl = `https://myblog.herokuapp.com/posts/count`;
const totalRes = await fetch(totalUrl);
// fecth articles
const url = `https://sveltestrapiblog.herokuapp.com/posts?_sort=created_at:DESC&_start=${startPage}&_limit=${PER_PAGE}`;
const articelRes = await fetch(url);
if (articelRes.ok && totalRes.ok) {
return {
props: {
posts: await articelRes.json(),
total: await totalRes.json(),
pagination: +pagination
}
};
}
return {
status: articelRes.status,
error: new Error(`Could not load ${url}`)
};
}
</script>
不知道怎么回事
当您部署 SvelteKit 应用程序时,它使用无服务器函数来处理传入请求并为每个页面呈现 HTML。当您部署到像 Netlify 或 Vercel 这样的提供商时,它们会限制这些功能可以使用的时间 运行。在 Vercel 上,限制是 5 seconds for the Hobby Plan and Netlify's limit is 10 seconds。如果您的页面呈现时间超过此限制,您将收到 FUNCTION_INVOCATION_TIMEOUT 错误。
我注意到 https://sveltestrapiblog.herokuapp.com/posts 上的 API 在我第一次点击它时需要一些时间才能 return 响应,这可能是因为它在一段时间不活动后才启动。发生这种情况时,如果您花费的时间超过执行时间限制,您将看到错误。您要么需要处理此错误,要么确保您的 API 始终可以在 5 秒内 return 做出响应。
我不知道为什么有时会收到此错误。
504: GATEWAY_TIMEOUT
Code: FUNCTION_INVOCATION_TIMEOUT
我正在使用加载函数来获取索引页上的帖子。
<script context="module">
export async function load({ page, fetch }) {
const pagination = page.query.get('page') ?? 1;
let PER_PAGE = 2;
// calculate start page
let startPage = +pagination === 1 ? 0 : (+pagination - 1) * PER_PAGE;
// fetch total/count
const totalUrl = `https://myblog.herokuapp.com/posts/count`;
const totalRes = await fetch(totalUrl);
// fecth articles
const url = `https://sveltestrapiblog.herokuapp.com/posts?_sort=created_at:DESC&_start=${startPage}&_limit=${PER_PAGE}`;
const articelRes = await fetch(url);
if (articelRes.ok && totalRes.ok) {
return {
props: {
posts: await articelRes.json(),
total: await totalRes.json(),
pagination: +pagination
}
};
}
return {
status: articelRes.status,
error: new Error(`Could not load ${url}`)
};
}
</script>
不知道怎么回事
当您部署 SvelteKit 应用程序时,它使用无服务器函数来处理传入请求并为每个页面呈现 HTML。当您部署到像 Netlify 或 Vercel 这样的提供商时,它们会限制这些功能可以使用的时间 运行。在 Vercel 上,限制是 5 seconds for the Hobby Plan and Netlify's limit is 10 seconds。如果您的页面呈现时间超过此限制,您将收到 FUNCTION_INVOCATION_TIMEOUT 错误。
我注意到 https://sveltestrapiblog.herokuapp.com/posts 上的 API 在我第一次点击它时需要一些时间才能 return 响应,这可能是因为它在一段时间不活动后才启动。发生这种情况时,如果您花费的时间超过执行时间限制,您将看到错误。您要么需要处理此错误,要么确保您的 API 始终可以在 5 秒内 return 做出响应。