This article explains how to install and configure Prismic in a SvelteKit project. By the end of this page, you will have Prismic utilities installed in your app.
Quickstart from the dashboard
To start building quickly, go to prismic.io/dashboard, select SvelteKit, and choose a starter.
Start from scratch
Create a SvelteKit project
Before proceeding, you will need a SvelteKit project. You can quickly create a brand new project using the following
create-svelte command. (Read the official documentation to learn more):
npm create svelte@latest
yarn create svelte@latest
This command will prompt an option to name your project. After that you'll be able to open your newly created SvelteKit app.
Once it's finished, you'll have a brand new SvelteKit project where you can install Slice Machine.
Run the setup command
In the root of your SvelteKit project, run the following command:
What happens when you run
The command executes the following actions in your codebase:
- Creates a new Prismic repository (or lets you specify an existing one)
- Adds a
- Creates a
slicemachine.config.jsonfile containing the name of your Prismic repository and the location of your slice library
- Creates a
prismicio.js|tsfile in the
src/lib/directory of your project to configure Prismic
- Detects your framework (SvelteKit)
- Installs dependencies:
- Creates a
src/routes/slice-simulator/+page.sveltefile to simulate slices
That might seem like a lot of dependencies. Don't worry, they each perform an important function:
@prismicio/clientis responsible for fetching data from the Prismic API
@prismicio/svelteprovides Svelte components to render data from the Prismic API
slice-machine-uiis a local development tool for building slices
@slicemachine/adapter-sveltekitis the SvelteKit adapter for Slice Machine
We also mentioned that the init command creates a
src/routes/slice-simulator/+page.svelte file. What does that file do? The simulator is a mini-app that simulates what your slices will look like in production. The slice simulator uses an
iframe, which runs locally, to simulate your slices. The mock data is provided by Slice Machine and is customizable. (See What Is Slice Machine? for more information.)
init script creates a file called
prismicio.js in the
src/lib/ directory of your project. This file contains configurations for your project.
The most important configuration is the
routes array, which is your route resolver. Update the route resolver to match the routing structure of your SvelteKit app. To learn more about how to add configurations, see the documentation for the route resolver and
prismicio.js file is prefilled with an example routes array:
const routes = [
prismicio.js, customize the
routes array to match the routing of your project. For each page type, add an object that describes the route for that page. Learn more about how the route resolver works.
You now have Prismic utilities available throughout your project.
Now you can start creating slices and types to model your project.
Was this article helpful?
Can't find what you're looking for? Spot an error in the documentation? Get in touch with us on our Community Forum or using the feedback form above.