Skip to content

Commit 53bc346

Browse files
committed
new odr strategy
1 parent c201dd0 commit 53bc346

File tree

8 files changed

+43
-57
lines changed

8 files changed

+43
-57
lines changed

packages/basehub/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# basehub
22

3+
## 8.1.21
4+
5+
### Patch Changes
6+
7+
- new odr strategy
8+
39
## 8.1.20
410

511
### Patch Changes

packages/basehub/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "basehub",
33
"description": "A very fast Headless CMS.",
44
"author": "JB <[email protected]>",
5-
"version": "8.1.20",
5+
"version": "8.1.21",
66
"license": "MIT",
77
"repository": "basehub-ai/basehub",
88
"bugs": "https://github.com/basehub-ai/basehub/issues",

packages/basehub/src/bin/util/get-stuff-from-env.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { ResolvedRef } from "../../common-types";
99

1010
export const basehubAPIOrigin = "https://api.basehub.com";
1111
const defaultEnvVarPrefix = "BASEHUB";
12-
const DEFAULT_API_VERSION = "3";
12+
const DEFAULT_API_VERSION = "4";
1313

1414
export type Options = {
1515
forceDraft?: boolean;

packages/basehub/src/next/toolbar/client-conditional-renderer.tsx

+12-14
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,20 @@ export const ClientConditionalRenderer = ({
1717
revalidateTags,
1818
resolvedRef,
1919
getLatestBranches,
20-
humanRevalidatePendingTags,
2120
}: {
2221
draft: boolean;
2322
isForcedDraft: boolean;
2423
enableDraftMode: (o: {
2524
bshbPreviewToken: string;
2625
}) => Promise<{ status: number; response: object }>;
2726
disableDraftMode: () => Promise<void>;
28-
revalidateTags: (o: { tags: string[] }) => Promise<{ success: boolean }>;
27+
revalidateTags: (o: {
28+
bshbPreviewToken: string;
29+
}) => Promise<{ success: boolean }>;
2930
getLatestBranches: (o: { bshbPreviewToken: string | undefined }) => Promise<{
3031
status: number;
3132
response: LatestBranch[] | { error: string };
3233
}>;
33-
humanRevalidatePendingTags: (o: {
34-
bshbPreviewToken: string;
35-
ref: string;
36-
}) => Promise<{ success: boolean }>;
3734
resolvedRef: ResolvedRef;
3835
}) => {
3936
const [hasRendered, setHasRendered] = React.useState(false);
@@ -87,17 +84,19 @@ export const ClientConditionalRenderer = ({
8784

8885
React.useEffect(() => {
8986
const url = new URL(window.location.href);
90-
const tags = url.searchParams.get("bshb-odr-tags");
91-
if (tags) {
92-
url.searchParams.delete("bshb-odr-tags");
93-
window.history.replaceState(null, "", url);
94-
revalidateTags({ tags: tags.split(",") })
87+
const shouldRevalidate = url.searchParams.get("__bshb-odr") === "true";
88+
const odrToken = url.searchParams.get("__bshb-odr-token");
89+
90+
if (shouldRevalidate && odrToken) {
91+
revalidateTags({ bshbPreviewToken: odrToken })
9592
.then(({ success }) => {
9693
document.documentElement.dataset.basehubOdrStatus = success
9794
? "success"
9895
: "error";
99-
document.documentElement.dataset.basehubOdrErrorMessage =
100-
"Response failed";
96+
if (!success) {
97+
document.documentElement.dataset.basehubOdrErrorMessage =
98+
"Response failed";
99+
}
101100
})
102101
.catch((e) => {
103102
document.documentElement.dataset.basehubOdrStatus = "error";
@@ -132,7 +131,6 @@ export const ClientConditionalRenderer = ({
132131
seekAndStoreBshbPreviewToken={seekAndStoreBshbPreviewToken}
133132
resolvedRef={resolvedRef}
134133
getLatestBranches={getLatestBranches}
135-
humanRevalidatePendingTags={humanRevalidatePendingTags}
136134
bshbPreviewLSName={bshbPreviewLSName}
137135
/>,
138136
document.body

packages/basehub/src/next/toolbar/client-toolbar.tsx

-20
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ export const ClientToolbar = ({
4242
status: number;
4343
response: LatestBranch[] | { error: string };
4444
}>;
45-
humanRevalidatePendingTags: (o: {
46-
bshbPreviewToken: string;
47-
ref: string;
48-
}) => Promise<{ success: boolean }>;
4945
}) => {
5046
const [toolbarRef, setToolbarRef] = React.useState<HTMLDivElement | null>(
5147
null
@@ -232,22 +228,6 @@ export const ClientToolbar = ({
232228
setRefWithEvents,
233229
]);
234230

235-
// // human revalidate pending tags
236-
// const lastHumanRevalidatedRef = React.useRef<string | null>(null);
237-
// React.useEffect(() => {
238-
// if (!bshbPreviewToken) return;
239-
// if (!previewRef) return;
240-
// if (isForcedDraft) return;
241-
// if (lastHumanRevalidatedRef.current === previewRef) return;
242-
// lastHumanRevalidatedRef.current = previewRef;
243-
244-
// humanRevalidatePendingTags({ bshbPreviewToken, ref: previewRef }).catch(
245-
// () => {
246-
// // ignore
247-
// }
248-
// );
249-
// }, [bshbPreviewToken, humanRevalidatePendingTags, previewRef, isForcedDraft]);
250-
251231
/** Position tooltip when message changes. */
252232
React.useLayoutEffect(() => {
253233
tooltipRef.current?.checkOverflow();

packages/basehub/src/next/toolbar/server-toolbar.tsx

+15-20
Original file line numberDiff line numberDiff line change
@@ -110,28 +110,17 @@ export const ServerToolbar = async ({
110110
(await draftMode()).disable();
111111
};
112112

113-
const revalidateTags = async ({ tags }: { tags: string[] }) => {
114-
"use server";
115-
await Promise.all(
116-
tags.map(async (tag) => {
117-
if (tag.startsWith("basehub-")) {
118-
await revalidateTag(tag);
119-
}
120-
})
121-
);
122-
return { success: true };
123-
};
124-
125-
// used by a client that's authenticated to the toolbar
126-
// sort of as a fallback to the headless browser's revalidation failing
127-
const humanRevalidatePendingTags = async ({
113+
const revalidateTags = async ({
128114
bshbPreviewToken,
129-
ref,
130115
}: {
131116
bshbPreviewToken: string;
132-
ref: string;
133117
}) => {
134118
"use server";
119+
120+
if (!bshbPreviewToken) {
121+
return { success: false, error: "Unauthorized" };
122+
}
123+
135124
const { headers, url } = getStuffFromEnv(basehubProps);
136125
const appApiEndpoint = getBaseHubAppApiEndpoint(
137126
url,
@@ -145,7 +134,6 @@ export const ServerToolbar = async ({
145134
"content-type": "application/json",
146135
"x-basehub-token": headers["x-basehub-token"],
147136
"x-basehub-preview-token": bshbPreviewToken,
148-
"x-basehub-ref": ref,
149137
},
150138
});
151139

@@ -159,7 +147,15 @@ export const ServerToolbar = async ({
159147
return { success: false };
160148
}
161149

162-
return await revalidateTags({ tags });
150+
await Promise.all(
151+
tags.map(async (tag) => {
152+
if (tag.startsWith("basehub-")) {
153+
await revalidateTag(tag);
154+
}
155+
})
156+
);
157+
158+
return { success: true };
163159
};
164160

165161
return (
@@ -171,7 +167,6 @@ export const ServerToolbar = async ({
171167
revalidateTags={revalidateTags}
172168
getLatestBranches={getLatestBranches}
173169
resolvedRef={resolvedRef}
174-
humanRevalidatePendingTags={humanRevalidatePendingTags}
175170
/>
176171
);
177172
};

playground/CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# playground
22

3+
## 0.0.190
4+
5+
### Patch Changes
6+
7+
- Updated dependencies
8+
9+
310
## 0.0.189
411

512
### Patch Changes

playground/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "playground",
33
"private": true,
4-
"version": "0.0.189",
4+
"version": "0.0.190",
55
"scripts": {
66
"dev": "basehub dev & next dev --port 3003",
77
"build": "basehub && next build",

0 commit comments

Comments
 (0)