Skip to content

Commit

Permalink
Merge pull request #376 from sreetamdas/ignore-i18n-404s
Browse files Browse the repository at this point in the history
Skip 404-ed localized URLs in sitemap urlSet
  • Loading branch information
iamvishnusankar committed May 28, 2022
2 parents 34cb4e4 + 2e6abf3 commit 5cc88ad
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 1 deletion.
51 changes: 51 additions & 0 deletions packages/next-sitemap/src/fixtures/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const samplePreRenderManifest: IPreRenderManifest = {
'/page-2': {},
'/page-3': {},
},
notFoundRoutes: [],
}

export const sampleManifest: INextManifest = {
Expand Down Expand Up @@ -51,6 +52,7 @@ export const sampleI18nPreRenderManifest: IPreRenderManifest = {
'/fr/page-2': {},
'/page-3': {},
},
notFoundRoutes: [],
}

export const sampleRenderManifest: IRoutesManifest = {
Expand All @@ -65,3 +67,52 @@ export const sampleI18nManifest: INextManifest = {
preRender: sampleI18nPreRenderManifest,
routes: sampleRenderManifest,
}

export const sampleNotFoundRoutesBuildManifest: IBuildManifest = {
pages: {
'/': [],
'/about': [],
'/[dynamic]': [],
'/_app': [],
'/_error': [],
},
}
export const sampleNotFoundRoutesPreRenderManifest: IPreRenderManifest = {
routes: {
'/en-US': {},
'/fr': {},
'/nl-NL': {},

'/en-US/about': {},
'/fr/about': {},
'/nl-NL/about': {},

'/en-US/page-0': {},
'/fr/page-0': {},
'/nl-NL/page-0': {},

'/en-US/page-1': {},
'/fr/page-1': {},
'/nl-NL/page-1': {},
},
notFoundRoutes: [
'/fr',
'/nl-NL/about',
'/nl-NL/page-0',
'/fr/page-1',
'/nl-NL/page-1',
],
}

export const sampleNotFoundRoutesRenderManifest: IRoutesManifest = {
i18n: {
locales: ['en-US', 'fr', 'nl-NL'],
defaultLocale: 'en-US',
},
}

export const sampleNotFoundRoutesManifest: INextManifest = {
build: sampleNotFoundRoutesBuildManifest,
preRender: sampleNotFoundRoutesPreRenderManifest,
routes: sampleNotFoundRoutesRenderManifest,
}
1 change: 1 addition & 0 deletions packages/next-sitemap/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export interface IPreRenderManifest {
routes: {
[key: string]: any
}
notFoundRoutes: string[]
}

export interface IRoutesManifest {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { createUrlSet } from '..'
import { transformSitemap } from '../../../config'
import { sampleConfig } from '../../../fixtures/config'
import { sampleManifest, sampleI18nManifest } from '../../../fixtures/manifest'
import {
sampleManifest,
sampleI18nManifest,
sampleNotFoundRoutesManifest,
} from '../../../fixtures/manifest'
import { IConfig, ISitemapField } from '../../../interface'

describe('createUrlSet', () => {
Expand Down Expand Up @@ -582,4 +586,75 @@ describe('createUrlSet', () => {
}),
])
})

test('with i18n, without notFound routes', async () => {
const urlset = await createUrlSet(
{
...sampleConfig,
},
sampleNotFoundRoutesManifest
)

expect(urlset).toStrictEqual([
{
changefreq: 'daily',
lastmod: expect.any(String),
priority: 0.7,
loc: 'https://example.com',
alternateRefs: [],
trailingSlash: false,
},
{
changefreq: 'daily',
lastmod: expect.any(String),
priority: 0.7,
loc: 'https://example.com/about',
alternateRefs: [],
trailingSlash: false,
},
// /about
{
changefreq: 'daily',
lastmod: expect.any(String),
priority: 0.7,
loc: 'https://example.com/nl-NL',
alternateRefs: [],
trailingSlash: false,
},
{
changefreq: 'daily',
lastmod: expect.any(String),
priority: 0.7,
loc: 'https://example.com/fr/about',
alternateRefs: [],
trailingSlash: false,
},
// page-0
{
changefreq: 'daily',
lastmod: expect.any(String),
priority: 0.7,
loc: 'https://example.com/page-0',
alternateRefs: [],
trailingSlash: false,
},
{
changefreq: 'daily',
lastmod: expect.any(String),
priority: 0.7,
loc: 'https://example.com/fr/page-0',
alternateRefs: [],
trailingSlash: false,
},
// page-1
{
changefreq: 'daily',
lastmod: expect.any(String),
priority: 0.7,
loc: 'https://example.com/page-1',
alternateRefs: [],
trailingSlash: false,
},
])
})
})
4 changes: 4 additions & 0 deletions packages/next-sitemap/src/url/create-url-set/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ export const createUrlSet = async (

urlSet = [...new Set(urlSet)]

// Remove routes which don't exist
const notFoundRoutes = (manifest.preRender?.notFoundRoutes ?? []) as string[]
urlSet = urlSet.filter((url) => !notFoundRoutes.includes(url))

// Create sitemap fields based on transformation
const sitemapFields: ISitemapField[] = [] // transform using relative urls

Expand Down

0 comments on commit 5cc88ad

Please sign in to comment.