Skip to content

Commit

Permalink
fix: add "use server" directive at the start of built files
Browse files Browse the repository at this point in the history
Next.js needs the `"use server"` directive at the start of index files when creating server
component functions.
  • Loading branch information
TheEdoRan committed Apr 16, 2023
1 parent e1c7816 commit 7f84ac3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
22 changes: 22 additions & 0 deletions fix-use-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// This is needed because Next.js files with server functions must have
// the "use server" directive at the start of file.

const { readFileSync, writeFileSync } = require("fs");
const { join } = require("path");

const pathDir = join(__dirname, "dist");

const main = () => {
const fixFile = (path) => {
let rows = readFileSync(path).toString().split("\n");
rows.unshift('"use server";');
writeFileSync(path, rows.join("\n"));

console.log("Fixed", path);
};

fixFile(join(pathDir, "index.js"));
fixFile(join(pathDir, "index.mjs"));
};

main();
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"scripts": {
"prepare": "is-ci || husky install",
"lint": "tsc && eslint --ext ts .",
"build": "tsup ./src/index.ts --format cjs,esm --dts",
"build": "tsup ./src/index.ts --format cjs,esm --dts; node fix-use-server.js",
"semantic-release": "semantic-release --branches main"
},
"keywords": [
Expand Down
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
"use server";

import { z } from "zod";

type UndefinedKeys<T extends object> = {
Expand Down

0 comments on commit 7f84ac3

Please sign in to comment.