Node.js platform abstractions and utilities for Remix applications
—
File-based session storage that persists session data to the filesystem with automatic expiration handling and secure session ID generation.
Creates a SessionStorage that stores session data on a filesystem with automatic cleanup of expired sessions.
/**
* Creates a SessionStorage that stores session data on a filesystem
* @param options - Configuration options for file session storage
* @returns SessionStorage instance for managing sessions
*/
function createFileSessionStorage<Data = SessionData, FlashData = Data>(
options: FileSessionStorageOptions
): SessionStorage<Data, FlashData>;
interface FileSessionStorageOptions {
/** The Cookie used to store the session id on the client, or options used to automatically create one */
cookie?: SessionIdStorageStrategy["cookie"];
/** The directory to use to store session files */
dir: string;
}Usage Examples:
import { createFileSessionStorage } from "@remix-run/node";
// Basic file session storage
const sessionStorage = createFileSessionStorage({
dir: "/tmp/sessions"
});
// With custom cookie configuration
const sessionStorage = createFileSessionStorage({
dir: "./app/sessions",
cookie: {
name: "__session",
maxAge: 86400, // 24 hours
httpOnly: true,
secure: process.env.NODE_ENV === "production",
sameSite: "lax",
secrets: ["session-secret-key"]
}
});
// Use in a Remix loader
export async function loader({ request }: LoaderFunctionArgs) {
const session = await sessionStorage.getSession(
request.headers.get("Cookie")
);
const userId = session.get("userId");
return json({ userId });
}
// Use in a Remix action
export async function action({ request }: ActionFunctionArgs) {
const session = await sessionStorage.getSession(
request.headers.get("Cookie")
);
session.set("userId", "user123");
return redirect("/dashboard", {
headers: {
"Set-Cookie": await sessionStorage.commitSession(session)
}
});
}File Organization:
Session files are organized using a two-level directory structure to optimize filesystem performance:
abcd1234567890ef becomes /sessions/abcd/1234567890efSecurity Features:
Storage Operations:
The file session storage handles all standard session operations:
Error Handling:
Performance Considerations:
wx flag to prevent race conditionsInstall with Tessl CLI
npx tessl i tessl/npm-remix-run--node