Single codebase, dynamic slicemachine

Hi Guys, i hope you well.

I'm creating a single codebase and i want to use slicemachine in it. Now, i have a doubt... is there any way to use only one codebase to multiple slicemachine?

In the file: sm.json, we have the apiEndpoint property. I thought about do something like that:

{
  "apiEndpoint": `https://{process.env.PRISMIC_PROJECT}.cdn.prismic.io/api/v2`,
  "libraries": [
    "@/src/components/slices"
  ],
  "_latest": "0.1.0",
  "storybook": "http://localhost:8888"
}

So with this, i could run:

PRISMIC_PROJECT=my-prismic yarn start
OR
PRISMIC_PROJECT=my-prismic-two yarn start

Is possible do something like that?

@Phil can you help me with this?

Hey @telco.pls,

Can you say more about your use case? Are you going to duplicate the codebase for multiple projects, or do you want to switch between repositories in one app? What is this for?

Sam

Hey @samlittlefair .

I want to swtich between repositories in one app (codebase).

is it possible?

Thanks.

Hi @telco.pls,

I've never seen anyone try that before. Why do you want to switch between repositories in one app? (If I understand your app better, I can give better suggestions.)

Sam

Hi @samlittlefair .

We want a single codebase for multiple websites. We have done it before, because our codebase is generic and we need only to change the prismic api. So we do it, based on a environment variable.

Something like: https://${PRISMIC_PROJECT}.cdn.prismic.io/api/v2

But, we can't do this with slicemachine. Because the slicemachine create a sm.json file and there's an apiEndpoint.

Can you understand now?

Thank you so much.

Hey @telco.pls,

The main benefit of Slice Machine is for modelling content in your Prismic repo. If what you're describing is possible (though I'm not sure it is; I'll check with my team), you would have to manage your Custom Types and Slices separately for each repo. That means if you change your Custom Types or Slices, you would have to change the environment variable to push the changes to each repo. Is that what you have in mind?

Sam

Hi @telco.pls,

Env variables and Slice Machine have previously been discussed here:

Although, it's not possible at the minute, but tracked as a feature request.

(This is something the team really wants to implement but it’s not in the pipe right now. If you have a certain scripted flow, you can rewrite the manifest whenever necessary of course.)

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

I've done a workaround for this, I think it fits @telco.pls what he is looking for.

Working with Next.js, in prismicio.ts file, I've created a dynamic object instead of fetching data from the static JSON file:

const config = {
	repositoryName: process.env.NEXT_PUBLIC_PRISMIC_REPOSITORY || "",
	adapter: "@slicemachine/adapter-next",
	libraries: ["./src/prismic/slices"], // libraries can be dynamic too, of course
	localSliceSimulatorURL: process.env.NEXT_PUBLIC_STOREFRONT_URL + "/slice-simulator"
}

Instead of:

import config from "../slicemachine.config.json";

Also, change how local slicemachine is being called:

 npm add dotenv-cli --save-dev

Create a prismic-slicemachine-config.cjs file to create a slicemachine.config.json dynamically:

const path = require('path');
const fs = require('fs');

const config = {
	repositoryName: process.env.NEXT_PUBLIC_PRISMIC_REPOSITORY || "",
	adapter: "@slicemachine/adapter-next",
	libraries: ["./src/prismic/slices"],
	localSliceSimulatorURL: process.env.NEXT_PUBLIC_STOREFRONT_URL + "/slice-simulator"
}

fs.writeFileSync(
	path.join(__dirname, '..', 'slicemachine.config.json'),
	JSON.stringify(config, null, 2)
);

And then, modify package.json's scripts:

"slicemachine": "dotenv -e .env -- node src/prismic-slicemachine-config.cjs && start-slicemachine"

I hope this can help.

1 Like