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.

nested router - doesnt work

See original GitHub issue

Environment


  • Operating System: Linux
  • Node Version: v16.14.2
  • Nuxt Version: 3.0.0-27460146.2ad93eb
  • Package Manager: npm@7.17.0
  • Builder: vite
  • User Config: -
  • Runtime Modules: -
  • Build Modules: -

Reproduction

https://stackblitz.com/edit/github-zvowq4-gj7iuh?file=pages/index.vue

Describe the bug

with latest version of nuxt 3 and follow the documentation i cant create nested route when i’m trying to get data from api i get error:

handle is not a function
  at eval (file://./node_modules/h3/dist/index.mjs:57:24)  
  at new Promise (<anonymous>)  
  at callHandle (file://./node_modules/h3/dist/index.mjs:54:10)  
  at eval (file://./node_modules/h3/dist/index.mjs:50:12)  
  at eval (file://./node_modules/h3/dist/index.mjs:78:34)  
  at async handle (file://./node_modules/h3/dist/index.mjs:318:19)  
  at async ufetch (file://./node_modules/unenv/runtime/fetch/index.mjs:22:17)  
  at async $fetchRaw2 (file://./node_modules/ohmyfetch/dist/chunks/fetch.mjs:144:20)  
  at async setup (file://./.nuxt/dist/server/server.mjs:2442:176)
[Vue Router warn]: No match found for location with path "/test"

Additional context

No response

Logs

No response

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:6
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
danielroecommented, Apr 11, 2022

For now, you can workaround this with:

import { createRouter, useBase } from 'h3';

const router = createRouter();

router.get('/', () => 'Hello World');

export default useBase('/api/test', router.handler);
5reactions
Aareksiocommented, Apr 19, 2022

@ahku As of right now you can not. The handler is working only for the file-system path no matter if you use useBase or not. Currently defining programmatic routes is not possible this way. The only mean I am aware of to programmatically define routes is by using server middleware and handling req / res yourself.

// @/server/middleware/api.ts -- arbitrary file name inside /server/middleware

import { createRouter, defineEventHandler, useBase } from 'h3';

const router = createRouter();
router.get('/hello', defineEventHandler(event => 'Hello'));
router.get('/hello/world', defineEventHandler(event => 'Hello World'));
const handler = useBase('/api', router.handler);

export default (req, res, next) => {
  if (!req.originalUrl.startsWith('/api')) {
    return next();
  }

  return handler(req, res);
}

Edit, scratch that. @danielroe code is perfectly fine, but you must use catch-all:

// @/server/api/[...].ts -- catch-all route

import { createRouter, defineEventHandler, useBase } from 'h3';

const router = createRouter();
router.get('/hello', defineEventHandler(event => 'Hello'));
router.get('/hello/world', defineEventHandler(event => 'Hello World'));

export default useBase('/api', router.handler);

Edit 2, you can not use root (/) path in nested router when using catch-all ([...].ts) 😅

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Router v6 Nested Routes doesn't work - Stack Overflow
Nested Route components need an Outlet component to be rendered into. Apply either of the following: Have Information component render the ...
Read more >
The Guide to Nested Routes with React Router - ui.dev
In this comprehensive, up-to-date guide, you'll learn everything you need to know about creating nested routes with React Router.
Read more >
React Router and nested routes - Kevin Farrugia
In my own words, a nested route is a region within a page layout that responds to route changes. For example, in a...
Read more >
How to Use Nested Routes in React Router 6
It is standard practice to import BrowserRouter as Router because BrowserRouter is too long to type! Basic Routing. From here you can begin...
Read more >
react router v6 nested routes not working - You.com
Nested routing is not working in React Router v6 ... 4 Answers. The default behavior of React Router fails to render multiple child...
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