Skip to content

Commit

Permalink
feat: add pathMatch
Browse files Browse the repository at this point in the history
  • Loading branch information
s00d committed Oct 24, 2024
1 parent 3ab807d commit b23aeb2
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 26 deletions.
12 changes: 12 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,18 @@ export default defineNuxtModule<ModuleOptions>({

pageManager.extendPages(pages, nuxt.options.rootDir, options.customRegexMatcher)

if (options.includeDefaultLocaleRoute) {
const fallbackRoute: NuxtPage = {
path: '/:pathMatch(.*)*',
name: 'custom-fallback-route',
file: resolver.resolve('./runtime/components/locale-redirect.vue'),
meta: {
globalLocaleRoutes: options.globalLocaleRoutes,
},
}
pages.push(fallbackRoute)
}

nuxt.options.generate.routes = Array.isArray(nuxt.options.generate.routes) ? nuxt.options.generate.routes : []

const prerenderRoutes: string[] = []
Expand Down
10 changes: 1 addition & 9 deletions src/page-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,7 @@ export class PageManager {
const pagePath = page.path ?? ''
const pageName = page.name ?? ''

const defaultLocalePath = this.localizedPaths[pagePath]?.[this.defaultLocale.code]
if (defaultLocalePath !== undefined) continue

const isLocalized = Object.values(this.localizedPaths).some(
paths => paths[this.defaultLocale.code] === pagePath,
)
if (isLocalized) continue

if (this.globalLocaleRoutes[pageName] !== undefined) continue
if (this.globalLocaleRoutes[pageName] === false) continue

// Удаляем страницы, если они не начинаются с /:locale и не являются корневыми
if (!/^\/:locale/.test(pagePath) && pagePath !== '/') {
Expand Down
42 changes: 42 additions & 0 deletions src/runtime/components/locale-redirect.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<template>
<div />
</template>

<script setup>
import { useRoute, useI18n, createError, navigateTo } from '#imports'
const route = useRoute()
const { $getLocales, $defaultLocale } = useI18n()
const locales = $getLocales().map(locale => locale.code)
const defaultLocale = $defaultLocale() || 'en'
const pathSegments = route.fullPath.split('/')
const firstSegment = pathSegments[1]
const generateRouteName = (segments) => {
return segments
.filter(segment => segment)
.map(segment => segment.replace(/:/g, ''))
.join('-')
}
const currentPageName = generateRouteName(pathSegments)
const globalLocaleRoutes = route.meta.globalLocaleRoutes ?? {}
if (globalLocaleRoutes && globalLocaleRoutes[currentPageName]) {
const localizedRoutes = globalLocaleRoutes[currentPageName]
if (localizedRoutes && localizedRoutes[defaultLocale]) {
const localizedPath = localizedRoutes[defaultLocale]
navigateTo(localizedPath, { redirectCode: 301, external: true })
}
}
else if (!locales.includes(firstSegment)) {
const newPath = `/${defaultLocale}${route.fullPath}`
navigateTo(newPath, { redirectCode: 301, external: true })
}
else {
throw createError({
statusCode: 404,
})
}
</script>
10 changes: 0 additions & 10 deletions test/fixtures/named/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ function navigateBroken() {
navigateTo($localeRoute({ name: 'page', params: { id: 'my-id' } }))
}
function navigateWorking() {
navigateTo({ name: 'page', params: { id: 'my-id' } })
}
function navigateBrokenDefaultNuxtPageNaming() {
navigateTo($localeRoute({ name: 'test-id', params: { id: 'my-id' } }))
}
Expand Down Expand Up @@ -47,12 +43,6 @@ function navigateBrokenDefaultNuxtPageNaming() {
>
Navigate
</button>
<button
id="link5"
@click="navigateWorking"
>
Navigate
</button>

<NuxtLink
id="link6"
Expand Down
7 changes: 0 additions & 7 deletions test/named-params.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,6 @@ test('test navigation links and buttons de', async ({ page, goto }) => {
await page.click('#link4')
await expect(page).toHaveURL('/de/test-my-id') // Assuming this is the expected URL for navigateBrokenDefaultNuxtPageNaming()

// Navigate back to the main page
await goto('/de', { waitUntil: 'hydration' })

// Test Button 5
await page.click('#link5')
await expect(page).toHaveURL('/de/page/my-id') // Assuming this is the expected URL for navigateWorking()

await goto('/de', { waitUntil: 'hydration' })

await page.click('#link6')
Expand Down

0 comments on commit b23aeb2

Please sign in to comment.