Builder.io SDK for Qwik
Use Qwik SDK to connect your Qwik application to Builder.io.
Installation
Installing the Qwik SDK is done in two steps:
- Set up Qwik-city project.
- Install the Qwik SDK into your Qwik-city project.
Fetch
This Package uses fetch. See these docs for more information.
Non-Node.js Runtimes (Edge, Serverless)
If planning to deploy your app to a non-Node.js runtime (like Edge or Serverless functions), make sure to read the Non-Node.js Runtimes section of the main README.
Set up Qwik-city project
- Follow the instructions on Qwik-city
npm init qwik@latest
Follow instructions for setting up Qwik-city project.
Install the Qwik SDK into your Qwik-city project
Add @builder.io/sdk-qwik
to your package.json
file:
npm add --save @builder.io/sdk-qwik
Add Qwik SDK code to a particular route (such as src/routes/index.tsx
)
import { component$ } from "@builder.io/qwik";
import { loader$ } from "@builder.io/qwik-city";
import {
getBuilderSearchParams,
getContent,
RenderContent,
} from "@builder.io/sdk-qwik";
export const BUILDER_PUBLIC_API_KEY = "YOUR_API_KEY_GOES_HERE"; // ggignore
export const MODEL = 'page';
export const builderContentLoader = loader$(({ url }) => {
return getContent({
model: MODEL,
apiKey: BUILDER_PUBLIC_API_KEY,
options: {
...getBuilderSearchParams(url.searchParams),
cachebust: true,
},
userAttributes: {
urlPath: url.pathname || "/",
},
});
});
export default component$(() => {
const builderContent = builderContentLoader.use();
return (
<div>
<RenderContent
model={MODEL}
content={builderContent.value}
apiKey={BUILDER_PUBLIC_API_KEY}
// Optional: pass in a custom component registry
// customComponents={CUSTOM_COMPONENTS}
/>
</div>
);
});
// OPTIONAL: need to add custom components to your Qwik app
export const MyFunComponent = component$((props: { text: string }) => {
const state = useStore({
count: 0,
});
return (
<div>
<h3>{props.text.toUpperCase()}</h3>
<p>{state.count}</p>
<button onClick$={() => state.count++}>Click me</button>
</div>
);
});
export const CUSTOM_COMPONENTS: RegisteredComponent[] = [
{
component: MyFunComponent,
name: 'MyFunComponent',
inputs: [
{
name: 'text',
type: 'string',
defaultValue: 'Hello world',
},
],
},
];
[Beta] Guide to use API Version v3 to query for content
For using API Version v3
, you need to add apiVersion: 'v3'
to the getContent
function and in the RenderContent
tag. For example:
import { component$ } from "@builder.io/qwik";
import { loader$ } from "@builder.io/qwik-city";
import {
getBuilderSearchParams,
getContent,
RenderContent,
} from "@builder.io/sdk-qwik";
export const BUILDER_PUBLIC_API_KEY = "YOUR_API_KEY_GOES_HERE";
export const MODEL = "page";
export const builderContentLoader = loader$(({ url }) => {
return getContent({
model: MODEL,
apiKey: BUILDER_PUBLIC_API_KEY,
apiVersion: "v3",
options: {
...getBuilderSearchParams(url.searchParams),
cachebust: true,
},
userAttributes: {
urlPath: url.pathname || "/",
},
});
});
export default component$(() => {
const builderContent = builderContentLoader.use();
return (
<div>
<RenderContent
model={MODEL}
content={builderContent.value}
apiKey={BUILDER_PUBLIC_API_KEY}
apiVersion="v3"
/>
</div>
);
});
Reasons to switch to API Version v3
- Better, more scalable infra: Query v3 is built on global scale infrastructure to ensure fast response times and high availability
- Ability to ship more features, faster: Query V3 will allow us to keep shipping the latest features to our customers without breaking fundamental flows. These will be shipped only to Query V3 and not to the older versions of the query API
Coming soon...
- Better support for localization: Some of the newer features of localization and querying based on it will be better supported in Query V3
- Support multi-level nested references: Query V3 will allow you to query, resolve, and return content that has nested references of other contents and symbols.
Mitosis
This SDK is generated by Mitosis. To see the Mitosis source-code, go here.