Dynamic Routing not working as expected
See original GitHub issueBug 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:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
Can you try fixing the case mismatch? Your folder is
CaseStudies
but yourhref
iscaseStudies
.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.