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

...slug.js undefined data #72

Open
sunilsingh2019 opened this issue Nov 19, 2022 · 0 comments
Open

...slug.js undefined data #72

sunilsingh2019 opened this issue Nov 19, 2022 · 0 comments

Comments

@sunilsingh2019
Copy link

Trying to fetch data from slug [...slug].js page but showing undefined.

slug page code here
import {GET_PAGES_URI} from '../queries/pages/get-pages';
import {isEmpty} from 'lodash';
import {GET_PAGE} from '../queries/pages/get-page';
import {useRouter} from 'next/router';
import client from '../apollo/client';
import Layout from '../components/layout';

const Page = ( {data} ) => {

console.log('data', data);

const router = useRouter();

// If the page is not yet generated, this will be displayed
// initially until getStaticProps() finishes running
if ( router.isFallback ) {
	return <div>Loading...</div>;
}

return (

	<>
	<Layout data={data}>
		{router?.query?.slug.join("/")}
	</Layout>
</>
	);

}

export default Page;

export async function getStaticProps( {params} ) {
const {data, loading, networkStatus} = await client.query( {
query: GET_PAGE,
variables: {
uri: params?.slug.join( '/' ),
},
} );

return {
	props: {
		data: {
			menus: {
					headerMenus: data?.headerMenus?.edges || [],
					footerMenus: data?.footerMenus?.edges || [],
			},
			page: data?.page ?? {},
			path: params?.slug.join("/"),
		}
	},
	revalidate: 1,
};

}

export async function getStaticPaths() {
const {data} = await client.query( {
query: GET_PAGES_URI
});

const pathsData = [];

data?.pages?.nodes && data?.pages?.nodes.map( page => {
	if ( ! isEmpty( page?.uri ) ) {
		const slugs = page?.uri?.split( '/' ).filter( pageSlug => pageSlug );
		pathsData.push( {params: {slug: slugs}} );
	}
} );

return {

	paths: pathsData,
	fallback: true
};

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant