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.

Context failed to pass in dynamic route

See original GitHub issue

Description

Having used Gridsome from version 0.5, I’m relatively new to the newly introduced dynamic routing in version 0.7.5

I have created an empty file /customer/[:id].vue and use an API that GET all customer information and pass the context to each route by using dynamic route /customer/:id .

However, when I click into individual customer page, for example, /customer/1 and /customer/2 , both context.name remain the same value. context.name is called in the customer template.

I haven’t implemented any rewrite rule as I’m not entirely sure where and how I should apply it based on my use case. The documentation is pretty limited too.

Any help? Am I using dynamic route the correct way?

// gridsome.server.js

api.createManagedPages(async ({ createPage }) => {
    const agent = new https.Agent({
      rejectUnauthorized: false
    });
    const { data } = await axios.get(
      "https://api.mycompany.com/Customer/GetAll",
      { httpsAgent: agent }
    );

    data.forEach(item => {
      console.log(item.customer_id)
      console.log(item.name)
      console.log(item.alias)
      createPage({
        path: `/customer/:id(\\d+)`,
        component: "./src/templates/Customer.vue",
        context: {
          id: item.customer_id,
          name: item.name,
          alias: item.alias
        }
      });
    });
  });

// /templates/Customer.vue 

<template>
  <Layout>
    <div class="main-content">
      <div class="header">
        <div class="container-fluid">
          <!-- Body -->
          <div class="header-body">
            <div class="row align-items-center">
              <div class="col-auto">
                <!-- Avatar -->
                <a href="#!" class="avatar avatar-lg">
                  <span class="avatar-title rounded bg-white text-warning">
                    <span class="fe fe-user"></span>
                  </span>
                </a>
              </div>
              <div class="col ml-n3 ml-md-n2">
                <!-- Pretitle -->
                <h6 class="header-pretitle">Customer</h6>

                <!-- Title -->
                <h1 class="header-title">{{ $context.name }}</h1>
              </div>
            </div>
            <!-- <SingleCustomerTopNavbar /> -->
            <div class="row align-items-center">
              <div class="col">
                <ul class="nav nav-tabs nav-overflow header-tabs">
                  <li
                    class="nav-item"
                    v-for="tab in tabs"
                    v-bind:key="tab"
                    v-on:click="currentTab = tab"
                  >
                    <a
                      href="#"
                      class="nav-link"
                      v-bind:class="['tab-button', { active: currentTab === tab }]"
                    >{{ tab }}</a>
                  </li>
                </ul>
              </div>
            </div>
            <!-- end nav bar -->
          </div>
        </div>
      </div>
      <component v-bind:is="currentTabComponent" v-bind:customer_name="$context.name" v-bind:customer_id="$context.id"></component>
    </div>
  </Layout>
</template>

<script>
import Layout from "~/layouts/Default.vue";
import SingleCustomerTopNavbar from "~/components/navbar/SingleCustomerTopNavbar.vue";
import Overview from "~/components/customer/Overview.vue";
import News from "~/components/customer/News.vue";
import Products from "~/components/customer/Products.vue";
import Visits from "~/components/customer/Visits.vue";

export default {
  data() {
    return {
      currentTab: "Overview",
      tabs: ["Overview", "News", "Products", "Visits", "Share"]
    };
  },
  computed: {
    currentTabComponent: function() {
      return this.currentTab;
    }
  },
  components: {
    Layout,
    SingleCustomerTopNavbar,
    Overview,
    News,
    Products,
    Visits
  }
};
</script>

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
brandonpittmancommented, Mar 24, 2020

createPage with a dynamic path will only create one page, so thats why you always get the same context. I think what you should do instead, is to use createRoute. It’s not documented yet, but I don’t think it is going to change and works pretty well. It is used for templates, for example. Here is how it works:

@hjvedvik When is this going to be officially in the docs?

1reaction
u12206050commented, Oct 25, 2019

I am having the same issue, though it also doesn’t even work when I create explicit pages:

console.log(`Creating topic: ${topic.slug}`)
        createPage({
          path: topic.slug,
          component: './src/templates/ql_Topic.vue',
          context: {
            id: topic.id
          }
        })

        const perPage = 1
        const noOfPages = Math.ceil(topic.count / perPage)
        for(let i = 1; i <= noOfPages; i++) {
          createPage({
            path: `${topic.slug}/${i}`,
            component: './src/templates/ql_Topic.vue',
            context: {
              id: topic.id,
              page: i
            }
          })
        }

when querying allPage I do see the correct context, but it is never sent to the template, eg. $context is {}


      {
        "path": "/topic/strategi/",
        "context": {
          "id": "1"
        }
      },
      {
        "path": "/topic/tanker/",
        "context": {
          "id": "2"
        }
      },
      {
        "path": "/topic/vart-arbeid/",
        "context": {
          "id": "3"
        }
      },
      {
        "path": "/topic/vart-arbeid/1/",
        "context": {
          "id": "3",
          "page": 1
        }
      },
Read more comments on GitHub >

github_iconTop Results From Across the Web

My next.js code for dynamic content and routing isn't ...
I'm trying to create a dynamic content and routing for my website with the codes below but there is an error: import Link...
Read more >
Router tutorial: tour of heroes
Define an array of routes, appRoutes , and pass them to the RouterModule. ... The application fails if the user clicks that button...
Read more >
How to use getStaticPaths on dynamic routes in Next.js
You will learn how to make use of Next.js getStaticPaths & getStaticProps on a dynamic route to create a Master-Detail UI commonly used...
Read more >
Shallow Routing
This is due to middleware being able to rewrite dynamically and can't be verified client-side without a data fetch which is skipped with...
Read more >
Troubleshooting Router Error Responses
For more information, see Dynamic Routing Table in the Gorouter ... ContextCancelled, 499, No, Client/App, Logs with error context canceled
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