Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: How to handle dynamically generated pages in Next JS? #215

Open
jtfairbank opened this issue Nov 12, 2024 · 3 comments
Open

Question: How to handle dynamically generated pages in Next JS? #215

jtfairbank opened this issue Nov 12, 2024 · 3 comments
Assignees
Labels
good first issue Good for newcomers question Further information is requested

Comments

@jtfairbank
Copy link
Contributor

Can anyone look into this and comment with your suggested approach? I'll need to describe a process in order to create related issues.

Examples:

  • On the Donate Page we'll have multiple fundraisers. For now we could hard-code them, but dynamically creating fundraisers (and the related "More Info" pages) will be one of the fist things we want to move to the CMS.
  • Down the road, we'll want to dynamically create Shipment pages based on data from our Shipment Data Reporting Pipeline project, so that partners and donors can see our progress and impact.
@jtfairbank jtfairbank added good first issue Good for newcomers question Further information is requested labels Nov 12, 2024
@github-project-automation github-project-automation bot moved this to Triage in DA Tech Nov 12, 2024
@jtfairbank jtfairbank moved this from Triage to Ready for Work in DA Tech Nov 12, 2024
@noelcastillon1 noelcastillon1 self-assigned this Nov 13, 2024
@noelcastillon1
Copy link

Hey @jtfairbank, here's what I came up with looking at this page from Strapi about integrating for Next.js. Let me know if you're thinking of dynamically generated content in a specific way that is not covered here.

Possible Solution

Use Next.js’s data fetching methods (getStaticProps, getServerSideProps (getStaticProps is better for our use case, probably)) to pull data from your Strapi API.

export async function getStaticProps() {
  const res = await fetch('http://localhost:1337/posts');
  const posts = await res.json();
  return { props: { posts } };
}

Render the fetched data in a Next.js component.

function Blog({ posts }) {
  return (
    <div>
      {posts.data.map(post => (
        <article key={post.id}>
          <h2>{post.attributes.title}</h2>
          <p>{post.attributes.content}</p>
        </article>
      ))}
    </div>
  );
}

@jtfairbank
Copy link
Contributor Author

Hey @noelcastillon1! Thanks for looking into this. That seems like a solid way to handle dynamic content on an overview page, such as listing all the active fundraisers as cards on the Donate page or listing relevant shipment summaries on a Region page. Ex:

Donate Page (static) => gets Strapi fundraiser content => Fundraiser Cards (dynamic component)
Region Page (static) => gets Strapi shipment content => Shipment List (dynamic component)

What I'm looking for is a way to create dynamic pages, where data from a server is applied to a template to make a new page, along with a route for that page. Ex:

Strapi fundraiser content => Fundraiser Details Page (dynamic page, 1 per fundraiser added to Strapi)
Strapi shipment content => Shipment Page (dynamic, 1 per shipment added to Strapi)

I think what you have so far is a great start and we should definitely document that approach! Want to keep looking into the other half of this?

@noelcastillon1
Copy link

Heard, I can continue with this and will look at dynamic pages!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers question Further information is requested
Projects
Status: Ready for Work
Development

No branches or pull requests

2 participants