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

Added named routes module #482

Closed
wants to merge 1 commit into from
Closed

Added named routes module #482

wants to merge 1 commit into from

Conversation

tiborpino
Copy link

Added named routes module

@codesandbox
Copy link

codesandbox bot commented Oct 27, 2022

CodeSandbox logoCodeSandbox logo  Open in CodeSandbox Web Editor | VS Code | VS Code Insiders

@tiborpino tiborpino changed the title Main Added named routes module Oct 27, 2022
Copy link
Member

atinux commented Oct 28, 2022

Hi @tiborpino

I am not sure to fully understand the purpose of the module, could you give me an example of using it vs creating in pages/ directory?

@tiborpino
Copy link
Author

tiborpino commented Oct 29, 2022

Hi @atinux The main purpose is you can set custom key for every page inside pages directory and also custom url path for every route. Here is the example

I set named route key custom-key and custom url /custom-url-for-homepage for /pages/homepage.vue

export default defineNuxtConfig({
  modules: [
    'nuxtjs-named-routes'
  ],
  namedRoutes: {
    routes: {
        'custom-key': {
            path: '/custom-url-for-homepage',
            file: '/pages/homepage.vue'
        }
    }
  }
}

Now I can use it as follows

<NuxtLink :to="{name: 'custom-key'}">Link to the homepage</NuxtLink>

And the generated HTML link will look like

<a href="/custom-url-for-homepage">Link to the homepage</a>

And now if I want to change file name to something like /pages/index.vue or just url /new-url-for-homepage all I can do is to change nuxt.config namedRoutes object and all of my <NuxtLink> directives for homepage will be the same and I don't have to change anything in my code base.

Copy link
Member

atinux commented Nov 2, 2022

Indeed, I thought definePageMeta in the Vue page could actually do the trick, but it seems not: https://stackblitz.com/edit/github-n8wcq6?file=pages%2Fcustom-url-example.vue,pages%2Findex.vue

It would be nice to be able to leverage definePageMeta to update those meta informations. Would that be possible @pi0 @danielroe ?

@pi0
Copy link
Member

pi0 commented Nov 2, 2022

It should be possible overriding name with pageMeta indeed. We already support similar alias: '/custom' with definePageMeta (https://stackblitz.com/edit/github-n8wcq6-p7fn2c?file=pages%2Fcustom-url-example.vue,pages%2Findex.vue)

Copy link
Member

Agreed 👍

Copy link
Member

atinux commented Nov 2, 2022

I guess this will land in RC13 👀

Copy link
Member

~> nuxt/framework#8633

@tiborpino
Copy link
Author

Indeed, I thought definePageMeta in the Vue page could actually do the trick, but it seems not: https://stackblitz.com/edit/github-n8wcq6?file=pages%2Fcustom-url-example.vue,pages%2Findex.vue

It would be nice to be able to leverage definePageMeta to update those meta informations. Would that be possible @pi0 @danielroe ?

No it won't. If you will define alias via page all your NuxtLink are fully dependent on the alias value. Change alias value and all links with that alias are broken.

My module will separate link key from file declaration and url for that page. When you decide to change page file name or page url, your key remains the same and all links are valid.

Copy link
Member

atinux commented Nov 3, 2022

By using Nuxt RC 13 (coming):

definePageMeta({
  name: 'my-key'
})

Will specify the name of the route, you can rename the page but the name won't change.

@tiborpino
Copy link
Author

tiborpino commented Nov 3, 2022

The code above will generate page on the url /my-key

When you want to change the url, you have to change the value of the name key in definePageMeta function. Then all keys are invalid.

@atinux atinux closed this May 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants