Skip to main content

Posts

Showing posts with the label TypeScript

Securing Next.js 15 Server Actions: Preventing Data Leaks & CSRF

  Next.js Server Actions have fundamentally changed how we write full-stack React applications by collapsing the boundary between client and server. However, this convenience introduces a critical misconception: treating Server Actions as internal JavaScript functions. They are not internal functions. Every Server Action is a public-facing HTTP endpoint. If you treat a Server Action like a standard utility function, you inadvertently expose your database logic to the public internet. Without strict input validation and output sanitization, you risk Mass Assignment vulnerabilities, IDOR (Insecure Direct Object References), and leaking sensitive schema details to the client. This guide analyzes the root causes of Server Action vulnerabilities and provides a reusable, type-safe architecture to secure them in Next.js 15. The Anatomy of the Vulnerability To understand the security risk, we must look at how Next.js compiles Server Actions. When you add the  "use server"  direct...

How to Stream LangChain Responses in Next.js 15 (App Router Guide)

  You have set up your Next.js 15 application, configured your LangChain chains, and everything works perfectly in the console. But when you connect it to your React frontend, the application hangs. The user stares at a loading spinner for five seconds, and then the entire response snaps into existence at once. This destroys the User Experience. The "magic" of LLMs lies in the  token-by-token streaming effect —the typewriter illusion that makes the AI feel alive and responsive. Achieving this in the App Router is surprisingly difficult. You are battling three adversaries: the serialization boundary between React Server Components (RSC) and the client, the mismatch between LangChain’s async iterables and standard Web Streams, and the strict typing of TypeScript. This guide provides a production-grade, rigorous solution to implement real-time streaming using Next.js 15 Route Handlers and LangChain. The Root Cause: Why Streaming Breaks To fix the problem, we must understand the ...