%% generate tags start %%
#software-engineering
%% generate tags end %%
#software-engineering/astro
I used this script to check server module leakage.
```ts
// Importing necessary modules
import { promises as fs } from "fs";
import path from "path";
// Directory to search and line to find
const DIRECTORY = ".vercel/output/static"; // Replace with your directory path
const LINE_TO_FIND = "This function should only be used in the server side"; // Replace with the line you're searching for
const searchInFile = async (filePath: string): Promise<void> => {
const content = await fs.readFile(filePath, "utf8");
if (content.includes(LINE_TO_FIND)) {
throw new Error(
`🚨 Line found in file ${filePath}. Server module leakage!!!`
);
}
};
const searchDirectory = async (dir: string): Promise<void> => {
const entries = await fs.readdir(dir, { withFileTypes: true });
for (const entry of entries) {
const entryPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
await searchDirectory(entryPath); // Recursive search in subdirectories
} else {
await searchInFile(entryPath); // Search in file
}
}
};
// Main execution
searchDirectory(DIRECTORY)
.then(() => console.log("✨ No occurrences found. No server code leaks."))
.catch((error) => {
console.error(error.message);
process.exit(1);
});
```
then we need to includes these lines in server module. This is only runtime validation. In build time, this has no effect and therefore we need the above scripts.
```ts
if (typeof window !== "undefined") {
throw new Error("This function should only be used in the server side");
}
```