14 Jul 2024
https://latestartdev.com/page?param1=value1¶m2=value2
For my personal projects, I was using c-panel and hosting through Hostgator. For work, our web server is built with Apache. I would build the application locally, and everything would work the way I'd expect it to work with info rendering nicely using query stringing. And then, when I'd serve it up through one of the aforementioned servers, any time I'd refresh the page populated from the query string, the page would just break. So frustrating! I have zero experience with setting up either server, so I didn't really know what to do.app|__page.jsx|__posts.json|__[postSlug] |__ page.jsx
Take note of the folder named [postSlug]. To make the URL dynamic, it's essential to have the folder name surrounded by brackets like above. posts.json - 'posts': [ {title:'My First Post',query:'first-post'}, {title:'My Second Post',query:'second-post'}
import Posts from './posts.json'; Posts.posts.map( item => { return ( <div key={item.id}> <Link href={`demo/${item.query}`}>{item.title}</Link> </div> );})]
import Posts from "../posts.json";const page = ({params}) => { const queryString = params.render; console.log(queryString); const foundPost = Posts.posts.find((item) => item.query === queryString); return ( <> <h1>{foundPost.title}</h1> </> );};
Following the above we are rendering out the 'title' key value pair that is connected to the 'query' field on our JSON file using the file method.I found this video that I thought was very helpful: YOUTUBE VIDEO . Check it out if you are looking for something a little more in-depth.Dynamic routes in Next.js offer a robust mechanism for creating flexible and SEO-friendly web applications. By leveraging dynamic segments, accessing URL parameters, and seamlessly linking between pages, developers can enhance user experiences and streamline development. Whether you're building a blog, e-commerce site, or any dynamic application, Next.js' dynamic routing capabilities provide powerful solutions to meet your needs. Explore, experiment, and harness the full potential of Next.js for your projects. I hope this guide proves helpful to you!