Skip to content
This repository was archived by the owner on Jun 19, 2025. It is now read-only.

Commit 1a9bfb1

Browse files
feat: content layer feature added
1 parent 8995c49 commit 1a9bfb1

35 files changed

+921
-112
lines changed

lib/content-layer/updateContentLayer.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

lib/index.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import type {
1818
import { initStoryblokBridge } from "./utils/initStoryblokBridge";
1919
import { storyblokLogo } from "./dev-toolbar/toolbarApp";
2020
export { handleStoryblokMessage } from "./live-preview/handleStoryblokMessage";
21+
export { syncContentUpdate } from "./content-layer/syncContentUpdate";
22+
export { storyblokLoader } from "./content-layer/storyblokLoader";
2123

2224
export {
2325
storyblokEditable,
@@ -103,6 +105,10 @@ export type IntegrationOptions = {
103105
* A boolean to enable/disable the Experimental Live Preview feature. Disabled by default.
104106
*/
105107
livePreview?: boolean;
108+
/**
109+
* A boolean to enable/disable storyblok content layer.
110+
*/
111+
contentLayer?: boolean;
106112
};
107113

108114
export default function storyblokIntegration({
@@ -111,6 +117,7 @@ export default function storyblokIntegration({
111117
componentsDir = "src",
112118
enableFallbackComponent = false,
113119
livePreview = false,
120+
contentLayer = false,
114121
...restOptions
115122
}: IntegrationOptions): AstroIntegration {
116123
const resolvedOptions = {
@@ -119,6 +126,7 @@ export default function storyblokIntegration({
119126
componentsDir,
120127
enableFallbackComponent,
121128
livePreview,
129+
contentLayer,
122130
...restOptions,
123131
};
124132

@@ -205,13 +213,66 @@ export default function storyblokIntegration({
205213
order: "pre",
206214
});
207215
}
216+
// This is only enabled if experimentalLivePreview feature is on
217+
if (contentLayer) {
218+
injectScript(
219+
"page",
220+
`
221+
import { loadStoryblokBridge, syncContentUpdate } from "@storyblok/astro";
222+
loadStoryblokBridge().then(() => {
223+
const { StoryblokBridge } = window;
224+
const storyblokInstance = new StoryblokBridge()
225+
storyblokInstance.on(["published", "change", "input"], syncContentUpdate);
226+
});
227+
`
228+
);
229+
}
230+
208231
addDevToolbarApp({
209232
id: "storyblok",
210233
name: "Storyblok",
211234
icon: storyblokLogo,
212235
entrypoint: "@storyblok/astro/toolbarApp.ts",
213236
});
214237
},
238+
...(contentLayer
239+
? {
240+
"astro:server:setup": async ({ server, refreshContent }) => {
241+
// `server` is the Vite dev server instance
242+
server.middlewares.use("/_refresh", async (req, res) => {
243+
if (req.method !== "POST") {
244+
res.writeHead(405, { "Content-Type": "application/json" });
245+
res.end(JSON.stringify({ error: "Method Not Allowed" }));
246+
return;
247+
}
248+
let body = [];
249+
req.on("data", (chunk) => body.push(chunk));
250+
req.on("end", async () => {
251+
try {
252+
const story = JSON.parse(Buffer.concat(body).toString());
253+
await refreshContent?.({
254+
context: { story },
255+
loaders: ["story-loader"],
256+
});
257+
res.writeHead(200, { "Content-Type": "application/json" });
258+
res.end(
259+
JSON.stringify({
260+
message: "Content refreshed successfully",
261+
})
262+
);
263+
} catch (error) {
264+
res.writeHead(500, { "Content-Type": "application/json" });
265+
res.end(
266+
JSON.stringify({
267+
error: `Failed to refresh content: ${error.message}`,
268+
})
269+
);
270+
}
271+
});
272+
});
273+
},
274+
}
275+
: {}),
215276
},
216277
};
217278
}

lib/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"@rollup/plugin-dynamic-import-vars": "^2.1.2",
6161
"@types/lodash.mergewith": "^4.6.9",
6262
"@types/node": "22.4.1",
63-
"astro": "^5.0.0-alpha.8",
63+
"astro": "^5.0.0-alpha.6",
6464
"cypress": "^13.13.3",
6565
"eslint-plugin-cypress": "^3.2.0",
6666
"start-server-and-test": "^2.0.5",

lib/vite-plugins/vite-plugin-storyblok-components.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export function vitePluginStoryblokComponents(
99
components?: object,
1010
enableFallbackComponent?: boolean,
1111
customFallbackComponent?: string
12-
) {
12+
): Plugin {
1313
const virtualModuleId = "virtual:storyblok-components";
1414
const resolvedVirtualModuleId = "\0" + virtualModuleId;
1515

lib/vite-plugins/vite-plugin-storyblok-init.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export function vitePluginStoryblokInit(
55
accessToken: string,
66
useCustomApi: boolean,
77
apiOptions: ISbConfig
8-
) {
8+
): Plugin {
99
const virtualModuleId = "virtual:storyblok-init";
1010
const resolvedVirtualModuleId = "\0" + virtualModuleId;
1111

lib/vite-plugins/vite-plugin-storyblok-options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Plugin } from "vite";
22

3-
export function vitePluginStoryblokOptions(options: object) {
3+
export function vitePluginStoryblokOptions(options: object): Plugin {
44
const virtualModuleId = `virtual:storyblok-options`;
55
const resolvedVirtualModuleId = "\0" + virtualModuleId;
66

0 commit comments

Comments
 (0)