I'm trying to query my documents into a NextJs app using getServerSideProps
, and I've followed all of the tutorials in the Prismic documentation (which, I'd like to respectfully add, could probably use a bit of clarity and updating. I found it pretty confusing.) However, no matter how I've queried the documents, I'm only ever receiving an empty object.
I've created a client, like so:
import Prismic from 'prismic-javascript';
export const apiEndpoint = 'https://one-day-marketing.prismic.io/api/v2/';
export const accessToken = '';
export const Client = (req = null) => {
Prismic.client(apiEndpoint, createClientOptions(req, accessToken));
const createClientOptions = (req = null, prismicAccessToken = null) => {
const reqOption = req ? { req } : {};
const accessTokenOption = prismicAccessToken ? { accessToken: prismicAccessToken } : {};
return {
...reqOption,
...accessTokenOption,
};
};
};
And have been attempting to query my documents, we'll take homepage
as an example, like so:
import React from 'react';
import { Helmet } from 'react-helmet';
import Prismic from 'prismic-javascript';
import { Client } from '../../prismic-configuration';
// tslint:disable no-default-export
export default (index: any) => {
return (
<>
<Helmet title={'Home'} />
<pre>{JSON.stringify(index)}</pre>
</>
);
};
export const getServerSideProps = async () => {
const client = Client();
const index = await client.query(Prismic.Predicates.at('document.type', 'homepage'));
console.log(index);
return { props: { index } };
}
However, the console.log
in the getServerSideProps
function, and the stringified JSON are simply returning an empty object {}
, and I am at a complete and total loss for how to get these results back. Like I said, the Prismic documentation wasn't as helpful as I'd hoped, so I've been scouring YouTube and the far reaches of the internet to get this working, but I haven't gotten anything so far. What am I doing wrong?