尝试使用静态适配器构建 sveltekit 应用程序时出现此错误: Using @sveltejs/adapter-static TypeError: render2 is not a function
不久前我创建了一个 sveltekit 应用程序并更新了软件包,因为我打算将网站发布到生产环境。然后适配器开始抛出上述错误。
在下面寻找答案
我尝试了一段时间找到解决方案,并在这里找到了解决方案:
https://www.reddit.com/r/SvelteKit/comments/npwc6m/sveltekit_adapternode_error_on_production/
问题是之前版本的 svelte kit 搭建的 hook.ts 文件与新版本不兼容。
长话短说:
处理函数的参数从 { request, render } 更改为 { request, resolve }。
改变渲染解决,问题就解决了。简单的修复但令人沮丧
完整代码如下
import cookie from 'cookie';
import { v4 as uuid } from '@lukeed/uuid';
import type { Handle } from '@sveltejs/kit';
export const handle: Handle = async ({ request, render }) => {
const cookies = cookie.parse(request.headers.cookie || '');
request.locals.userid = cookies.userid || uuid();
// TODO https://github.com/sveltejs/kit/issues/1046
if (request.query.has('_method')) {
request.method = request.query.get('_method').toUpperCase();
}
const response = await render(request);
if (!cookies.userid) {
// if this is the first time the user has visited this app,
// set a cookie so that we recognise them when they return
response.headers['set-cookie'] = `userid=${request.locals.userid}; Path=/; HttpOnly`;
}
return response;
};
将其更改为:
import cookie from 'cookie';
import { v4 as uuid } from '@lukeed/uuid';
import type { Handle } from '@sveltejs/kit';
export const handle: Handle = async ({ request, resolve }) => {
const cookies = cookie.parse(request.headers.cookie || '');
request.locals.userid = cookies.userid || uuid();
// TODO https://github.com/sveltejs/kit/issues/1046
if (request.query.has('_method')) {
request.method = request.query.get('_method').toUpperCase();
}
const response = await resolve(request);
if (!cookies.userid) {
// if this is the first time the user has visited this app,
// set a cookie so that we recognise them when they return
response.headers['set-cookie'] = `userid=${request.locals.userid}; Path=/; HttpOnly`;
}
return response;
};