question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

router.extendRoutes support in nuxt.config.js for Nuxt 3

See original GitHub issue

Environment

PS D:\nuxt3-app> npx nuxi info Nuxt CLI v3.0.0-27288761.d3fb512 11:17:49 RootDir: D:\nuxt3-app 11:17:52 Nuxt project info: 11:17:52


  • Operating System: Windows_NT
  • Node Version: v16.12.0
  • Nuxt Version: 3.0.0-27288761.d3fb512
  • Package Manager: npm@8.1.0
  • Bundler: Vite
  • User Config: router
  • Runtime Modules: -
  • Build Modules: -

Reproduction

https://codesandbox.io/s/laughing-tereshkova-3byow?file=/nuxt.config.ts (visit /topic1 in Browser)

or

https://github.com/wzdxy/nuxt3-extend-routes-issue

Describe the bug

I need a router like /topic:id(\\d+). So I config a extendRoutes in nuxt.config.ts

export default defineNuxtConfig({
  router: {
    extendRoutes(routes, resolve) {
      routes.push({
        path: '/product:id(\\d+)',
        name: product1',
        file: join(__dirname, 'pages/product.vue'),
      })
      console.log(routes)
    },
  },
})

But I got a 404 error. And No match found for location with path "/topic1" on terminal. image image

Additional context

https://github.com/nuxt/framework/pull/1757/commits/c8d8cfbbc438fcb0cccb2328b02b20e6469f3c20

I refer to this commit and rewrite my code,it worked,but very bloated.

  buildModules: [
    '~/modules/addRoutes'
  ],

Logs

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:8
  • Comments:18 (6 by maintainers)

github_iconTop GitHub Comments

15reactions
danielroecommented, Dec 20, 2021

This is how you can do it in Nuxt 3 at the moment (with the extend:pages hook):

import { resolve } from 'path'
import { defineNuxtConfig } from 'nuxt3'
import { createCommonJS } from 'mlly'
const { __dirname } = createCommonJS(import.meta.url)

export default defineNuxtConfig({
  hooks: {
    'pages:extend' (pages) {
      pages.push({
        name: 'cmsPage',
        path: '*',
        file: resolve(__dirname, 'cms/index.vue')
      })
    }
  }
})
3reactions
toddpadwickcommented, Nov 17, 2022

Ah Ive figured it out (if anyone else is struggling). The --dirname was causing it to break and it seems its not needed. So the following worked:

import { resolve } from 'path'; 

export default defineNuxtConfig({
	hooks: {
        'pages:extend'(pages) {
            pages.push(
	            {
	                name: 'site.gender',
	                path: '/products/:gender',
	                file: resolve('/pages/products/index.vue'),
	            },
	            {
	                name: 'site.collection',
	                path: '/products/collections/:collection/:gender?',
	                file: resolve('/pages/products/index.vue'),
	            }
            );
        },
    },
});

Read more comments on GitHub >

github_iconTop Results From Across the Web

The router Property - Nuxt
You may want to extend the routes created by Nuxt. You can do so via the extendRoutes option. ... The schema of the...
Read more >
Migrate to Nuxt 3: Configuration
Build your next Vue.js application with confidence using Nuxt. ... Nuxt 3. Nuxt 2 export default { router: { extendRoutes (routes) { //...
Read more >
Custom Routes in NuxtJS - Stack Overflow
To customize the route path, simply, you can use extendRoutes function in the nuxt.config.js and edit the path property like this:
Read more >
Routing - NuxtJS
Nuxt.js automatically generates the vue-router configuration based on your file tree of Vue files inside the pages directory. To navigate between pages ...
Read more >
API: The router Property - Nuxt.js
You may want to extend the routes created by Nuxt.js. You can do so via the extendRoutes option. Example of adding a custom...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found