Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intergrate Prismic.io #14

Open
M4TThys123 opened this issue Jan 23, 2023 · 3 comments
Open

Intergrate Prismic.io #14

M4TThys123 opened this issue Jan 23, 2023 · 3 comments

Comments

@M4TThys123
Copy link
Owner

M4TThys123 commented Jan 23, 2023

Primsic.io connection

We gaan gebruik maken van twee packages om te werken met Prismic in Svelte.

  • @prismicio/client is voor het fetching van content uit de API
npm install -D @prismicio/client
@M4TThys123
Copy link
Owner Author

M4TThys123 commented Jan 23, 2023

prismicClient.js

Maak vervolgens een nieuwe directory in /src, in /vendors genaamd /lib. Daarin maak je een nieuwe file genaamd prismicClient.js, en plak deze code:

~/src/lib/vendors/prismicClient.js

import * as prismic from '@prismicio/client'

// Update your repository name here
const repositoryName = ''

const createClient = (params) => {
  const client = prismic.createClient(repositoryName, params)

  return client
}

export default createClient

Het prismicClient.js file zorgt er voor dat je instellingen kunt meegeven voor je Prismic API connection.

@M4TThys123
Copy link
Owner Author

Fetch Prismic.io data

Nu gaan we een endpoint maken om content te fetchen van de repo. Maak het bestand +page.js, en plak deze code:

~/src/routes/+page.js

import createClient from '$lib/prismicio'

export async function get({ fetch }) {
  const client = createClient({ fetch })
  const document = await client.getByUID('page', 'homepage')

  if (document)
    return {
      body: {
        document,
      },
    }

  return {
    status: 404,
  }
}

Deze endpoint heet +Page.js. Deze queriet de API voor een homepage component. Als het een document vind, geeft het deze terug. Anders geeft het een error.

@M4TThys123
Copy link
Owner Author

M4TThys123 commented Jan 24, 2023

Render data using @primicio/helpers package

  • Install @primicio/helpers
npm install @primicio/helpers

+page.svelte

  • Import @prismicio/helpers
  • Add prismicH.asHTML
<script>
      import * as prismicH from "@prismicio/helpers";
      export let data;
      const textField = data.documents[0].data.ritch_text_field
</script>

{@html prismicH.asHTML(textField)}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant