![]() |
_app.js is the root component of our NextJS application, used for globally accessible components, layouts, and structures. _document.js is being used to increase the performance and SEO of our website. What is a _app.js?_app.js is a file in Next.js that contains features such as layout, structure, state management, and custom error handling globally, which are essential for building maintainable web applications. Features of creating an _app.js:
What is a _document.js?_document.js is a file in Next.js that contains features such as optimization for SEO, which helps in ranking of your website and control over the HTML document structure. Features of creating an _document.js:
Steps to Create Next JS Application and Installing ModulesStep 1: Install basic dependencies by running the following command in your terminal:- npm install next@latest react@latest react-dom@latest Step 2: Update your package.json by adding the following scripts:- {
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
}
} Step 3: Create a pages folder and add _app.js file:-
Step 4: Create a index.js in pages directory:
Output: ![]() Header and Footer components will be available on every page Step 5: Create a _document.js in pages directory as well:
Output: ![]() In chrome’s inspect we can see our meta tags and script tags. Difference between _app.js and _document.js
ConclusionIn summary, _app.js is being used for the state management or components we want to be available globally on the other hand _document.js is being used for optimization of you website such as SEO which can be achieved by adding meta tags, and by using defer keyword we can also improve the performance of our website. |
Reffered: https://www.geeksforgeeks.org
ReactJS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 18 |