Prismic with nextjs getsingle, unknown type

I have getStaticProps functions on my homepage and news page to fetch single pages which always worked. But then I added a page with getServerSideProps and now nothing works not even my homepage which always did work.

Here is the fetch function for the homepage which I never changed

export async function getStaticProps({ previewData }) {
  const client = createClient({ previewData })

  const page = await client.getSingle('homepage')

  return {
    props: {
      page,
    },
  }
}

Here is the error message:

Hello @bjornandrip

Welcome to the Prismic community, and thanks for reaching out to us.

This error usually comes when you conflict between the name of types in your linkresolver or routeResolver function and the name given in Prismic docs. Can you paste the link resolver function and the name of your repo? You can send a repo name by private message.

Thanks,
Priyanka

Do you mean this?

import * as prismic from '@prismicio/client'
import * as prismicNext from '@prismicio/next'
import sm from './sm.json'

/**
 * The project's Prismic repository name.
 */
export const repositoryName = prismic.getRepositoryName(sm.apiEndpoint)

// Update the routes array to match your project's route structure
/** @type {prismic.ClientConfig['routes']} **/
const routes = [
  {
    type: 'homepage',
    path: '/',
  },
  {
    type: 'page',
    path: '/:uid',
  },
]

/**
 * Creates a Prismic client for the project's repository. The client is used to
 * query content from the Prismic API.
 *
 * @param config {prismicNext.CreateClientConfig} - Configuration for the Prismic client.
 */
export const createClient = (config = {}) => {
  const client = prismic.createClient(sm.apiEndpoint, {
    routes,
    ...config,
  })

  prismicNext.enableAutoPreviews({
    client,
    previewData: config.previewData,
    req: config.req,
  })

  return client
}

Do you mean my repo on prismic?

@bjornandrip Can you share the file system-based router of your project?

How and where do I do that? I am using nextjs so it's just the pages directory.
I just have files in the pages directory called index, about, news etc.

@bjornandrip I meant the folder structure along with the file and folder names under /pages. Can you share this with me?

Screenshot 2023-03-30 at 16.45.42

Hello @bjornandrip

It's difficult to troubleshoot for me with the provided information. Can you share your github repo in a private message?

Thanks,
Priyanka

Hello @bjornandrip

I have checked your code and repository.
To run the code correctly, you need to remove the page route from the prismic.io file because your repository doesn't have any page Custom Type. The correct routes is:

const routes = [
  {
    type: 'homepage',
    path: '/',
  },
]

Give this a try, and let me know.

Thanks,
Priyanka

1 Like

This worked thank you so much!

Great @bjornandrip. I am happy to help you. Feel free to reach out to us if you have any questions.

Hey, a similar issue happened to us today. We added a new customType and immediately the client functions stopped working because of the that error - Unknown type.

The solution was kind of unexpected - we needed to publish the first document of that type (even though we have the preview for drafts configured).

It seems like a bug to me. It should accept the valid customType value even if there are no documents of that type published. Otherwise any content editor can break the whole website if they remove all docs of any type (which is a rare scenario, but completely realistic and valid from the content editor perspective).

Hello @szd

Your repository Custom Type name and your routes should match each other. The routes and API only have the published documents. To get to work, you need to publish the docs if you added them in the routes.

Thanks,
Priyanka