fridays / next-routes

Universal dynamic routes for Next.js
MIT License
2.47k stars 230 forks source link

Problem creating a dynamic Link #247

Closed thmsgbrt closed 5 years ago

thmsgbrt commented 5 years ago

Hey guys, I think I followed the instructions but I've an issue when I try to create a route with Link.

I do manage to receive params on my dynamic URL but I can't successfully create a link.

// server.js

const express = require("express");
const next = require("next");
const routes = require("./routes");
const app = next({ dev: process.env.NODE_ENV !== "production" });
const handler = routes.getRequestHandler(app);
const address = require("address");
const qrcode = require("qrcode-terminal");

app.prepare().then(() => {

  const server = express();

  // here you could do some express stuff if you fancy, eg. running custom middleware or offering an API
  // server.get('/api', (req, res) => {
  //   return res.send({ version: 1.0 })
  // });

  // always make sure the handler is the last route entry
  server.use(handler);

  const port = process.env.PORT || 3000;
  server.listen(port, (err) => {
    if (err) throw err;
    console.log(`> Env ${process.env.WILD_ENV} Ready on http://localhost:${port}`); // eslint-disable-line no-console

    const url = `http://${address.ip()}:${port}`;
    qrcode.generate(url, {small: true});
  });

});

// routes.js

const nextRoutes = require('next-routes');
const routes = (module.exports = nextRoutes());

routes.add('index', '/', 'index');
routes.add('blog', '/blog/:slug', 'index3');

// Nav.js

import React from 'react';
import { Link } from '../routes';

const Nav = () => {
  return (
    <nav>
      <Link route="/blog" params={{ slug: 'toztzeotiezotiz' }}>
        <a>Hi</a>
      </Link>
      <Link route="/blog/wai">
        <a>Hi</a>
      </Link>
    </nav>
  );
};

export default Nav;

The first Link generates a ".../blog" while the second link create a ".../blog/wai" (and works fine).

Am I doing something wrong?

Thanks, Thomas

thmsgbrt commented 5 years ago

Ok, for those for end here, in the first link, the leading "/" was the problem.

Sorry for the useless issue.