#dynamic pages
1 messages · Page 1 of 1 (latest)
Hi @desert isle. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
In Astro, you can pass additional data to each generated page using the props value on every returned path object in the getStaticPaths() function. Unlike params, props are not encoded into the URL and so aren’t limited to only strings.
Here's an example of how you can pass data to a page:
---export async function getStaticPaths() {
const data = await fetch('...').then(response => response.json());
return data.map((post) => {
return {
params: { id: post.id },
props: { post },
};
});
}
const { id } = Astro.params;
const { post } = Astro.props;
---<h1>{id}: {post.name}</h1>
In this example, pages are generated based on data fetched from a remote API. The full data object is passed to the page component inside of getStaticPaths.
You can also pass a regular array, which may be helpful when generating or stubbing a known list of routes.
---export async function getStaticPaths() {
const posts = [
{id: '1', category: "astro", title: "API Reference"},
{id: '2', category: "react", title: "Creating a React Counter!"}
];
return posts.map((post) => {
return {
params: { id: post.id },
props: { post }
};
});
}
const {id} = Astro.params;
const {post} = Astro.props;
---<body>
<h1>{id}: {post.title}</h1>
<h2>Category: {post.category}</h2>
</body>
In this case, Astro will statically generate posts/1 and posts/2 at build time using the page component in pages/posts/[id].astro. The page can reference this data using Astro.props.
For more information, you can refer to the Astro documentation on data passing with props (https://docs.astro.build/en/reference/api-reference#data-passing-with-props).
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@desert isle kapa.ai is still learning and improving, please let us know how it did by reacting below
@south creek What happen if I pass empty array to paginate
Let me check the knowledge base for you. One moment! :books:
If you pass an empty array to the paginate() function in Astro, it will generate a single page with no data. This is because the paginate() function is designed to create one URL for every page of the paginated collection. If the collection is empty, it will still create a single page, but without any data.
Here's an example:
---export async function getStaticPaths({ paginate }) {
const astronautPages = []; // Empty array
return paginate(astronautPages, { pageSize: 2 });
}
const { page } = Astro.props;
---
<h1>Page {page.currentPage}</h1>
<ul>
{page.data.map(({ astronaut }) => <li>{astronaut}</li>)}
</ul>
In this case, Astro will generate a single page at /astronauts/1, but the page.data array will be empty, so no list items will be rendered.
For more information, you can refer to the Astro documentation on pagination (https://docs.astro.build/en/core-concepts/routing#pagination).
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@desert isle kapa.ai is still learning and improving, please let us know how it did by reacting below
@south creek But I don't want to create page if array is empty how to do that
Let me check the knowledge base for you. One moment! :books:
If you don't want to generate a page when the array is empty, you can add a condition to check if the array is empty before calling the paginate() function. If the array is empty, you can return an empty array from getStaticPaths(), which will result in no pages being generated.
Here's an example:
---export async function getStaticPaths({ paginate }) {
const astronautPages = []; // Empty array
// Check if the array is empty
if (astronautPages.length === 0) {
return []; // Return an empty array
}
// If the array is not empty, generate the pages
return paginate(astronautPages, { pageSize: 2 });
}
const { page } = Astro.props;
---
<h1>Page {page.currentPage}</h1>
<ul>
<!--List the array of astronaut info-->
{page.data.map(({ astronaut }) => <li>{astronaut}</li>)}
</ul>
In this case, if astronautPages is empty, getStaticPaths() will return an empty array and no pages will be generated. If astronautPages is not empty, the pages will be generated as usual.