Skip to content

Commit a953624

Browse files
Merge pull request #2692 from appwrite/next-wrong-article
2 parents 79c7677 + d8463e8 commit a953624

File tree

2 files changed

+216
-0
lines changed

2 files changed

+216
-0
lines changed
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
---
2+
layout: post
3+
title: "You're probably using Next.js wrong"
4+
description: Next.js isn't just React with extra steps. Learn when server-side rendering actually matters and how to use it properly with Appwrite.
5+
date: 2026-01-22
6+
cover: /images/blog/using-nextjs-wrong/cover.png
7+
timeToRead: 8
8+
author: atharva
9+
category: tutorial
10+
featured: false
11+
---
12+
13+
Next.js has become the default choice for new React projects. But here's the problem: most developers treat it like React with a fancy router. They spin up a Next.js app, slap `'use client'` on every component, fetch data in `useEffect`, and wonder why when they face problems.
14+
15+
If that sounds familiar, you're shipping bloat for no reason.
16+
17+
# Next.js is not React
18+
19+
React is a UI library. It renders components and manages state. Routing, data fetching, and server logic are your problems.
20+
21+
Next.js is a full-stack framework that uses React for its UI layer. It gives you server-side rendering, static generation, API routes, and server components out of the box. These features exist for specific reasons, and if you're not using them, you're just adding complexity without benefit.
22+
23+
# Does SEO matter?
24+
25+
This is the deciding factor. If search engines need to index your content, you need server-side rendering. Client-rendered pages ship JavaScript that builds the DOM after load. Search crawlers can technically execute JavaScript, but they're inconsistent at it. Your e-commerce product pages, blog posts, and landing pages should render on the server or should be statically pre-rendered.
26+
27+
If you're building an internal dashboard or admin panel that lives behind a login, SEO is irrelevant. Client-side rendering is fine. You could use plain React with Vite and skip the Next.js overhead entirely.
28+
29+
# What server components actually solve
30+
31+
Server components aren't just about SEO. They solve three problems:
32+
33+
## Initial page load
34+
35+
Client components ship JavaScript to the browser, which then fetches data and renders. Users see a loading spinner. Server components fetch data and render HTML before anything reaches the browser. Users see content immediately.
36+
37+
## Bundle size
38+
39+
Every library you import in a client component ends up in your JavaScript bundle. Server components run on the server only. That heavy markdown parser or date library never touches the browser.
40+
41+
## Security
42+
43+
Server components can access databases and secrets directly. They are also capable of accessing environment variables without the `NEXT_PUBLIC_` prefix, since these components are run exclusively on the server.
44+
45+
# Converting a client component to a server component
46+
47+
Here's a typical client-side pattern:
48+
49+
```jsx
50+
"use client";
51+
52+
import { useState, useEffect } from "react";
53+
import { tablesDB } from "@/lib/appwrite";
54+
55+
export default function Products() {
56+
const [products, setProducts] = useState([]);
57+
const [loading, setLoading] = useState(true);
58+
59+
useEffect(() => {
60+
tablesDB
61+
.listRows({
62+
databaseId: DATABASE_ID,
63+
tableId: TABLE_ID,
64+
queries: [Query.equal("status", "active")],
65+
})
66+
.then((res) => setProducts(res.rows))
67+
.finally(() => setLoading(false));
68+
}, []);
69+
70+
if (loading) return <div>Loading...</div>;
71+
return <ProductGrid products={products} />;
72+
}
73+
```
74+
75+
Here's the server component equivalent (without `'use client'`):
76+
77+
```jsx
78+
import { createAdminClient } from "@/lib/appwrite/server";
79+
80+
export default async function Products() {
81+
const { tablesDB } = await createAdminClient();
82+
const { rows } = await tablesDB.listRows({
83+
databaseId: DATABASE_ID,
84+
tableId: TABLE_ID,
85+
queries: [Query.equal("status", "active")],
86+
});
87+
88+
return <ProductGrid products={rows} />;
89+
}
90+
```
91+
92+
# Setting up Appwrite for server-side rendering
93+
94+
The Appwrite Web SDK runs in browsers. For server components, you need the Node SDK:
95+
96+
```bash
97+
npm install node-appwrite
98+
```
99+
100+
Create two client factories. The admin client uses an API key for public data:
101+
102+
```ts
103+
// lib/appwrite/server.ts
104+
import { Client, TablesDB, Account } from "node-appwrite";
105+
106+
export async function createAdminClient() {
107+
const client = new Client()
108+
.setEndpoint(process.env.APPWRITE_ENDPOINT)
109+
.setProject(process.env.APPWRITE_PROJECT_ID)
110+
.setKey(process.env.APPWRITE_API_KEY);
111+
112+
return {
113+
tablesDB: new TablesDB(client),
114+
account: new Account(client),
115+
};
116+
}
117+
```
118+
119+
The session client uses the logged-in user's session for protected data:
120+
121+
```ts
122+
import { Client, TablesDB, Account } from "node-appwrite";
123+
import { cookies } from "next/headers";
124+
125+
export async function createSessionClient() {
126+
const client = new Client()
127+
.setEndpoint(process.env.APPWRITE_ENDPOINT)
128+
.setProject(process.env.APPWRITE_PROJECT_ID);
129+
130+
const session = (await cookies()).get("session");
131+
if (session) {
132+
client.setSession(session.value);
133+
}
134+
135+
return {
136+
tablesDB: new TablesDB(client),
137+
account: new Account(client),
138+
};
139+
}
140+
```
141+
142+
The difference matters. Admin client queries return all rows matching your query. Session client queries return only rows the user has permission to access.
143+
144+
# Handling authentication
145+
146+
Login with a server action:
147+
148+
```tsx
149+
// app/login/page.tsx
150+
import { cookies } from "next/headers";
151+
import { redirect } from "next/navigation";
152+
import { createAdminClient } from "@/lib/appwrite/server";
153+
154+
export default function LoginPage() {
155+
async function login(formData: FormData) {
156+
"use server";
157+
const { account } = await createAdminClient();
158+
const session = await account.createEmailPasswordSession(
159+
formData.get("email") as string,
160+
formData.get("password") as string
161+
);
162+
163+
(await cookies()).set("session", session.secret, {
164+
httpOnly: true,
165+
secure: true,
166+
sameSite: "strict",
167+
expires: new Date(session.expire),
168+
});
169+
170+
redirect("/");
171+
}
172+
173+
return (
174+
<form action={login}>
175+
<input name="email" type="email" required />
176+
<input name="password" type="password" required />
177+
<button type="submit">Log in</button>
178+
</form>
179+
);
180+
}
181+
```
182+
183+
Protect routes with a layout:
184+
185+
```tsx
186+
// app/(protected)/layout.tsx
187+
import { redirect } from "next/navigation";
188+
import { createSessionClient } from "@/lib/appwrite/server";
189+
190+
export default async function ProtectedLayout({ children }) {
191+
try {
192+
const { account } = await createSessionClient();
193+
await account.get();
194+
} catch {
195+
redirect("/login");
196+
}
197+
198+
return children;
199+
}
200+
```
201+
202+
# When to skip all of this
203+
204+
Use plain React when:
205+
206+
- Your app lives behind authentication
207+
- Search engines don't need to index it
208+
- You prefer client-side data fetching patterns
209+
- You're building a very simple app that doesn't need server-capabilities
210+
211+
There's nothing wrong with client-side rendering. The mistake is using Next.js and not leveraging what makes it useful.
212+
213+
# Resources
214+
215+
- [Appwrite SSR documentation](/docs/products/auth/server-side-rendering)
216+
- [Next.js App Router documentation](https://nextjs.org/docs/app)
628 KB
Loading

0 commit comments

Comments
 (0)