docs(pages): add example of limiting scanned routes (#19181)

This commit is contained in:
Joel Wenzel 2023-02-21 07:39:36 -06:00 committed by GitHub
parent c4eb4b784c
commit b1fd5200f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -423,6 +423,35 @@ export default defineNuxtConfig({
})
```
## Modifying or Removing Scanned Pages
Nuxt will create a route for all files ending with one of the `extensions` configured in `nuxt.config`. (By default these are `.js`, `.jsx`, `.mjs`, `.ts`, `.tsx` and `.vue`.)
You can change or remove pages from the scanned routes with the `pages:extend` nuxt hook. For example, to prevent creating routes for any `.ts` files:
```ts [nuxt.config.ts]
export default defineNuxtConfig({
hooks: {
'pages:extend' (pages) {
function removePagesMatching (pattern: RegExp, pages: NuxtPage[] = []) {
const pagesToRemove = []
for (const page of pages) {
if (pattern.test(page.file)) {
pagesToRemove.push(page)
} else {
removePagesMatching(pattern, page.children)
}
}
for (const page of pagesToRemove) {
pages.splice(pages.indexOf(page), 1)
}
}
removePagesMatching(/\.ts$/, pages)
}
}
})
```
## Programmatic Navigation
Nuxt 3 allows programmatic navigation through the `navigateTo()` utility method. Using this utility method, you will be able to programmatically navigate the user in your app. This is great for taking input from the user and navigating them dynamically throughout your application. In this example, we have a simple method called `navigate()` that gets called when the user submits a search form.