From c3559ed3255e2b2b7d82a49cad842c5c77887675 Mon Sep 17 00:00:00 2001 From: Bart van den Aardweg Date: Mon, 3 May 2021 09:59:00 +0200 Subject: [PATCH] fix: allow passing token for resolverelations --- src/bridge/context.tsx | 10 ++++++++-- src/bridge/init.ts | 3 ++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/bridge/context.tsx b/src/bridge/context.tsx index 1dfce61..a9f4631 100644 --- a/src/bridge/context.tsx +++ b/src/bridge/context.tsx @@ -18,6 +18,8 @@ interface ContextProps { interface ProviderProps { children: ReactNode; + /** Storyblok API token (only necessary if resolveRelations is set) */ + token?: string; /** * Relations that need to be resolved in preview mode, for example: * `['Post.author']` @@ -27,7 +29,11 @@ interface ProviderProps { const StoryContext = createContext(undefined); -const StoryProvider = ({ children, resolveRelations }: ProviderProps) => { +const StoryProvider = ({ + children, + token, + resolveRelations, +}: ProviderProps) => { const [, setStoryState] = useState(undefined); const storyRef = useRef(undefined); @@ -46,7 +52,7 @@ const StoryProvider = ({ children, resolveRelations }: ProviderProps) => { useEffect(() => { if (window?.location?.search?.includes('_storyblok=')) { - init(storyRef.current, onStoryInput, resolveRelations); + init(storyRef.current, onStoryInput, token, resolveRelations); } }, []); diff --git a/src/bridge/init.ts b/src/bridge/init.ts index 374b153..f47a7c3 100644 --- a/src/bridge/init.ts +++ b/src/bridge/init.ts @@ -14,11 +14,12 @@ const loadBridge = (callback: () => void) => { export const init = ( story: Story, onStoryInput: (story: Story) => void, + token: string, resolveRelations: string[] = [], ) => { loadBridge(() => { if (window.storyblok) { - window.storyblok.init(); + window.storyblok.init({ accessToken: token }); // Update story on input in Visual Editor // this will alter the state and replaces the current story with a