-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
61 lines (53 loc) · 1.97 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { component$ } from "@builder.io/qwik";
import { DocumentHead, routeLoader$ } from "@builder.io/qwik-city";
import {
getContent,
RenderContent,
getBuilderSearchParams,
} from "@builder.io/sdk-qwik";
import { CUSTOM_COMPONENTS } from "../../components/builder-registry";
// This page is a catch-all for all routes that don't have a pre-defined route.
// Using a catch-all route allows you to dynamically create new pages in Builder.
// Use the `useBuilderContent` route loader to get your content from Builder.
// `routeLoader$()` takes an async function to fetch content
// from Builder with using `getContent()`.
export const useBuilderContent = routeLoader$(async ({ url, error }) => {
const isPreviewing = url.searchParams.has("builder.preview");
// Fetch Builder.io Visual CMS content using the Qwik SDK.
// The public API key is set in the .env file at the root
// https://www.builder.io/c/docs/using-your-api-key
const builderContent = await getContent({
model: "page",
apiKey: import.meta.env.PUBLIC_BUILDER_API_KEY,
options: getBuilderSearchParams(url.searchParams),
userAttributes: {
urlPath: url.pathname,
},
});
// If there's no content, throw a 404.
// You can use your own 404 component here
if (!builderContent && !isPreviewing) {
throw error(404, "Page not found");
}
// return content fetched from Builder, which is JSON
return builderContent;
});
export default component$(() => {
const builderContent = useBuilderContent();
// RenderContent component uses the `content` prop to render
// the page, specified by the API Key, at the current URL path.
return (
<RenderContent
model="page"
content={builderContent.value}
apiKey={import.meta.env.PUBLIC_BUILDER_API_KEY}
customComponents={CUSTOM_COMPONENTS}
/>
);
});
export const head: DocumentHead = ({ resolveValue }) => {
const builderContent = resolveValue(useBuilderContent);
return {
title: builderContent?.data?.title,
};
};