我正在开发一个JAMstack网站,使用Next.js (SSG)和Storyblok (无头CMS),并使用Storyblok内容交付API从Storyblok获取内容。
我已经创建了Storyblok服务,它具有Storyblok配置和Bridge功能。
StoryblokService.js
import StoryblokClient from 'storyblok-js-client'
class StoryblokService {
constructor() {
this.devMode = false // Always loads draft
this.token = '#########'
this.client = new StoryblokClient({
accessToken: this.token,
cache: {
clear: 'auto',
type: 'memory'
}
})
this.query = {}
}
getCacheVersion() {
return this.client.cacheVersion
}
get(slug, params) {
params = params || {}
if (this.getQuery('_storyblok') || this.devMode || (typeof window !== 'undefined' && window.storyblok)) {
params.version = 'draft'
}
if (typeof window !== 'undefined' && typeof window.StoryblokCacheVersion !== 'undefined') {
params.cv = window.StoryblokCacheVersion
}
return this.client.get(slug, params)
}
initEditor(reactComponent) {
if (window.storyblok) {
window.storyblok.init()
window.storyblok.on(['change', 'published'], () => location.reload(true))
// this will alter the state and replaces the current story with a current raw story object (no resolved relations or links)
window.storyblok.on('input', (event) => {
if (event.story.content._uid === reactComponent.state.story.content._uid) {
reactComponent.setState({
story: {
...event.story,
content: window.storyblok.addComments(event.story.content, event.story.id)
}
})
}
})
}
}
setQuery(query) {
this.query = query
}
getQuery(param) {
return this.query[param]
}
bridge() {
if (!this.getQuery('_storyblok') && !this.devMode) {
return ''
}
return (<script src={'//app.storyblok.com/f/storyblok-latest.js?t=' + this.token}></script>)
}
}
const storyblokInstance = new StoryblokService()
export default storyblokInstance我正在调用桥函数,并在layout.js中获取布局内容,这是从app.js调用的。
app.js
import React, { useState } from 'react';
import StoryblokService from '../utils/storyblok-service';
function MyApp({ Component, pageProps, layoutContent }) {
return (
<Layout navColor={appendHeaderColor} title={`Test title`} description={`Test description`} content={layoutContent}>
);
}
MyApp.getInitialProps = async (query) => {
StoryblokService.setQuery(query)
const res = await StoryblokService.get('cdn/stories/layout')
const layoutContent = res.data.story.content
return {
layoutContent
}
}
export default MyApp;Storyblok桥在layout.js被这样称呼
layout.js
{StoryblokService.bridge()}问题陈述
使用Storyblok进行实时内容更新,即如果我更改Storyblok中的某些内容并将其发布,那么当我重新加载页面时,内容也会在Next.js /本地应用程序中得到更新,但在生产环境中或将Next.js代码发布到Vercel中也是一样的。
似乎配置是错误的,或者可能与Storyblok、webhooks或工作流有关。
任何帮助都将不胜感激。提前感谢!
发布于 2022-10-26 08:17:44
您应该检查get URL是否正确。
https://stackoverflow.com/questions/66656520
复制相似问题