docs: add routeNameSplitter example in migration docs (#25838)

This commit is contained in:
Serge Doda 2024-03-18 08:18:41 -07:00 committed by GitHub
parent 41f6a0a3a0
commit f4173b362b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -57,6 +57,44 @@ Nuxt configuration will be loaded using [`unjs/jiti`](https://github.com/unjs/ji
::
1. If you were using `router.routeNameSplitter` you can achieve same result by updating route name generation logic in the new `pages:extend` hook:
::code-group
```ts [Nuxt 2]
export default {
router: {
routeNameSplitter: '/'
}
}
```ts [Nuxt 3]
import { createResolver } from '@nuxt/kit'
export default defineNuxtConfig({
hooks: {
'pages:extend' (routes) {
const routeNameSplitter = '/'
const root = createResolver(import.meta.url).resolve('./pages')
function updateName(routes) {
if (!routes) return
for (const route of routes) {
const relativePath = route.file.substring(root.length + 1)
route.name = relativePath.replace(/\//g, routeNameSplitter).slice(0, -4)
updateName(route.children)
}
}
updateName(routes)
},
},
})
```
::
#### ESM Syntax
Nuxt 3 is an [ESM native framework](/docs/guide/concepts/esm). Although [`unjs/jiti`](https://github.com/unjs/jiti) provides semi compatibility when loading `nuxt.config` file, avoid any usage of `require` and `module.exports` in this file.