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.

Define routes for nodes in one place

See original GitHub issue

Move route options for all content types into a routes (or any better name) section in gridsome.config.js. Currently, each source plugin implements routes on their own. But after this change, source plugins doesn’t need to worry about routing etc. We also believe it will give a better overview having all node routes in one place. And it will be easier to extend the functionality later. Content types not defined there will not be generated or have a path field in the GraphQL schema.

The routes config will be config for how pages for nodes should be generated. This is not a breaking change, but the route option for content types will be deprecated.

So instead of defining a route for the source or a content type like this:

{
  use: '@gridsome/source-filesystem',
  options: {
    typeName: 'User',
    route: '/user/:name'
  }
}

Define the route in routes like this:

module.exports = {
  routes: {
    User: '/user/:name' // typeName: route
  }
}

Here is how the full config might look like:

module.exports = {
  routes: {
    // simple config
    Post: '/:year/:month/:day/:slug',
    // specify component
    Post: {
      path: '/:year/:month/:day/:slug',
      component: './src/templates/CustomPostTemplate.vue'
    },
    // all possible options
    Post, {
      path: '/:year/:month/:day/:slug',
      component: './src/templates/Post.vue',
      dynamicRoute: false, // generate one route for each node
      fieldName: 'path' // graphql field name
    },
    // generate multiple templates for content type
    Author: [
      {
        path: '/author/:name',
        component: './src/templates/Author.vue'
      },
      {
        path: '/author/:name/books',
        component: './src/templates/AuthorBooks.vue',
        fieldName: 'booksPath'
      }
    ]
  }
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:27
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
hjvedvikcommented, May 27, 2019

I think pages/[id].vue looks much better @Atinux 😃 We’ll consider the same for the same reasons.

@Al-Rozhkov Nested routes and named views could be interesting. I’m not sure how to implement it with automatic generation and GraphQL etc atm, but will definitely take a look at it sometime.

2reactions
tomtevcommented, May 27, 2019

@Al-Rozhkov 0.7 will have client-side route support. We’re thinking about doing it like Nuxt does. Where you add for example a user/_id.vue file to get a user/:id route + an option to do it programmatically.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Express Tutorial Part 4: Routes and controllers - MDN Web Docs
The route paths define the endpoints at which requests can be made. The examples we've seen so far have just been strings, and...
Read more >
Routing in Node.js - GeeksforGeeks
We define the routes by using the methods of this “app” object. This app object specifies a callback function, which is called when...
Read more >
How To Define Routes and HTTP Request Methods in Express
You will learn how to define routes and use the HTTP request methods GET , POST , PUT , and DELETE to manipulate...
Read more >
express.js - single routing handler for multiple routes in a ...
I know this is a syntax mess, but just for giving an idea of what I would like to achieve, an array of...
Read more >
Node JS — Router and Routes - Medium
In this blog, I am explaining how to perform routing with Node JS. Routing is one of the most important parts of any...
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