abann sunny
abann sunny

Reputation: 998

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

Trying to build sveltekit application using the static adapter I get this error: Using @sveltejs/adapter-static TypeError: render2 is not a function

I created a sveltekit application a while ago and updated the packages since I was going to publish the website to production. and then the adapter started throwing the above error.

Look for the answer below

Upvotes: 0

Views: 1068

Answers (1)

abann sunny
abann sunny

Reputation: 998

I tried to find a solution for a while and found the solution here:

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

The problem was that the hook.ts file scaffolded by a previous version of svelte kit was not compatible with the new version.

Long story short:

handle function's parameter changed from { request, render } to { request, resolve }.

change the render to resolve and the problem is solved. simple fix but frustrating

full code below

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;
};

change that to :

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;
};

Upvotes: 0

Related Questions