Skip to content

Commit

Permalink
fix: redirect without name
Browse files Browse the repository at this point in the history
  • Loading branch information
s00d committed Oct 22, 2024
1 parent 4696a59 commit 4ed7428
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
15 changes: 13 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions src/page-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import {
extractLocaleRoutes,
} from './utils'

const buildRouteNameFromRoute = (name: string | null | undefined, path: string | null | undefined) => {
return name ?? (path ?? '').replace(/[^a-z0-9]/gi, '-').replace(/^-+|-+$/g, '')
}

// Класс PageManager
export class PageManager {
locales: Locale[]
Expand Down Expand Up @@ -48,7 +52,6 @@ export class PageManager {
pages.forEach((page) => {
if (!page.name) {
console.warn(`[nuxt-i18n-next] Page name is missing for the file: ${page.file}`)
return
}
const customRoute = this.globalLocaleRoutes[page.name ?? ''] ?? null

Expand Down Expand Up @@ -79,7 +82,7 @@ export class PageManager {
const localizedPaths: { [key: string]: { [locale: string]: string } } = {}

pages.forEach((page) => {
const pageName = page.name ?? ''
const pageName = buildRouteNameFromRoute(page.name, page.path)
const globalLocalePath = this.globalLocaleRoutes[pageName]

if (!globalLocalePath) {
Expand Down Expand Up @@ -165,7 +168,7 @@ export class PageManager {
const currentChildren = page.children ? [...page.children] : []

if (originalChildren.length) {
const newName = normalizePath(path.join('/', page.name ?? ''))
const newName = normalizePath(path.join('/', buildRouteNameFromRoute(page.name, page.path)))
const localizedChildren = this.mergeChildren(originalChildren, newName, [this.defaultLocale.code])

// Мапа для поиска детей по имени
Expand Down Expand Up @@ -254,7 +257,7 @@ export class PageManager {
customRegex?: string | RegExp,
): NuxtPage {
const routePath = this.buildRoutePath(localeCodes, page.path, customPath, isCustom, customRegex)
const routeName = buildRouteName(page.name ?? '', localeCodes[0], isCustom)
const routeName = buildRouteName(buildRouteNameFromRoute(page.name, page.path), localeCodes[0], isCustom)

return {
...page,
Expand All @@ -274,7 +277,7 @@ export class PageManager {
addLocalePrefix: boolean,
): NuxtPage {
const finalPath = this.buildLocalizedRoutePath(routePath, locale, customLocalePaths, addLocalePrefix)
const routeName = this.buildLocalizedRouteName(route.name ?? '', locale, modifyName)
const routeName = this.buildLocalizedRouteName(buildRouteNameFromRoute(route.name, route.path), locale, modifyName)

return {
...route,
Expand Down
14 changes: 14 additions & 0 deletions src/runtime/plugins/01.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,20 @@ function getLocalizedRoute(

return params
}

if (!routeName || routeName === '') {
const resolved = router.resolve(to)
let url = resolved.path.replace(new RegExp(`^/${currentLocale}/`), '/')
if (currentLocale !== i18nConfig.defaultLocale || i18nConfig.includeDefaultLocaleRoute) {
url = '/' + currentLocale + '' + url
}
return router.resolve({
path: url,
query: selectRoute.query,
hash: selectRoute.hash,
})
}

// Check if the localized route exists
if (router.hasRoute(`localized-${routeName}-${currentLocale}`)) {
const newParams = resolveParams(selectRoute)
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/redirect/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default defineNuxtConfig({
],

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
// @ts-ignore
i18n: {
locales: [
{ code: 'en', iso: 'en_EN' },
Expand Down

0 comments on commit 4ed7428

Please sign in to comment.