Static adapter throwing "Using @sveltejs/adapter-static TypeError: render2 is not a function" while trying to build for production

Static adapter throwing "Using @sveltejs/adapter-static TypeError: render2 is not a function" while trying to build for production

尝试使用静态适配器构建 sveltekit 应用程序时出现此错误: 使用@sveltejs/adapter-static TypeError: render2 不是函数

我刚才创建了一个 sveltekit 应用程序并更新了包,因为我打算将网站发布到生产环境。然后适配器开始抛出上述错误。

寻找下面的答案

找了一段时间的解决方法,在这里找到了解决方法:

https://www.reddit.com/r/SvelteKit/comments/npwc6m/sveltekit_adapternode_error_on_production/

问题是以前版本的 svelte kit 搭建的 hook.ts 文件与新版本不兼容。

长话短说:

处理函数的参数从 { request, render } 更改为 { request, resolve }。

更改render to 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;
};