Загрузка...

Mastering Next.js 13: Creating Separate Layouts for User and Admin Pages

Discover how to effectively manage layouts in Next.js 13 using the experimental App Directory feature for both user and admin pages.
---
This video is based on the question https://stackoverflow.com/q/75669998/ asked by the user 'M Muqiit Faturrahman' ( https://stackoverflow.com/u/12614834/ ) and on the answer https://stackoverflow.com/a/76270814/ provided by the user 'Adam' ( https://stackoverflow.com/u/7770512/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.

Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Next.js 13 App Dir - Layouting user and admin page

Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/licensing
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license.

If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Mastering Next.js 13: Creating Separate Layouts for User and Admin Pages

If you're diving into the world of Next.js, you may have encountered challenges when trying to create distinct layouts for different sections of your website—particularly for user and admin interfaces. In this post, we'll address a common problem faced by many developers: how to set up a separate layout for admin pages while still maintaining a landing page and a default layout for the rest of your website using Next.js 13.

Understanding the Problem

Many applications require different layouts for distinct areas of the site. Here, we have a scenario where:

The / route serves as the landing page.

User routes (like projects and about pages) should share a common layout.

Admin routes need a unique layout that differs from the root app directory.

Directory Structure

Let's take a look at the current folder structure:

[[See Video to Reveal this Text or Code Snippet]]

Solution: Setting Up Layouts for Admin Pages

To create a separate layout specifically for the admin section, follow these steps:

Step 1: Create the Admin Layout File

Add a new layout.tsx file inside the (admin) directory.

This new file will house the structure and styles specific to your admin pages.

Here’s how the updated folder structure should look:

[[See Video to Reveal this Text or Code Snippet]]

Step 2: Populate the layout.tsx File for Admin

In the new layout.tsx file within the (admin) directory, include your necessary layout structure:

[[See Video to Reveal this Text or Code Snippet]]

Step 3: Usage Across Your Admin Pages

With your AdminLayout component set up, you can now utilize it across your admin pages (e.g., page.tsx, loading.tsx, etc.) by wrapping the page content with this layout.

Example usage in an admin page:

[[See Video to Reveal this Text or Code Snippet]]

Conclusion

By creating a separate layout.tsx file for your admin pages in the (admin) directory, you effectively isolate its layout from the main application layout. This allows you to maintain distinct structures tailored to the needs of different user roles within your application.

Now, you can make your admin interface visually different and functionally unique while keeping your main app layout intact for user-facing features. Happy coding with Next.js 13!

Видео Mastering Next.js 13: Creating Separate Layouts for User and Admin Pages канала vlogize
Страницу в закладки Мои закладки
Все заметки Новая заметка Страницу в заметки

На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.

Об использовании CookiesПринять