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.

Dynamic Routing not working as expected

See original GitHub issue

Bug report

Describe the bug

I’ve just upgraded to Next 9 and as such I would like to take advantage of dynamic routing. In my pages folder I’ve made a CaseStudies folder and inside I have a file named [study.js]. The inside of that file looks like so

import { useRouter } from 'next/router';
import caseStudiesPosts from '../../data/caseStudiesPosts.js';
import '../../style.sass';

Post.getInitialProps = ({query}) => {

  return {query}; //has to be like an object
}


function Post(props) {
  const router = useRouter();
  const postData = caseStudiesPosts.find((data) => {return data.title === router.query.study});
 
  return (
    <React.Fragment>
      <h1>{postData.title}</h1>
      <p>{postData.description}</p>
    </React.Fragment>
  );
}

export default Post

When I follow the documentation and use a link as follows

<Link href = '/caseStudies/[study]' as = {/caseStudies/${props.projectTitle}} ><a></a></Link>

I get the following errors:

Development - Works except when you refresh the page and use the back button in the browser the URL changes but it remains on the page ( /caseStudies/[study] )

Production - Doesn’t work at all. Throws 404 errors in the console. Right clicking and opening up a new tab however loads the page fine.

Using the link this way works on both development and production with no issues

<Link href = {/caseStudies/${props.projectTitle}} ><a></a></Link>

The pretty URL links work, refresh works, back button works. Not sure if I’m either missing or misreading something, but I’m not quite sure what’s going on. I’m not using a custom server and this is a small static project, so I’m only using a few libraries.

  • OS: [e.g. macOS, Windows] Windows 10 Home version 1803
  • Browser (if applies) [e.g. chrome, safari] tested on chrome and firefox
  • Version of Next.js: [e.g. 6.0.2] 9.0.3

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
Timercommented, Aug 7, 2019

Can you try fixing the case mismatch? Your folder is CaseStudies but your href is caseStudies.

0reactions
balazsorban44commented, Jan 31, 2022

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamic Routing not working as expected #8278 - GitHub
I'm not using a custom server and this is a small static project, so I'm only using a few libraries. OS: [e.g. macOS,...
Read more >
My next.js code for dynamic content and routing isn't working ...
My expected URL is like "localhost:300/vehicle/jahid" and the content of the page for that url should be "jahid's vehicle". Its a dynamic ......
Read more >
Dynamic Routing Not Working As Expected In Oracle API ...
and trying to configure use case 1. When the client makes a request to OAG, we see the following: the path '<hostname>.<domain>:443' does...
Read more >
Dynamic Routing with static home page not working as expected
I have just set up a Ghost Blog. This is a clean install on a VPS, and it is up and running correctly....
Read more >
Routing in Next.js – How to Set Up Dynamic Routing with Pre ...
In this tutorial, you'll learn how to set up dynamic routing in Next.js. You'll also learn about pre-rendering and why it's important.
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