Sling Academy
Home/Next.js/How to Change the Base Path in Next.js (Subdirectory Path)

How to Change the Base Path in Next.js (Subdirectory Path)

Last updated: January 09, 2023

There might be cases where you want to deploy a complex web app that consists of multiple child web apps. Each child web app is developed independently by a team and goes with a subdirectory path like this:

slingacademy.com/blogs/
slingacademy.com/app/
slingacademy.com/shop/
slingacademy.com/forums/

To access a child web app, you will type the root domain + the subdirectory path. In Next.js, you can set the subdirectory path with the basePath option in the next.config.js file (the default value is an empty string):

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  // add this line
  basePath: '/subfolder'
}

module.exports = nextConfig

The basePath will be automatically added to every link in your app. It works the same in both development and production. If you don’t clear on what I mean, let’s see the complete example below.

Example

App Preview

The tiny project we’re going to build uses /sling-academy as the base path. On localhost, the home page is http://localhost:3000/sling-academy.

The Steps

1. Create a brand new Next.js app:

npx create-next-app example

2. Modify the next.config.js file as follows:

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  // add this line
  basePath: '/sling-academy'
}

module.exports = nextConfig

3. Replace the default code in pages/index.js with the following:

// pages/index.js
import Link from 'next/link';
export default function Home(props) {
  return (
    <div style={{ padding: 30 }}>
      <h1>Welcome to Sling Academy</h1>
      <Link
        href='/sample-page'
        style={{ color: 'blue', textDecoration: 'underline' }}
      >
        Go to Sample page
      </Link>
    </div>
  );
}

4. In your pages folder, create a new file called sample-page.js, then add the code below into it:

// pages/sample-page.js
export default function Sample(){
    return <div style={{padding: 30}}>
        <h1>Sample Page</h1>
    </div>
}

5. Boot your app up by running this:

npm run dev

Open a web browser, then go to the following address to check your work:

http://localhost:3000/sling-academy

My screenshot:

Good luck!

Next Article: How to programmatically navigate in Next.js

Series: Next.js Navigation and Routing

Next.js

Related Articles

You May Also Like

  • Solving Next.js Image Component Error with CSS Classes
  • How to Use MUI (Material UI) in Next.js 14
  • Fixing Data Update Issues in Next.js Production
  • Fixing Next.js Undefined ENV Variables in Production Build
  • Solving Next.js SyntaxError: Unexpected token ‘export’
  • Next.js Error: Found Page Without a React Component as Default Export – How to Fix
  • Fixing Next.js Error: Blank Page on Production Build
  • Fixing Next.js Error when Importing SVGs: A Step-by-Step Guide
  • Next.js Issue: useEffect Hook Running Twice in Client
  • Fixing ‘self’ is not defined error in Next.js when importing client-side libraries
  • How to Dockerize a Next.js App for Production
  • How to set up Next.js with Docker Composer and Nginx
  • Solving Next.js CORS Issues: A Comprehensive Guide
  • Fixing Next.js Hydration Mismatch Error in Simple Ways
  • Next.js Error: Cannot use import statement outside a module
  • Next.js: How to Access Files in the Public Folder
  • Fixing Next.js Import CSS Error: A Simple Guide
  • Fixing LocalStorage Not Defined Error in Next.js
  • Fixing Next.js Error: No HTTP Methods Exported