Skip to content
This repository has been archived by the owner on May 21, 2020. It is now read-only.

feat(ssr) Prismic package support ssr #433

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions packages/prismic/src/composables/usePrismic/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ref, Ref } from '@vue/composition-api';
import { PrismicQuery, PrismicMeta, PrismicOptions } from '../../types';
import { useSSR } from '../../../../core/core/src/utils';
import { Document } from 'prismic-javascript/d.ts/documents';
import loadDocuments from './loadDocuments';

Expand All @@ -13,19 +14,23 @@ interface UsePrismic {
search: Search;
}

export default function usePrismic(): UsePrismic {
export default function usePrismic(cacheId?: string): UsePrismic {
const { initialState, saveToInitialState } = useSSR(cacheId);
const loading = ref(false);
const error = ref(null);
const doc: Ref<Document | Document[]> = ref({});
const doc: Ref<Document | Document[]> = ref(initialState || {});
const meta: Ref<PrismicMeta | null> = ref(null);

const search: Search = async (query: PrismicQuery | PrismicQuery[], options: PrismicOptions = {}) => {
loading.value = true;
if (!initialState) {
loading.value = true;
}

const { results, metadata } = await loadDocuments(query, options);

meta.value = metadata;
doc.value = results;
saveToInitialState(doc.value);
loading.value = false;
};

Expand Down