Skip to content

Commit

Permalink
Merge pull request #861 from storyblok/next
Browse files Browse the repository at this point in the history
fix: live preview bug fix
  • Loading branch information
dipankarmaikap authored Jul 11, 2024
2 parents 61db77f + 479a6b6 commit ba60d60
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
19 changes: 16 additions & 3 deletions lib/live-preview/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,22 @@ import { defineMiddleware } from "astro/middleware";

export const onRequest = defineMiddleware(async ({ locals, request }, next) => {
if (request["method"] === "POST") {
const requestBody = await request.json();
if (requestBody && requestBody["is_storyblok_preview"]) {
locals["_storyblok_preview_data"] = requestBody;
const url = new URL(request.url);
//First do a basic check if its coming from within storyblok
const isStoryblokRequest =
url.searchParams.has("_storyblok") &&
url.searchParams.has("_storyblok_c");

if (isStoryblokRequest) {
try {
//Create a copy of the request
const requestBody = await request.clone().json();
if (requestBody && requestBody["is_storyblok_preview"]) {
locals["_storyblok_preview_data"] = requestBody;
}
} catch (error) {
console.error("Error reading request body:", error);
}
}
}
return next();
Expand Down
5 changes: 5 additions & 0 deletions playground-ssr/.astro/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"_variables": {
"lastUpdateCheck": 1720535052198
}
}
16 changes: 16 additions & 0 deletions playground-ssr/src/pages/api/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { APIRoute } from "astro";

export const GET: APIRoute = ({ request }) => {
return new Response(
JSON.stringify({
path: new URL(request.url).pathname,
})
);
};
export const POST: APIRoute = async ({ request }) => {
return new Response(
JSON.stringify({
body: await request.json(),
})
);
};
5 changes: 5 additions & 0 deletions playground/.astro/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"_variables": {
"lastUpdateCheck": 1720535044084
}
}

0 comments on commit ba60d60

Please sign in to comment.