New Build: Sync slices/types from the Prismic UI, or re-create manually?

I've recently created a new Next.js project to upgrade from an early version of Prismic, to use the lastest edition of the Slicemachine, and a new version of Next.js.

When building the new site, I've been leaning on the Prismic data already there, and manually creating my slices and content types in the new slicemachine locally. Then, I'm replacing the relevant parts of the JSON for the Types with the code from JSON editor so that everything is "synced" with the db.

Is this the right approach, or is there a better way to "pull" the types and slices into my new code base, so that I can ensure nothing will break when I sync everything again and/or go live with the new project?

On a related note, I didn't copy all of the content types over into the new slice machine, since some were not being used any more. Will these ever disappear because they're not in the new repo, or do they live on because they're in the db? I wasn't able to find exactly the info I need about this in the docs; this is kind of new since Types weren't ever in the old version of the slicemachine, but there they are in the new one...

Would appreciate any thoughts on this topic!

Michael

Hello Michael,

Thanks for reaching out to us.

I already have answered here:

Please check out this thread and let me know if you have further questions.

Thanks,
Priyanka

thanks @Priyanka, but I'm not sure this address the question exactly. I don't want to pull from the slicemachine, as I've already reproduced everything I need. I'm wondering if I can pull the Types from the Prismic endpoint, where Types were originally created. The new slicemachine provides the ability to manage Types, whereas previously these were only defined in the Prismic db.

I'm wanting to avoid overwriting the existing Types in the Prismic endpoint since I had to create them in the new slicemachine, as I'm only rebuilding the web project, not the Prismic db endpoint.

Does that clarify? I think I'm safe as it indicates things are "synced" but I don't want to risk breaking things...

Hello @mike.laroy

I understand your question now. Thanks for clarifying.

Currently, it's impossible to pull Custom Types from the Prismic repository to Slice Machine. You have to create Custom Types again in the Slice Machine.

Thanks,
Priyanka

I've found a kind-of workaround, where if you edit your custom type on the server and go to the JSON view. Then you can copy the JSON out of there an in to the json field in the corresponding file on your local machine.

I've just tried it now and it's removed the badge next to the Changes menu, which I believe means they are now back in sync.

It's a bit hacky, but give it a try

1 Like

Yes I did something like this in the end!

For anyone else that lands here, my solution was to use the Custom Types API to pull down my current types and generate the directories and files to dump their JSON into. I used this quick Node.js script:

import fs from "node:fs"
import path from "node:path"
import url from "node:url"

async function getContentTypes() {
  const response = await fetch("https://customtypes.prismic.io/customtypes", {
    headers: {
      Authorization: "Bearer " + YOUR_BEARER_TOKEN,
      repository: YOUR_REPOSITORY_ID,
    },
  })

  const types = await response.json()

  for (const type of types) {
    const directory =
      path.dirname(url.fileURLToPath(import.meta.url)) +
      "/customtypes/" +
      type.id

    const filepath = directory + "/index.json"

    fs.mkdirSync(directory, { recursive: true })
    fs.writeFileSync(filepath, JSON.stringify(type))
  }
}

getContentTypes()

This pulled down the current types in my repo, created the directories and files I needed and dumped their contents into them. Slicemachine is showing 0 changes needed to be pushed meaning the content types in my code repository are the same as the ones in the Prismic repository.

1 Like