Skip to content

Commit d43e5f9

Browse files
dharun36rdharun36icyJoseph
authored
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: dharunr36 <[email protected]> Co-authored-by: Joseph <[email protected]>
1 parent aab50c3 commit d43e5f9

File tree

14 files changed

+28
-19
lines changed

14 files changed

+28
-19
lines changed

examples/i18n-routing/app/[lang]/components/counter.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use client";
22

33
import { useState } from "react";
4-
import { type getDictionary } from "../../../get-dictionary";
4+
import { type getDictionary } from "@/get-dictionary";
55

66
export default function Counter({
77
dictionary,

examples/i18n-routing/app/[lang]/components/locale-switcher.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { usePathname } from "next/navigation";
44
import Link from "next/link";
5-
import { i18n, type Locale } from "../../../i18n-config";
5+
import { i18n, type Locale } from "@/i18n-config";
66

77
export default function LocaleSwitcher() {
88
const pathname = usePathname();

examples/i18n-routing/app/[lang]/layout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { i18n, type Locale } from "../../i18n-config";
1+
import { i18n, type Locale } from "@/i18n-config";
22

33
export const metadata = {
44
title: "i18n within app router - Vercel Examples",

examples/i18n-routing/app/[lang]/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { getDictionary } from "../../get-dictionary";
2-
import { Locale } from "../../i18n-config";
1+
import { getDictionary } from "@/get-dictionary";
2+
import { Locale } from "@/i18n-config";
33
import Counter from "./components/counter";
44
import LocaleSwitcher from "./components/locale-switcher";
55

examples/i18n-routing/tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
{
1919
"name": "next"
2020
}
21-
]
21+
],
22+
"paths": {
23+
"@/*": ["./*"]
24+
}
2225
},
2326
"include": [
2427
"next-env.d.ts",

examples/with-grafbase/app/posts/[slug]/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { graphql } from "../../../gql";
2-
import { grafbase } from "../../../lib/grafbase";
1+
import { graphql } from "@/gql";
2+
import { grafbase } from "@/lib/grafbase";
33

44
const GetPostBySlugDocument = graphql(/* GraphQL */ `
55
query GetPostBySlug($slug: String!) {

examples/with-grafbase/tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
{
1919
"name": "next"
2020
}
21-
]
21+
],
22+
"paths": {
23+
"@/*": ["./*"]
24+
}
2225
},
2326
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
2427
"exclude": ["node_modules"]

examples/with-mongodb-mongoose/pages/[id]/edit.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useRouter } from "next/router";
22
import useSWR from "swr";
3-
import Form from "../../components/Form";
3+
import Form from "@/components/Form";
44

55
const fetcher = (url: string) =>
66
fetch(url)

examples/with-mongodb-mongoose/pages/[id]/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { useState } from "react";
22
import { useRouter } from "next/router";
33
import Link from "next/link";
4-
import dbConnect from "../../lib/dbConnect";
5-
import Pet, { Pets } from "../../models/Pet";
4+
import dbConnect from "@/lib/dbConnect";
5+
import Pet, { Pets } from "@/models/Pet";
66
import { GetServerSideProps, GetServerSidePropsContext } from "next";
77
import { ParsedUrlQuery } from "querystring";
88

examples/with-mongodb-mongoose/pages/api/pets/[id].ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { NextApiRequest, NextApiResponse } from "next";
2-
import dbConnect from "../../../lib/dbConnect";
3-
import Pet from "../../../models/Pet";
2+
import dbConnect from "@/lib/dbConnect";
3+
import Pet from "@/models/Pet";
44

55
export default async function handler(
66
req: NextApiRequest,

0 commit comments

Comments
 (0)