1.0.5 • Published 1 year ago

nextjs-app-sitemap-generator v1.0.5

Weekly downloads
-
License
MIT
Repository
gitlab
Last release
1 year ago

nextjs-app-sitemap-generator

Table of Contents

Introduction

nextjs-app-sitemap-generator is a utility package designed to simplify the generation of sitemap.xml and robots.txt files for Next.js projects. It helps in managing SEO by creating comprehensive sitemaps and correctly configured robots.txt files based on the provided options.

Installation

To install this package, run:

npm install nextjs-app-sitemap-generator

or

yarn add nextjs-app-sitemap-generator

After installing the package, a generate-sitemap.mjs file will be created in the root of your project. If the file is not created automatically, you should manually create it.

Usage

Initializing the Sitemap

To initialize the sitemap and robots.txt generation, import the initializeSitemap function and provide a configuration object.

import { initializeSitemap } from 'nextjs-app-sitemap-generator';

initializeSitemap({
    baseUrl: process.env.SITE_URL || 'https://example.com',
    robotsTxtOptions: {
        policies: [
            { userAgent: '*', allow: '/' },
            { userAgent: '*', disallow: '/api' },
            { userAgent: '*', disallow: '/admin' },
            { userAgent: '*', disallow: '/admin-login' },
        ]
    },
    sitemapOptions: {
        unauthenticated: false,
        authenticated: true,
        ignore: ["ignore-path1", "ignore-path2"],
        otherPages: [
            "other-path1",
            "other-path2"
        ],
    }
});

This example demonstrates how to set up the sitemap generation in a Next.js project. The baseUrl can be set via an environment variable or a hardcoded string. The robotsTxtOptions and sitemapOptions are used to configure the generated robots.txt and sitemap.xml files, respectively.

Configuration Options

Sitemap Options

OptionTypeDescription
unauthenticatedBooleanGenerate sitemap for unauthenticated pages if true.
authenticatedBooleanGenerate sitemap for authenticated pages if true.
ignoreArrayList of paths to ignore in the sitemap generation.
otherPagesArrayList of other dynamic pages to include in the sitemap.

robots.txt Options

OptionTypeDescription
policiesArrayList of policy objects defining rules for different user agents.
userAgentStringThe user agent to apply the policy to.
allowString/ArrayPaths to allow for the user agent.
disallowString/ArrayPaths to disallow for the user agent.

Folder Structure

The package expects the following folder structure in your Next.js project:

src
└── app
    ├── (unauthenticated)
    │   ├── test
    │   │   └── page.tsx
    │   └── example
    │       └── page.tsx
    ├── (authenticated)
    │   ├── dashboard
    │   │   └── page.tsx
    │   └── settings
    │       └── page.tsx
    ├── _document.js
    ├── _app.js
    └── ...
  • The (unauthenticated) folder contains pages that do not require authentication.
  • The (authenticated) folder contains pages that require authentication.
  • Other pages and components can be placed in the root of the app directory or other subdirectories.

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitLab.

License

This project is licensed under the MIT License. See the LICENSE file for details.

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago