Skip to content

Commit

Permalink
v
Browse files Browse the repository at this point in the history
  • Loading branch information
julianbenegas committed Nov 15, 2024
1 parent 250f1b2 commit 5bdf818
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 2 deletions.
6 changes: 6 additions & 0 deletions packages/basehub/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# basehub

## 7.5.26

### Patch Changes

- Add manual revalidation for pending tags

## 7.5.25

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/basehub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "basehub",
"description": "A very fast Headless CMS.",
"author": "JB <[email protected]>",
"version": "7.5.25",
"version": "7.5.26",
"license": "MIT",
"repository": "basehub-ai/basehub",
"bugs": "https://github.com/basehub-ai/basehub/issues",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const ClientConditionalRenderer = ({
revalidateTags,
resolvedRef,
getLatestBranches,
humanRevalidatePendingTags,
}: {
draft: boolean;
isForcedDraft: boolean;
Expand All @@ -29,6 +30,10 @@ export const ClientConditionalRenderer = ({
status: number;
response: LatestBranch[] | { error: string };
}>;
humanRevalidatePendingTags: (o: {
bshbPreviewToken: string;
ref: string;
}) => Promise<{ success: boolean }>;
resolvedRef: ResolvedRef;
}) => {
const [hasRendered, setHasRendered] = React.useState(false);
Expand Down Expand Up @@ -111,6 +116,7 @@ export const ClientConditionalRenderer = ({
seekAndStoreBshbPreviewToken={seekAndStoreBshbPreviewToken}
resolvedRef={resolvedRef}
getLatestBranches={getLatestBranches}
humanRevalidatePendingTags={humanRevalidatePendingTags}
/>,
document.body
);
Expand Down
16 changes: 16 additions & 0 deletions packages/basehub/src/next/toolbar/client-toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const ClientToolbar = ({
seekAndStoreBshbPreviewToken,
resolvedRef,
getLatestBranches,
humanRevalidatePendingTags,
}: {
draft: boolean;
isForcedDraft: boolean;
Expand All @@ -40,6 +41,10 @@ export const ClientToolbar = ({
status: number;
response: LatestBranch[] | { error: string };
}>;
humanRevalidatePendingTags: (o: {
bshbPreviewToken: string;
ref: string;
}) => Promise<{ success: boolean }>;
}) => {
const [toolbarRef, setToolbarRef] = React.useState<HTMLDivElement | null>(
null
Expand Down Expand Up @@ -162,6 +167,17 @@ export const ClientToolbar = ({
window.dispatchEvent(new Event("__bshb_ref_changed"));
}, []);

// human revalidate pending tags
React.useEffect(() => {
if (!bshbPreviewToken) return;
if (!ref) return;
if (isForcedDraft) return;

humanRevalidatePendingTags({ bshbPreviewToken, ref }).catch(() => {
// ignore
});
}, [bshbPreviewToken, humanRevalidatePendingTags, ref, isForcedDraft]);

React.useLayoutEffect(() => {
tooltipRef.current?.checkOverflow();
}, [message]);
Expand Down
41 changes: 41 additions & 0 deletions packages/basehub/src/next/toolbar/server-toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,46 @@ export const ServerToolbar = async ({
return { success: true };
};

// used by a client that's authenticated to the toolbar
// sort of as a fallback to the headless browser's revalidation failing
const humanRevalidatePendingTags = async ({
bshbPreviewToken,
ref,
}: {
bshbPreviewToken: string;
ref: string;
}) => {
"use server";
const { headers, url } = getStuffFromEnv(basehubProps);
const appApiEndpoint = getBaseHubAppApiEndpoint(
url,
"/api/nextjs/pending-tags"
);

const res = await fetch(appApiEndpoint, {
cache: "no-store",
method: "GET",
headers: {
"content-type": "application/json",
"x-basehub-token": headers["x-basehub-token"],
"x-basehub-preview-token": bshbPreviewToken,
"x-basehub-ref": ref,
},
});

if (res.status !== 200) {
return { success: false };
}

const response = await res.json();
const tags = response.tags;
if (!tags || !Array.isArray(tags)) {
return { success: false };
}

return await revalidateTags({ tags });
};

return (
<LazyClientConditionalRenderer
draft={(await draftMode()).isEnabled}
Expand All @@ -119,6 +159,7 @@ export const ServerToolbar = async ({
revalidateTags={revalidateTags}
getLatestBranches={getLatestBranches}
resolvedRef={resolvedRef}
humanRevalidatePendingTags={humanRevalidatePendingTags}
/>
);
};
Expand Down
7 changes: 7 additions & 0 deletions playground/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# playground

## 0.0.168

### Patch Changes

- Updated dependencies
- [email protected]

## 0.0.167

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion playground/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "playground",
"private": true,
"version": "0.0.167",
"version": "0.0.168",
"scripts": {
"dev": "basehub dev & next dev --port 3003",
"build": "basehub && next build",
Expand Down

0 comments on commit 5bdf818

Please sign in to comment.