Unverified Commit d43e5f93 authored by Dharun R's avatar Dharun R Committed by GitHub

docs: Convert relative imports to @ aliases in examples (#83813)

## For Contributors

### Improving Documentation

This PR improves the Next.js examples by converting relative imports to
@ aliases, making them more readable and maintainable.

## Description

Fixes #83717 by converting deep relative imports to clean @ aliases in
Next.js examples.

Currently, Next.js examples use long relative imports like:
```typescript
import { i18n, type Locale } from "../../../i18n-config";
```
This PR updates them to use clean @ aliases:
```typescript
import { i18n, type Locale } from "@/i18n-config";
```

## Changes Made

- **Added path alias configuration** `"@/*": ["./*"]` to tsconfig.json
files
- **Converted deep relative imports** (d:) to clean @ aliases (`@/`)
- **Updated examples**:
  - `i18n-routing` (specifically mentioned in issue #83717) - 5 files
  - `with-typescript` - 2 files  
  - `with-grafbase` - 2 files
  - `with-mongodb-mongoose` - 2 files

## Benefits

-  **Improved readability**: `@/config` vs `../../../config`
-  **Reduced errors**: No need to count `../` levels when refactoring
-  **Better maintainability**: Easier to move files around
-  **Modern standards**: Aligns with production Next.js practices
-  **Better IDE support**: Enhanced autocomplete and navigation

## Examples of Changes

### Before:
```typescript
import { i18n, type Locale } from "../../../i18n-config";
import { getDictionary } from "../../get-dictionary";
import { grafbase } from "../../../lib/grafbase";
```

### After:
```typescript
import { i18n, type Locale } from "@/i18n-config";
import { getDictionary } from "@/get-dictionary";
import { grafbase } from "@/lib/grafbase";
```

## Testing

-  TypeScript compilation works correctly
-  @ imports resolve properly in IDE
-  Runtime functionality preserved
-  No breaking changes to example functionality

This change is purely a developer experience improvement and doesn't
affect runtime behavior.

---------
Co-authored-by: 's avatardharunr36 <dharunr.23aid@kongu.edu>
Co-authored-by: 's avatarJoseph <joseph.chamochumbi@vercel.com>
parent aab50c39
"use client";
import { useState } from "react";
import { type getDictionary } from "../../../get-dictionary";
import { type getDictionary } from "@/get-dictionary";
export default function Counter({
dictionary,
......
......@@ -2,7 +2,7 @@
import { usePathname } from "next/navigation";
import Link from "next/link";
import { i18n, type Locale } from "../../../i18n-config";
import { i18n, type Locale } from "@/i18n-config";
export default function LocaleSwitcher() {
const pathname = usePathname();
......
import { i18n, type Locale } from "../../i18n-config";
import { i18n, type Locale } from "@/i18n-config";
export const metadata = {
title: "i18n within app router - Vercel Examples",
......
import { getDictionary } from "../../get-dictionary";
import { Locale } from "../../i18n-config";
import { getDictionary } from "@/get-dictionary";
import { Locale } from "@/i18n-config";
import Counter from "./components/counter";
import LocaleSwitcher from "./components/locale-switcher";
......
......@@ -18,7 +18,10 @@
{
"name": "next"
}
]
],
"paths": {
"@/*": ["./*"]
}
},
"include": [
"next-env.d.ts",
......
import { graphql } from "../../../gql";
import { grafbase } from "../../../lib/grafbase";
import { graphql } from "@/gql";
import { grafbase } from "@/lib/grafbase";
const GetPostBySlugDocument = graphql(/* GraphQL */ `
query GetPostBySlug($slug: String!) {
......
......@@ -18,7 +18,10 @@
{
"name": "next"
}
]
],
"paths": {
"@/*": ["./*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
......
import { useRouter } from "next/router";
import useSWR from "swr";
import Form from "../../components/Form";
import Form from "@/components/Form";
const fetcher = (url: string) =>
fetch(url)
......
import { useState } from "react";
import { useRouter } from "next/router";
import Link from "next/link";
import dbConnect from "../../lib/dbConnect";
import Pet, { Pets } from "../../models/Pet";
import dbConnect from "@/lib/dbConnect";
import Pet, { Pets } from "@/models/Pet";
import { GetServerSideProps, GetServerSidePropsContext } from "next";
import { ParsedUrlQuery } from "querystring";
......
import { NextApiRequest, NextApiResponse } from "next";
import dbConnect from "../../../lib/dbConnect";
import Pet from "../../../models/Pet";
import dbConnect from "@/lib/dbConnect";
import Pet from "@/models/Pet";
export default async function handler(
req: NextApiRequest,
......
import { NextApiRequest, NextApiResponse } from "next";
import dbConnect from "../../../lib/dbConnect";
import Pet from "../../../models/Pet";
import dbConnect from "@/lib/dbConnect";
import Pet from "@/models/Pet";
export default async function handler(
req: NextApiRequest,
......
......@@ -15,7 +15,7 @@
"jsx": "react-jsx",
"incremental": true,
"paths": {
"@/*": ["./src/*", "./lib/*"]
"@/*": ["./*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
......
import { NextApiRequest, NextApiResponse } from "next";
import { sampleUserData } from "../../../utils/sample-data";
import { sampleUserData } from "@/utils/sample-data";
const handler = (_req: NextApiRequest, res: NextApiResponse) => {
try {
......
......@@ -12,7 +12,10 @@
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "react-jsx"
"jsx": "react-jsx",
"paths": {
"@/*": ["./*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment