Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check if URL with replaced defaultLocale in notFoundRoutes #478

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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