Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/support-server-client-only.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/start": minor
---

Add support for `server-only` and `client-only` modules
12 changes: 12 additions & 0 deletions packages/start/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,15 @@ declare namespace App {
[key: string | symbol]: any;
}
}

/**
* Import `server-only` to ensure this module is never bundled for the client.
* Importing it in a client module will throw a build error.
*/
declare module "server-only" {}

/**
* Import `client-only` to ensure this module is never bundled for the server.
* Importing it in a server module will throw a build error.
*/
declare module "client-only" {}
23 changes: 23 additions & 0 deletions packages/start/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,29 @@ export function solidStart(options?: SolidStartOptions): Array<PluginOption> {
},
filter: options?.serverFunctions?.filter,
}),
{
name: "solid-start:boundary-modules",
enforce: "pre",
resolveId(id, importer, { ssr }) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is all that is needed?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I have already tested it.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a known limitation: when a client component imports a file that uses server-only, even if all functions in that file are marked with "use server", the build will incorrectly treat this as the client component directly calling server-only functions and throw an error. The only workaround is to apply "use server" at the file level.

However, "use server" already guarantees that the functions execute on the server — in that case, server-only is redundant. server-only is primarily meant to protect functions that must run on the server and cannot be called directly from outside (i.e., not exposed as RPC endpoints).

if (id === "server-only") {
if (!ssr)
this.error(
`Attempt to import 'server-only' in a client module: ${importer}`,
);
} else if (id === "client-only") {
if (ssr)
this.error(
`Attempt to import 'client-only' in a server module: ${importer}`,
);
} else {
return null;
}
return "\0solid-start:boundary-modules:id";
},
load(id) {
if (id === "\0solid-start:boundary-modules:id") return "export {}";
},
},
{
name: "solid-start:virtual-modules",
async resolveId(id) {
Expand Down
Loading