To fetch feature data using your fetchFeatureDataForProfiles function inside the RootLayout component in a Next.js application, you can invoke the function in an asynchronous manner, waiting for both the data to return and for the component to render.
Here's how you can do it step by step:
Step 1: Import the Function
Ensure that you import fetchFeatureDataForProfiles in your RootLayout component file.
// Import your fetch function (adjust the import path as necessary)
import fetchFeatureDataForProfiles from '@utils/fetchFeatureDataForProfiles'; // Adjust the path to your utility
Step 2: Modify the RootLayout Component
You need to call the fetchFeatureDataForProfiles function with the appropriate profileIds, wait for it to resolve, and pass its data into your context provider or use it in your component.
Assuming you have a set of profileIds to pass, modify your RootLayout as follows:
import type { Metadata } from 'next';
import GlobalContextProvider from '@components/GlobalContextProvider/GlobalContextProvider';
import getConfigs from '@utils/NextgenConfig';
import fetchFeatureDataForProfiles from '@utils/fetchFeatureDataForProfiles'; // Import the fetch function
import '../styles/globalStyle.scss';
export const metadata: Metadata = {
title: 'NG Operations Center',
};
export default async function RootLayout({ children }: { children: React.ReactNode }) {
const { configs } = getConfigs();
const configData = { ...configs };
const { NEXT_ASSETS_BASE_PATH } = configData;
// Dummy profileIds array; you should replace this with actual profileIds
const profileIds = ["profileId1", "profileId2", "profileId3"]; // Replace these with actual profile IDs
// Call fetchFeatureDataForProfiles to get the feature flags
const featureFlags = await fetchFeatureDataForProfiles(profileIds);
const fontAwesomeSourcePath = `/fontawesome-pro${process.env.TENANT_NAME === 'global' ? '' : '-prod'}`;
return (
<html lang="en">
<head>
<link
rel="stylesheet"
type="text/css"
href={`${NEXT_ASSETS_BASE_PATH}${fontAwesomeSourcePath}/css/fontawesome.min.css`}
/>
<link
rel="stylesheet"
type="text/css"
href={`${NEXT_ASSETS_BASE_PATH}${fontAwesomeSourcePath}/css/brands.min.css`}
/>
<link
rel="stylesheet"
type="text/css"
href={`${NEXT_ASSETS_BASE_PATH}${fontAwesomeSourcePath}/css/solid.min.css`}
/>
<link
rel="stylesheet"
type="text/css"
href={`${NEXT_ASSETS_BASE_PATH}${fontAwesomeSourcePath}/css/regular.min.css`}
/>
<link rel="icon" href={`${NEXT_ASSETS_BASE_PATH}/icons/amway-favicon.ico`} sizes="any" />
</head>
<body>
<GlobalContextProvider configData={configData} featureFlags={featureFlags}>
{children}
</GlobalContextProvider>
</body>
</html>
);
}
Things to Consider:
- Profile IDs: Ensure you have an appropriate array of profile IDs that you want to fetch the feature flags for, as this code uses a dummy array (
profileIds).
- Handling Error: You might want to wrap the fetching logic in a try-catch block to handle potential errors gracefully. Here’s a modified version of the fetching with error handling:
let featureFlags = {};
try {
featureFlags = await fetchFeatureDataForProfiles(profileIds);
} catch (error) {
logger.error('Error fetching feature flags', error);
// Handle error appropriately: maybe set some default feature flags, etc.
}
- Pass Feature Flags: Ensure that your
GlobalContextProvider is set up to accept the featureFlags prop. You might need to adjust its implementation to handle the received feature flags properly.
Summary
This approach allows you to fetch feature data asynchronously at the start of your application and inject it into your context provider, ensuring that the rest of your application can access this data as required.