Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

285
Views
Dynamic route with static part possible in vite-plugin-pages?

I'm building a Vue.js 3 app with Vite and vite-plugin-pages for the routing. https://github.com/hannoeru/vite-plugin-pages

Is it possible to have a dynamic route with a static part to it?

e.g. /@[handle].vue

When I do this, it doesn't work and instead falls back to my catch-all route [...all].vue

I don't want to put it in a sub-folder, so /@/[handle].vue is not an option. I'd like the @ symbol followed by whatever comes after being a dynamic username.

I managed to do this in another application using Vue router but not sure how to achieve the same thing in folder-based routing via this plugin.

How I did this before:

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'Home',
      component: Home,
      children: [
        {
          path: '/@:handle',
          name: 'Profile',
          component: () => import('../views/Profile.vue'),
          props: true,
        },
      ],
    },
  ]
})
about 4 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

You could use the plugin's onRoutesGenerated hook to transform the routes before they're passed to Vue Router.

The solution requires naming the dynamic route parameters with the @ prefix, so the filename in your case would be [@handle].vue:

pages/user/[@handle].vue

The following Vite config renames route parameters that start with @, so that the @ is moved into the path right before the parameter:

// vite.config.js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import Pages from 'vite-plugin-pages'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    Pages({
      onRoutesGenerated(routes) {
        const transformRoute = route => ({
          ...route,
          path: route.path.replace('/:@', '/@:'),
          children: route.children?.map(transformRoute) ?? [],
        })

        return routes.map(transformRoute)
      },
    }),
  ],
})

So the route path for the example above is transformed from this:

/user/:@handle.vue

...to this:

/user/@:handle.vue
      ๐Ÿ‘†

demo

about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!