Skip to content

Commit 0046f2e

Browse files
Add a cmsPreview flag to use alternative CMS dataset (#1135)
I tried using draft documents but that requires authentication. This way if we copy + publish to preview it's easy to see content changes deep in the tree which would otherwise be awkward as we fetch it all in one go.
1 parent cc28c29 commit 0046f2e

File tree

4 files changed

+30
-22
lines changed

4 files changed

+30
-22
lines changed

src/common/imageUrlBuilder.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
*/
66

77
import unconfiguredImageUrlBuilder from "@sanity/image-url";
8+
import { dataset, project } from "./sanity";
89

910
export const defaultQuality = 80;
1011

1112
export const imageUrlBuilder = unconfiguredImageUrlBuilder()
1213
// Hardcoded for now as there's no practical alternative.
13-
.projectId("ajwvhvgo")
14-
.dataset("apps")
14+
.projectId(project)
15+
.dataset(dataset)
1516
.auto("format")
1617
.dpr(window.devicePixelRatio ?? 1)
1718
.quality(defaultQuality);

src/common/sanity.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* Common sanity types.
33
*/
44

5+
import { flags } from "../flags";
6+
57
export interface PortableTextBlock {
68
_type: "block";
79
_key: string;
@@ -85,9 +87,11 @@ export const sanityLanguageId = (locale: string): string => {
8587
return `${parts[0]}-${parts[1].toUpperCase()}`;
8688
};
8789

90+
export const project = "ajwvhvgo";
91+
export const dataset = flags.cmsPreview ? "apps-preview" : "apps";
92+
8893
const queryUrl = (query: string): string => {
89-
return (
90-
"https://ajwvhvgo.apicdn.sanity.io/v1/data/query/apps?query=" +
91-
encodeURIComponent(query)
92-
);
94+
return `https://${project}.apicdn.sanity.io/v1/data/query/${dataset}?query=${encodeURIComponent(
95+
query
96+
)}`;
9397
};

src/documentation/reference/content.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ export const getTopicAndEntry = (
2929
return [entry.parent, entry];
3030
};
3131

32-
// For now we just slurp the whole toolkit at once.
33-
// Might revisit depending on eventual size.
32+
// We just slurp the whole toolkit at once.
33+
// This is necessary for the client-side search index.
3434
const toolkitQuery = (languageId: string): string => {
3535
return `
3636
*[_type == "toolkit" && language == "${languageId}" && (slug.current == "explore" || slug.current == "reference") && !(_id in path("drafts.**"))]{

src/flags.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,32 @@ import { Stage, stage as stageFromEnvironment } from "./environment";
1616
* A union of the flag names (alphabetical order).
1717
*/
1818
export type Flag =
19-
/**
20-
* Enables verbose debug logging to the console of drag events.
21-
*/
22-
| "dndDebug"
23-
2419
/**
2520
* Flag to add a beta notice. Enabled for staging site but not production stages.
2621
*/
2722
| "betaNotice"
28-
2923
/**
30-
* Disables the pop-up welcome dialog.
31-
*
32-
* Added to support user-testing and has the nice side-effect of disabling
33-
* the dialog for local development so is worth keeping for that use alone.
24+
* Uses CMS content from an alternative preview dataset.
25+
*/
26+
| "cmsPreview"
27+
/**
28+
* Enables verbose debug logging to the console of drag events.
3429
*/
35-
| "noWelcome"
30+
| "dndDebug"
3631
/**
3732
* Disables language selection from the settings menu.
3833
*
3934
* Added so we can embed the editor in micro:bit classroom without competing language
4035
* options. The language selected in classroom is passed through via query param.
4136
*/
42-
| "noLang";
37+
| "noLang"
38+
/**
39+
* Disables the pop-up welcome dialog.
40+
*
41+
* Added to support user-testing and has the nice side-effect of disabling
42+
* the dialog for local development so is worth keeping for that use alone.
43+
*/
44+
| "noWelcome";
4345

4446
interface FlagMetadata {
4547
defaultOnStages: Stage[];
@@ -48,10 +50,11 @@ interface FlagMetadata {
4850

4951
const allFlags: FlagMetadata[] = [
5052
// Alphabetical order.
51-
{ name: "dndDebug", defaultOnStages: [] },
5253
{ name: "betaNotice", defaultOnStages: ["local", "REVIEW", "STAGING"] },
53-
{ name: "noWelcome", defaultOnStages: ["local", "REVIEW"] },
54+
{ name: "cmsPreview", defaultOnStages: [] },
55+
{ name: "dndDebug", defaultOnStages: [] },
5456
{ name: "noLang", defaultOnStages: [] },
57+
{ name: "noWelcome", defaultOnStages: ["local", "REVIEW"] },
5558
];
5659

5760
type Flags = Record<Flag, boolean>;

0 commit comments

Comments
 (0)