Skip to content

Functions are passed as Promises in Server Actions #10

@iamomiid

Description

@iamomiid

Hi!

If you export server actions with memoize function, any call to any other function is going to be promisifed, too.

For example, let's say we have this file:

"use server";
import { desc, eq } from "drizzle-orm";
import { db } from "../db";
import { Posts } from "../db/schema";
import { memoize } from "nextjs-better-unstable-cache";

export const getPostMetadataById = memoize(
	async (id: number) => {
		return db.query.Posts.findFirst({
			where: eq(Posts.id, id),
			columns: { title: true, description: true },
		});
	},
	{
		revalidateTags: (id: number) => ["posts", "metadata", `post-${id}`],
	},
);

In this case, no errors will be thrown but revalidateTags will be an async function and since we're calling it without await the only tag is going to be all.

I think it makes sense to check if functions are async or not in

revalidateTagsFn(...args) : revalidateTagsFn

and

additionalCacheKeyFn(...args) : additionalCacheKeyFn

I can do the fix myself but I just wanted to make sure that I'm not the only one with this issue.

Versions

  • next: 14.2.3
  • nextjs-better-unstable-cache: 1.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions