SolidJSS
SolidJSโ€ข17mo agoโ€ข
5 replies
alrightsure

Top level "use server" doesn't seem to work

Hey all, I've noticed that I put "use server" at the top of the file, it doesn't seem to actually treat the functions in the file as server functions, and the actions don't work as expected.

I wanted to ask here before filing an issue, as I feel like maybe I am misunderstanding.

For example, this seems to work:
import { db } from "./db";
import { action, cache, revalidate } from "@solidjs/router";

export const getMessages = cache(async () => {
  "use server";

  const messages = await db.messages.findMany();
  return messages;
  
}, "messages");

export const sendMessage = action(async ({ text }: { text: string }) => {
  "use server";

  await db.messages.create({
    data: { text }
  });

  revalidate(getMessages.keyFor());
});


However, this throws an error when I try to call these functions:
"use server";

import { db } from "./db";
import { action, cache, revalidate } from "@solidjs/router";

export const getMessages = cache(async () => {
  const messages = await db.messages.findMany();
  return messages;
  
}, "messages");

export const sendMessage = action(async ({ text }: { text: string }) => {
  await db.messages.create({
    data: { text }
  });

  revalidate(getMessages.keyFor());
});


Is this intended behavior?
Was this page helpful?