Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions next-server-actions/app/layout.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// app/layout.tsx

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Remove duplicate layout file for app segment

This adds a second layout file (layout.jsx) in the same app directory where layout.tsx already exists. Next’s file-based routing expects exactly one layout per segment; having both extensions in the same folder results in a “duplicate layout” conflict or unpredictable file selection at build/dev time. Please remove the extra file or keep only one layout implementation.

Useful? React with 👍 / 👎.

export const metadata = {
title: 'Your App',
description: '…',
};
export default function RootLayout({ children, }) {
return (<html lang="en"><body>{children}</body>
</html>);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const metadata = {

export default function RootLayout({
children,
}) {
}: { children: React.ReactNode }) {
return (
<html lang="en"><body>{children}</body>
</html>
Expand Down
35 changes: 35 additions & 0 deletions next-server-actions/app/page.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import mongoose from 'mongoose';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Avoid duplicate page files in app directory

This introduces page.jsx alongside the existing page.tsx in the same app directory. Next routes require a single page file per segment; multiple page.* files can trigger a duplicate page error or cause the wrong file to be used at runtime. Please remove one of the two so there’s a single source of truth.

Useful? React with 👍 / 👎.

import { revalidatePath } from 'next/cache';
mongoose.connect('mongodb://127.0.0.1:27017/mongoose_test');
Copy link

Copilot AI Jan 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mongoose.connect() call is not awaited, which means the connection may not be established when the User model is used. This could lead to runtime errors when trying to query or create users before the connection is ready. Either await this call or handle the connection promise appropriately.

Suggested change
mongoose.connect('mongodb://127.0.0.1:27017/mongoose_test');
await mongoose.connect('mongodb://127.0.0.1:27017/mongoose_test');

Copilot uses AI. Check for mistakes.
const userSchema = new mongoose.Schema({
name: { type: String, required: true, trim: true, minLength: 6 }
}, { timestamps: true });
const User = mongoose.model('User', userSchema, 'users', { overwriteModels: true });
// --- Server Action ---
async function addUser(formData) {
'use server';
const name = String(formData.get('name') || '').trim();
await User.create({ name });
revalidatePath('/');
}
// --- Page ---
export default async function Page() {
const users = await User.find().sort({ _id: -1 }).lean();
return (<main style={{ fontFamily: 'sans-serif', maxWidth: 480, margin: '2rem auto' }}>
<h1 style={{ marginBottom: '0.75rem' }}>Users</h1>

<form action={addUser} style={{ marginBottom: '1rem', display: 'flex', gap: '0.5rem' }}>
<input name="name" placeholder="Enter name" required style={{ flex: 1, padding: '0.5rem' }}/>
<button type="submit" style={{ padding: '0.5rem 0.9rem' }}>Add</button>
</form>

<ul style={{ paddingLeft: '1rem' }}>
{users.map((u) => {
var _a, _b;
return (<li key={(_b = (_a = u._id) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : ''} style={{ margin: '0.25rem 0' }}>
{u.name}
</li>);
})}
</ul>
</main>);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import mongoose from 'mongoose';
import { revalidatePath } from 'next/cache';

await mongoose.connect('mongodb://127.0.0.1:27017/mongoose_test');
mongoose.connect('mongodb://127.0.0.1:27017/mongoose_test');

const userSchema = new mongoose.Schema(
{
Expand All @@ -13,7 +13,7 @@ const userSchema = new mongoose.Schema(
const User = mongoose.model('User', userSchema, 'users', { overwriteModels: true });

// --- Server Action ---
async function addUser(formData) {
async function addUser(formData: FormData) {
'use server';
const name = String(formData.get('name') || '').trim();
await User.create({ name });
Expand All @@ -40,7 +40,7 @@ export default async function Page() {

<ul style={{ paddingLeft: '1rem' }}>
{users.map((u) => (
<li key={u._id} style={{ margin: '0.25rem 0' }}>
<li key={u._id?.toString() ?? ''} style={{ margin: '0.25rem 0' }}>
{u.name}
</li>
))}
Expand Down
7 changes: 7 additions & 0 deletions next-server-actions/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />
/// <reference types="next/navigation-types/compat/navigation" />
import "./.next/dev/types/routes.d.ts";

// NOTE: This file should not be edited
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
9 changes: 7 additions & 2 deletions next-server-actions/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
{
"dependencies": {
"@mongoosejs/studio": "^0.1.4",
"mongoose": "^9.0.0-0",
"next": "^16.0.2"
"@types/node": "^25.0.3",
"@types/react": "^19.2.7",
"@types/react-dom": "^19.2.3",
"mongoose": "^8.19.0",
"next": "^16.0.2",
"typescript": "^5.9.3"
},
"scripts": {
"build": "tsc",
"start": "next dev"
}
}
35 changes: 35 additions & 0 deletions next-server-actions/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"compilerOptions": {
"target": "ES2017",
"strict": true,
"skipLibCheck": true,
"moduleResolution": "bundler",
"isolatedModules": true,
"jsx": "react-jsx",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"noEmit": true,
"incremental": true,
"module": "esnext",
"esModuleInterop": true,
"resolveJsonModule": true,
"plugins": [
{
"name": "next"
}
]
},
"include": [
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts",
".next/dev/types/**/*.ts"
],
"exclude": [
"node_modules"
]
}