Initial Data Fetching for Pages Router
Before initializing the search functionality in a server-side rendered application, you need to perform an initial data fetch using getServerSideProps
. Key points to keep in mind are:
- Use getServerSideProps to handle the initial data fetch on the server.
- Pass all required configurations (
siteKey
,apiKey
,defaultValues
,webUrlConfig
,apiUrlConfig
andcurrentPath
) to theinitialise
function within getServerSideProps. - Use
UnbxdSearchSSRWrapper
instead of the standard CSR wrapper. - The initial data will be passed as props and used for both server rendering and client hydration.
- The getServerSideProps code must be present in the
page.tsx
file.
// This is a sample example.
// Please go through the entire documentation for understanding different usecases and update as per your needs.
import { initialise } from "@unbxd-ui/react-search-hooks/ssr-utilities";
export default function SearchPage({ initialData }) {
return (
<Wrapper initialData={initialData} />
);
}
export const getServerSideProps = async (context) => {
// Initial data fetch happens on server-side
const currentPath = context.resolvedUrl || "";
const initialData = await initialise({
siteKey: "your-site-key",
apiKey: "your-api-key",
currentPath,
defaultValues={{
pageSize: 12,
query: "*",
currentPage: 1,
view: "GRID",
sort: "",
}}
webUrlConfig={{
rangeFacets: ["price"],
categoryFacets: ["categoryPath"],
externalParams: []
}}
apiUrlConfig={{
category: {...},
extraParams: () => {
//This is just a sample code , put your custom code here.
if (inField) {
return `category:"${inField}"`;
} else return null
}
}}
});
return {
props: {
initialData,
},
};
};