react-starter-kit: Dynamic routes not working

Dynamic routes not working for me. I have uncommented and set:

  const items= await fetch(`http://${server.host}/api/items`).then(res => res.json())
  items.forEach(item=> routes.push(
    `/items/${item._id}`
  ))

And I am still redirected to 404 page

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 22 (4 by maintainers)

Most upvoted comments

/* src/routes/index.js */
// ...
   {
      path: '/items/:id',
      load: () => import(/* webpackChunkName: 'item' */ './item'),
    },
// ...
/* src/routes/item/index.js */
import React from 'react';
import Item from './Item';
import Layout from '../../components/Layout';

async function action({ params, fetch }) {
  const resp = await fetch(`/api/items/${params.id}`); // use your api here
  const item = await resp.json();
  return {
    chunks: ['item'],
    title: item.title,
    component: (
      <Layout>
        <Item {...item} />
      </Layout>
    ),
  };
}

export default action;