Skip to content

Commit

Permalink
Merge pull request #478 from sreetamdas/omit-not-found-default-locale
Browse files Browse the repository at this point in the history
fix: check if URL with replaced defaultLocale in notFoundRoutes
  • Loading branch information
iamvishnusankar committed Sep 10, 2022
2 parents a8ccb37 + 1441124 commit 3338f60
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
7 changes: 7 additions & 0 deletions packages/next-sitemap/src/__fixtures__/manifest.ts
Expand Up @@ -72,6 +72,7 @@ export const sampleNotFoundRoutesBuildManifest: IBuildManifest = {
pages: {
'/': [],
'/about': [],
'/only-nl': [],
'/[dynamic]': [],
'/_app': [],
'/_error': [],
Expand All @@ -87,6 +88,10 @@ export const sampleNotFoundRoutesPreRenderManifest: IPreRenderManifest = {
'/fr/about': {},
'/nl-NL/about': {},

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

'/en-US/page-0': {},
'/fr/page-0': {},
'/nl-NL/page-0': {},
Expand All @@ -98,6 +103,8 @@ export const sampleNotFoundRoutesPreRenderManifest: IPreRenderManifest = {
notFoundRoutes: [
'/fr',
'/nl-NL/about',
'/en-US/only-nl',
'/fr/only-nl',
'/nl-NL/page-0',
'/fr/page-1',
'/nl-NL/page-1',
Expand Down
Expand Up @@ -633,6 +633,15 @@ describe('UrlSetBuilder', () => {
alternateRefs: [],
trailingSlash: false,
},
// only localized page
{
changefreq: 'daily',
lastmod: expect.any(String),
priority: 0.7,
loc: 'https://example.com/nl-NL/only-nl',
alternateRefs: [],
trailingSlash: false,
},
// page-0
{
changefreq: 'daily',
Expand Down
10 changes: 8 additions & 2 deletions packages/next-sitemap/src/builders/url-set-builder.ts
Expand Up @@ -79,8 +79,9 @@ export class UrlSetBuilder {
let urlSet = allKeys.filter((x) => !isNextInternalUrl(x))

// Remove default locale if i18n is enabled
let defaultLocale
if (i18n) {
const { defaultLocale } = i18n
defaultLocale = i18n.defaultLocale
const replaceDefaultLocale = createDefaultLocaleReplace(defaultLocale)
urlSet = urlSet.map(replaceDefaultLocale)
}
Expand All @@ -95,7 +96,12 @@ export class UrlSetBuilder {
// Remove routes which don't exist
const notFoundRoutes = (this.manifest?.preRender?.notFoundRoutes ??
[]) as string[]
urlSet = urlSet.filter((url) => !notFoundRoutes.includes(url))
urlSet = urlSet.filter((url) => {
return (
!notFoundRoutes.includes(url) &&
!notFoundRoutes.includes(`/${defaultLocale}${url}`)
)
})

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

0 comments on commit 3338f60

Please sign in to comment.