Unit 1: Fundamentals of Next.js and Routing - Subjective Questions
INT257 — Modern Web Application Development • Practice Questions with Detailed Answers
20 questions
What is Next.js, and how does it extend the capabilities of React for modern web application development?
Next.js is a React-based framework used to build full-stack, production-ready web applications. It provides built-in features that are not included in React alone.
Key capabilities include:
- File-based routing: Application routes are created through files and folders.
- Server-side rendering: Pages can be rendered on the server before being sent to the browser.
- Static site generation: Pages can be generated at build time for improved performance.
- Server Components: Components can execute on the server and reduce the amount of JavaScript sent to the client.
- API and backend support: Next.js can support server-side logic and request handlers.
- Image and font optimization: Built-in tools improve loading performance.
- Layouts and loading states: Applications can define shared layouts and route-specific loading interfaces.
React primarily focuses on building user interfaces, whereas Next.js provides an application framework containing routing, rendering, optimization, and deployment features.
Explain the major features of Next.js that make it suitable for developing scalable web applications.
The major features of Next.js include:
- Multiple rendering strategies: Next.js supports server-side rendering, static site generation, incremental static regeneration, and client-side rendering.
- App Router: The App Router organizes routes using the
appdirectory and supports layouts, nested routes, loading interfaces, and error boundaries. - File-based routing: Developers create routes by creating appropriately named folders and files.
- Server Components: Components can run on the server by default, improving performance and reducing client-side JavaScript.
- Dynamic routing: Routes can accept variable path segments such as
/products/[id]. - Automatic code splitting: Only the JavaScript required for a particular route is loaded.
- Built-in optimization: Next.js provides optimized image, font, and script handling.
- Fast development workflow: Fast Refresh updates the browser while preserving relevant component state.
- Production support: Applications can be built, tested, and deployed using standardized commands.
Together, these features improve developer productivity, application performance, maintainability, and scalability.
Describe the typical project structure of a Next.js application using the App Router and explain the purpose of its important files and directories.
A typical App Router project may contain the following structure:
app/: Contains route segments, layouts, pages, loading interfaces, and error boundaries.app/layout.js: The root layout shared by all routes. It usually contains the HTML and body structure.app/page.js: Defines the home page at/.public/: Stores static assets that can be accessed directly through URLs.components/: Commonly used for reusable user-interface components, although this directory is convention-based.lib/: Often contains utility functions, data-access logic, or configuration helpers.next.config.js: Stores project-level Next.js configuration.package.json: Lists dependencies and defines scripts such as development, build, and start commands.node_modules/: Contains installed dependencies and is normally not committed to version control..envfiles: Store environment-specific configuration values.
The exact structure can vary, but the app directory is central to routing and application composition when the App Router is used.
What is the App Router in Next.js? Explain its working principles and advantages over a basic page-oriented routing approach.
The App Router is the routing system introduced in modern versions of Next.js. It uses the app directory to define routes and application structure.
Its working principles include:
- Each folder inside
apprepresents a route segment. - A
page.jsfile makes a folder accessible as a page. - A
layout.jsfile defines persistent user-interface structure for a route segment. - A
loading.jsfile displays temporary loading content. - An
error.jsfile handles errors within a route segment. - Components are Server Components by default unless the
use clientdirective is added. - Nested folders create nested URL paths and nested user-interface boundaries.
Advantages include:
- Better support for nested layouts and shared interface elements.
- Improved streaming and loading experiences.
- Clear separation of route-level concerns.
- Reduced client-side JavaScript through Server Components.
- More convenient handling of errors and asynchronous data loading.
The App Router treats routing as part of the overall component and rendering architecture rather than only as a URL-to-component mapping mechanism.
Describe the standard development workflow for creating, testing, and deploying a Next.js application.
A standard Next.js development workflow consists of the following stages:
- Project creation: Create an application using a project generator or an existing starter project.
- Dependency installation: Install the required packages using the selected package manager.
- Local development: Run the development server, commonly with
npm run dev, and inspect the application in a browser. - Feature development: Add pages, components, layouts, data-fetching logic, and styles.
- Fast Refresh: Observe code changes immediately without manually restarting the server.
- Testing and debugging: Check navigation, loading states, error states, responsiveness, and browser-console messages.
- Production build: Run
npm run buildto compile and optimize the application. - Production execution: Run
npm run startto test the production build locally. - Deployment: Deploy the built application to a compatible hosting platform.
Environment variables, source control, linting, and automated testing should also be included in a professional workflow.
Explain file-based routing in Next.js with suitable examples for creating home, about, and contact routes.
File-based routing means that routes are generated from the folder and file structure of the project. In the App Router, each route folder must generally contain a page.js, page.jsx, or equivalent page file.
Example structure:
app/page.jscreates the home route/.app/about/page.jscreates the route/about.app/contact/page.jscreates the route/contact.
A simplified example is:
app/
page.js
about/
page.js
contact/
page.jsThe route path is formed from the folder hierarchy. Therefore, the about/page.js file is rendered when the user visits /about. Developers do not usually need to create a separate route configuration file for these routes. This approach makes the relationship between the project structure and the URL structure clear and easy to maintain.
What are dynamic routes in Next.js? Explain how a dynamic route can be used to display different product details.
A dynamic route contains a variable path segment whose value is supplied through the URL. In the App Router, dynamic segments are represented using square brackets.
For example:
app/products/[id]/page.jsThis route can match URLs such as:
/products/101/products/202/products/phone
The value of the segment is available through route parameters. A page can use the parameter to retrieve and display the corresponding product.
Conceptually, the page receives a parameter object containing an id value. The application can then:
- Read the product identifier.
- Query a database or external service.
- Display the matching product information.
- Show an appropriate not-found response when the product does not exist.
Dynamic routes prevent developers from creating a separate physical page for every product while still supporting unique URLs.
Distinguish between static routes and dynamic routes in Next.js. Include examples and suitable use cases for each.
Static and dynamic routes differ in how their URL paths are defined.
Static routes:
- Have a fixed URL path.
- Are created using ordinary folders.
- Example:
app/about/page.jsmaps to/about. - Suitable for pages such as About, Contact, Services, and Dashboard.
Dynamic routes:
- Contain one or more variable URL segments.
- Use square-bracket notation in folder names.
- Example:
app/blog/[slug]/page.jscan match/blog/next-routing. - Suitable for product pages, blog posts, user profiles, and articles.
For example, /about is a static route because its path never changes. In contrast, /products/[id] is a dynamic route because the id value changes for each product. Static routes are useful when the content is tied to a known section, whereas dynamic routes are useful when one page template represents many data records.
Explain nested routes in Next.js and show how a multi-level route such as /dashboard/settings/profile can be created.
Nested routes are routes created through multiple levels of folders inside the app directory. Each folder contributes one segment to the final URL.
For the route /dashboard/settings/profile, the structure can be:
app/
dashboard/
settings/
profile/
page.jsThe page.js file at the deepest level renders the profile page. The folder hierarchy produces the following route segments:
dashboardbecomes the first segment.settingsbecomes the second segment.profilebecomes the third segment.
The resulting URL is /dashboard/settings/profile. Nested routes are useful for organizing related areas such as administration panels, account settings, documentation, and content categories. Each segment can also define its own layout, loading state, and error boundary.
Describe layouts in the Next.js App Router. How do root layouts and nested layouts improve application design?
A layout is a user-interface component that wraps one or more pages and remains persistent while users navigate within its route segment.
A root layout is commonly defined as:
app/layout.jsIt usually provides:
- The
<html>and<body>elements. - Global styles.
- Application-wide providers.
- Persistent navigation or other shared interface elements.
A nested layout can be defined inside a route folder, for example:
app/dashboard/layout.jsThis layout is shared by pages under the dashboard route, such as /dashboard, /dashboard/reports, and /dashboard/settings.
Layouts improve application design by:
- Avoiding repeated markup.
- Preserving shared interface state during navigation.
- Organizing route-specific navigation.
- Supporting consistent visual structure.
- Allowing different sections of an application to have different shells.
Layouts are especially useful for sidebars, headers, authentication providers, and dashboard navigation.
Compare a root layout with a nested layout in Next.js, and explain how both layouts are composed for a deeply nested route.
A root layout applies to the entire application, while a nested layout applies only to a specific route segment and its descendants.
Root layout:
- Located directly inside
app. - Wraps every route in the application.
- Commonly contains document-level elements and global providers.
Nested layout:
- Located inside a route folder such as
app/dashboard/layout.js. - Wraps only pages under that route segment.
- Commonly contains section-specific navigation or controls.
For a route such as /dashboard/reports, Next.js composes the interface conceptually as:
RootLayout
DashboardLayout
ReportsPageThe root layout supplies global structure, the dashboard layout supplies dashboard-specific structure, and the page supplies the final route content. This composition allows developers to reuse common elements while keeping each application section independently organized.
Explain the different methods of navigation available in a Next.js application and state when each method should be used.
Next.js supports several navigation methods:
Linkcomponent: Used for normal navigation between internal routes. It supports client-side navigation and can improve performance through prefetching.- Programmatic navigation: The router API can be used when navigation must occur after an event, form submission, authentication result, or other logic.
- Server-side redirects: Redirect functions can send users to another route during server execution, such as redirecting unauthenticated users.
- Browser history controls: Back and forward operations can be triggered through router functionality when appropriate.
- Direct URL entry: Users can navigate by entering a route directly in the browser, provided the server is configured to handle the route.
For ordinary links, the Link component is generally preferred. Programmatic navigation is suitable for event-driven transitions, while redirects are appropriate when access or processing conditions require a route change.
What is client-side navigation in Next.js, and how does it differ from a traditional full-page browser navigation?
Client-side navigation changes the displayed route without reloading the entire browser document. In Next.js, this is commonly achieved with the Link component or router APIs.
During client-side navigation:
- The browser does not perform a complete document refresh.
- Next.js requests or loads the data and code required for the destination route.
- Shared layouts can remain mounted.
- Some component state can be preserved.
- The transition is typically faster and smoother.
In a traditional full-page navigation:
- The browser requests a new document from the server.
- The current page is discarded.
- Global interface elements may be recreated.
- More resources may need to be downloaded again.
Client-side navigation improves the user experience, but the application must still support direct requests to routes so that refreshing or opening a URL directly works correctly.
Explain the purpose of loading.js in the Next.js App Router and describe how it improves the user experience during asynchronous rendering.
The loading.js file defines a route-level loading interface that is displayed while the corresponding route content is being rendered or its data is being prepared.
For example:
app/dashboard/loading.jsThis loading interface applies to the dashboard route segment and can display:
- A progress indicator.
- Skeleton placeholders.
- A temporary message.
- A partial layout that communicates the expected content.
It improves the user experience by:
- Providing immediate visual feedback.
- Preventing the interface from appearing unresponsive.
- Supporting streaming of route content.
- Making slow data-fetching operations easier to understand.
- Allowing users to continue seeing persistent layouts while only the pending section changes.
A good loading interface should be brief, relevant to the expected content, and visually consistent with the final page.
What is an error UI in Next.js? Explain the role of error.js and the requirements for using an error boundary.
An error UI is a fallback interface displayed when an error occurs while rendering a route segment or processing an operation associated with that segment.
In the App Router, an error.js file defines this fallback interface. It acts as an error boundary for the route segment in which it is placed and its child segments.
An error UI should generally:
- Explain that the requested content could not be displayed.
- Provide a way to retry the failed operation.
- Avoid exposing sensitive technical details.
- Preserve unaffected parts of the application where possible.
- Offer navigation to a stable page when recovery is not possible.
Because error boundaries commonly need interactive recovery controls, the error component is typically a Client Component. The retry action can request the failed segment to render again without requiring a complete application restart.
Differentiate between loading UI and error UI in the Next.js App Router. Explain how both contribute to resilient application interfaces.
Loading UI and error UI represent different states of a route.
Loading UI:
- Appears while content is being generated or data is being loaded.
- Indicates that the operation is still in progress.
- Is commonly implemented using
loading.js. - May contain skeletons, spinners, or placeholder content.
Error UI:
- Appears when an operation fails or a rendering error occurs.
- Indicates that the expected content could not be produced.
- Is commonly implemented using
error.js. - May contain retry and recovery actions.
Together, they create a more resilient interface. Loading UI prevents users from interpreting delay as failure, while error UI provides a controlled response when failure actually occurs. Without these states, users may see blank areas, abrupt browser errors, or an interface that appears frozen.
Design a suitable route structure for a blog application containing a home page, an article listing page, individual article pages, and an author profile section.
A suitable App Router structure is:
app/
page.js
articles/
page.js
[slug]/
page.js
authors/
[username]/
page.jsThe resulting routes are:
app/page.jsmaps to/.app/articles/page.jsmaps to/articles.app/articles/[slug]/page.jsmaps to routes such as/articles/nextjs-routing.app/authors/[username]/page.jsmaps to routes such as/authors/alex.
The structure uses static routes for known sections and dynamic routes for data-driven pages. A shared app/articles/layout.js could contain article-related navigation, while loading.js and error.js could be added to provide route-specific loading and recovery states. This organization keeps related routes grouped and makes the URL hierarchy easy to understand.
Explain how dynamic routes and nested routes can be combined to create a route such as /courses/nextjs/lessons/1.
Dynamic and nested routes can be combined by placing a dynamic folder inside a nested folder hierarchy.
One possible structure is:
app/
courses/
[courseSlug]/
lessons/
[lessonId]/
page.jsThis structure creates routes such as:
/courses/nextjs/lessons/1/courses/react/lessons/4
The route contains two dynamic values:
courseSlugidentifies the course.lessonIdidentifies the lesson within that course.
The page can use both values to retrieve the correct course and lesson. Nested layouts may also be added at courses, [courseSlug], or lessons levels to provide shared course navigation and lesson controls. This approach supports scalable content hierarchies without requiring a separate hard-coded page for every course and lesson.
Explain the difference between Server Components and Client Components in the context of the Next.js App Router.
In the App Router, components are Server Components by default. They execute on the server and can be used to prepare content before it reaches the browser.
Server Components:
- Reduce client-side JavaScript.
- Can securely access server-side resources when configured correctly.
- Are suitable for data retrieval and static interface composition.
- Cannot directly use browser-only APIs or client-side interaction hooks.
Client Components:
- Are enabled by placing the
use clientdirective at the top of the file. - Can use state and event handlers.
- Can access browser APIs such as local storage when appropriate.
- Are required for highly interactive interface elements.
- Add JavaScript that must be sent to the browser.
Developers should keep components on the server when interaction is unnecessary and use Client Components only for areas that require browser-side state, events, or APIs.
Describe the purpose of route groups in Next.js and explain how they can organize routes without changing the public URL.
Route groups are folders whose names are enclosed in parentheses, such as (marketing) or (admin). They help organize routes internally without adding the group name to the URL.
Example:
app/
(marketing)/
about/
page.js
(admin)/
dashboard/
page.jsThe resulting routes are:
app/(marketing)/about/page.jsmaps to/about.app/(admin)/dashboard/page.jsmaps to/dashboard.
Route groups are useful for:
- Separating public and authenticated sections.
- Applying different layouts to different areas.
- Organizing large projects by feature or business domain.
- Keeping implementation folders separate from public URL design.
Because the group name is omitted from the URL, developers can improve project organization without changing links visible to users.
What is Next.js, and how does it extend the capabilities of React for modern web application development?
Next.js is a React-based framework used to build full-stack, production-ready web applications. It provides built-in features that are not included in React alone.
Key capabilities include:
- File-based routing: Application routes are created through files and folders.
- Server-side rendering: Pages can be rendered on the server before being sent to the browser.
- Static site generation: Pages can be generated at build time for improved performance.
- Server Components: Components can execute on the server and reduce the amount of JavaScript sent to the client.
- API and backend support: Next.js can support server-side logic and request handlers.
- Image and font optimization: Built-in tools improve loading performance.
- Layouts and loading states: Applications can define shared layouts and route-specific loading interfaces.
React primarily focuses on building user interfaces, whereas Next.js provides an application framework containing routing, rendering, optimization, and deployment features.
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →