Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support redirect option in custom route block #107

Open
zhiyuanzmj opened this issue Dec 22, 2022 · 9 comments
Open

Support redirect option in custom route block #107

zhiyuanzmj opened this issue Dec 22, 2022 · 9 comments
Labels
⚡️ enhancement improvement over an existing feature

Comments

@zhiyuanzmj
Copy link
Contributor

<template>
  <div />
</template>

<route lang="yaml">
redirect: /system/settings
meta:
  icon: i-iconoir:settings
  permission: true
</route>
@posva
Copy link
Owner

posva commented Dec 22, 2022

Right now, redirect records can be added through extendRoutes.
I’m not sure if there should be another way to add them but one shouldn’t write an empty component just for a redirect 🤔

maybe we can find other ways

@hi-reeve
Copy link
Contributor

Right now, redirect records can be added through extendRoutes. I’m not sure if there should be another way to add them but one shouldn’t write an empty component just for a redirect 🤔

maybe we can find other ways

stumble upon this exactly today, ended up creating empty page to redirecting to the intended route. probably adding it to the docs is a good choice for now

@zhiyuanzmj
Copy link
Contributor Author

Now I'm defining the redirect with definePage macro instead of the custom route block, It works well ✌️.

@posva posva added the enhancement label Dec 22, 2022 — with Volta.net
Copy link
Owner

posva commented Jan 4, 2023

I still think redirects should be defined without the need of creating an empty component. I'm thinking of different possibilities:

  • Adding a defineRedirect() macro that can be defined within pages but this has two issues: it's yet, another macro, and it also doesn't necessarily relate to the page component where it is used as the function syntax (which is often necessary when params are involved) stil allows redirecting anywhere:
    defineRedirect('/from-other-page') // redirects to this page
    defineRedirect(['/multiple', '/redirects'])
    defineRedirect(to => ({ name: 'this could be another route' }))
    defineRedirect(to => '/this could also be another route')
  • Allowing a redirects option with createRouter() but it adds to the bundle
  • Allowing defining redirects within a special file redirects.ts (or js) that exports an array or an object and is read at built time
    // src/redirects.ts or src/pages/_redirects.ts
    export default {
      '/': '/static-redirect', // simplest
      'named-route': '/target-redirect',
      '/other': { name: 'static object redirect' },
      '/with/:param': to => ({ name: 'user', params: { id: to.params.param }})
    }

To me, the last one makes the most sense, it's also easy to create the file by default with an empty object and some comments to point out how to use it

@amirtbi
Copy link

amirtbi commented Jul 2, 2023

Right now, redirect records can be added through extendRoutes. I’m not sure if there should be another way to add them but one shouldn’t write an empty component just for a redirect 🤔

maybe we can find other ways

How can define redirect in extendRoutes?

@amirtbi
Copy link

amirtbi commented Jul 2, 2023

Could you explain it how you did?

@liujinle
Copy link

Could you explain it how you did?你能解释一下你是怎么做到的吗?

you can use the definePage() macro , it work better

@Soviut
Copy link
Sponsor

Soviut commented Aug 1, 2023

I think the last option is a good one. It might be worth calling the file simply routes.ts so that other router-related things can be defined in it for that directory and sub-directories, For example, it would be a good place to put meta data for all sub-routes.

I seem to recall this mentioned elsewhere in one of the discussions here, so perhaps the two can be combined?

@aviorp
Copy link

aviorp commented Feb 3, 2024

  • defineRedirect

can you show a simple example for how we can do it ?

@settings settings bot removed the enhancement label Feb 21, 2024
@posva posva added the ⚡️ enhancement improvement over an existing feature label Feb 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⚡️ enhancement improvement over an existing feature
Projects
None yet
Development

No branches or pull requests

7 participants