乐闻世界logo
搜索文章和话题

How to set default page in next js?

1个答案

1

Setting the default page in Next.js primarily involves specifying which page serves as the entry point for the application. Typically, this is achieved through the file structure within the pages directory. The pages directory is the core of a Next.js project, where file paths directly map to the application's routes.

Steps to Set the Default Page:

  1. Create or Modify the Homepage File: In Next.js, the default page is typically the index.js file within the pages directory. This file corresponds to the root URL (i.e., /). Therefore, to set the default page, you simply ensure that pages/index.js is configured as needed.

  2. Write Homepage Content: Within the index.js file, you can define the page content using React components. For example, if you want a simple welcome page, you can write:

    jsx
    function HomePage() { return <div>Welcome to My Next.js App!</div>; } export default HomePage; ``
  3. Configure Routing (if needed): If you wish to change the default routing behavior through configuration (though this is usually unnecessary), you can use the next.config.js file for advanced settings, such as path rewriting.

Example: Setting a Default Page with Layout

Here is an example of a default homepage with a header navigation and footer:

jsx
import Header from '../components/Header'; import Footer from '../components/Footer'; function HomePage() { return ( <div> <Header /> <main> <h1>Welcome to My Next.js App!</h1> <p>This is the home page of the app.</p> </main> <Footer /> </div> ); } export default HomePage;

In this example, Header and Footer are assumed to be pre-created components used for displaying the page header and footer. This ensures that regardless of which page the user accesses first, they will always see the configured default page.

By doing this, Next.js makes routing and page management intuitive and easy to maintain, while also supporting more complex application requirements such as dynamic routing.

2024年6月29日 12:07 回复

你的答案