Skip to content

Commit

Permalink
[Fix] Index sitemap does not reference the generated sitemaps
Browse files Browse the repository at this point in the history
Fix: #301
  • Loading branch information
iamvishnusankar committed Feb 16, 2022
1 parent ae7d182 commit 4e09807
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 36 deletions.
30 changes: 12 additions & 18 deletions packages/next-sitemap/src/cli.ts
Expand Up @@ -10,8 +10,8 @@ import {
getConfigFilePath,
} from './path'
import { exportRobotsTxt } from './robots-txt'
import { merge } from '@corex/deepmerge'
import { exportSitemapIndex } from './sitemap-index/export'
import { INextSitemapResult } from '.'
import { Logger } from './logger'

// Async main
Expand Down Expand Up @@ -48,7 +48,7 @@ const main = async () => {
// All sitemaps array to keep track of generated sitemap files.
// Later to be added on robots.txt
// Add default index file as first entry of sitemap
const allSitemaps: string[] = [runtimePaths.SITEMAP_INDEX_URL]
const generatedSitemaps: string[] = []

// Generate sitemaps from chunks
await Promise.all(
Expand All @@ -57,34 +57,28 @@ const main = async () => {
const sitemapUrl = generateUrl(config.siteUrl, `/${chunk.filename}`)

// Add generate sitemap to sitemap list
if (config?.robotsTxtOptions?.includeNonIndexSitemaps) {
allSitemaps.push(sitemapUrl)
}
generatedSitemaps.push(sitemapUrl)

// Generate sitemap
return generateSitemap(chunk)
})
)

// combine-merge allSitemaps with user-provided additionalSitemaps
config = merge([
{
robotsTxtOptions: {
additionalSitemaps: allSitemaps,
},
},
config,
])
// Create result object
const result: INextSitemapResult = {
runtimePaths,
generatedSitemaps,
}

// Export sitemap index file
await exportSitemapIndex(runtimePaths, config)
await exportSitemapIndex(result)

// Generate robots.txt
if (config.generateRobotsTxt) {
await exportRobotsTxt(runtimePaths, config)
if (config?.generateRobotsTxt) {
await exportRobotsTxt(config, result)
}

return allSitemaps
return result
}

// Execute
Expand Down
10 changes: 10 additions & 0 deletions packages/next-sitemap/src/interface.ts
Expand Up @@ -49,6 +49,11 @@ export interface IRobotsTxt {
*/
additionalSitemaps?: string[]

/**
* Additional sitemap-indices which need to be added to robots.txt
*/
additionalSitemapIndices?: string[]

/**
* From v2.4x onwards, generated `robots.txt` will only contain url of `index sitemap` and custom provided endpoints from `robotsTxtOptions.additionalSitemaps`
*
Expand Down Expand Up @@ -218,3 +223,8 @@ export type ISitemapField = {
priority?: number
alternateRefs?: Array<AlternateRef>
}

export interface INextSitemapResult {
generatedSitemaps: string[]
runtimePaths: IRuntimePaths
}
11 changes: 8 additions & 3 deletions packages/next-sitemap/src/logger/index.ts
@@ -1,3 +1,5 @@
import { INextSitemapResult } from '..'

/**
* Generic console logger
*/
Expand Down Expand Up @@ -43,15 +45,18 @@ export class Logger {
* @param allSitemaps
* @returns
*/
static generationCompleted(allSitemaps: string[]) {
static generationCompleted(result: INextSitemapResult) {
// Initial stats
Logger.log(
`✅`,
`Generated index sitemap and ${allSitemaps?.length - 1} sitemap(s)`
`Generated index sitemap and ${result?.generatedSitemaps?.length} sitemap(s)`
)

// Temp assign
let sitemapsList = allSitemaps
let sitemapsList = [
result?.runtimePaths?.SITEMAP_INDEX_URL,
...(result?.generatedSitemaps ?? []),
]

// Only show 5 entries on console
if (sitemapsList?.length > 7) {
Expand Down
34 changes: 29 additions & 5 deletions packages/next-sitemap/src/robots-txt/export/index.ts
@@ -1,21 +1,45 @@
import { IConfig, IRuntimePaths } from '../../interface'
import { INextSitemapResult } from '../../interface'
import { generateRobotsTxt } from '../generate'
import { exportFile } from '../../file'
import { IConfig } from '../..'
import { merge } from '@corex/deepmerge'

export const getRobotsTxtExportConfig = (
config: IConfig,
result: INextSitemapResult
) => {
return merge([
{
robotsTxtOptions: {
additionalSitemaps: [
result?.runtimePaths?.SITEMAP_INDEX_URL, // URL of index sitemap
...(config?.robotsTxtOptions?.includeNonIndexSitemaps // Optionally include static generated sitemap files
? result?.generatedSitemaps ?? []
: []),
],
},
},
config,
])
}

/**
* Export robots txt file
* @param runtimePaths
* @param config
*/
export const exportRobotsTxt = async (
runtimePaths: IRuntimePaths,
config: IConfig
config: IConfig,
result: INextSitemapResult
): Promise<any> => {
// Create a config specific for robots.txt
const exportConfig = getRobotsTxtExportConfig(config, result)

// Generate robots text
const robotsTxt = generateRobotsTxt(config)
const robotsTxt = generateRobotsTxt(exportConfig)

// Create file
if (robotsTxt) {
await exportFile(runtimePaths.ROBOTS_TXT_FILE, robotsTxt)
await exportFile(result?.runtimePaths.ROBOTS_TXT_FILE, robotsTxt)
}
}
15 changes: 5 additions & 10 deletions packages/next-sitemap/src/sitemap-index/export.ts
@@ -1,4 +1,5 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { INextSitemapResult } from '..'
import { exportFile } from '../file'
import type { IRuntimePaths, IConfig } from '../interface'
import { buildSitemapIndexXML } from './build'
Expand All @@ -9,16 +10,10 @@ import { buildSitemapIndexXML } from './build'
* @param config
* @returns
*/
export const exportSitemapIndex = async (
runtimePaths: IRuntimePaths,
config: IConfig
) => {
// Remove first entry from additionalSitemaps (Index sitemap)
const [indexEntry, ...restSitemaps] =
config?.robotsTxtOptions?.additionalSitemaps ?? []

export const exportSitemapIndex = async (result: INextSitemapResult) => {
// Generate sitemap index content
const content = buildSitemapIndexXML(restSitemaps)
const content = buildSitemapIndexXML(result?.generatedSitemaps ?? [])

return exportFile(runtimePaths.SITEMAP_INDEX_FILE, content)
// Export file
return exportFile(result?.runtimePaths.SITEMAP_INDEX_FILE, content)
}

0 comments on commit 4e09807

Please sign in to comment.