Add Kentico Kontent super power to your nuxt app
The module makes it easy to do delivery client api calls via the Kentico kontent Delivery JS SDK.
- Install via npm
npm i kentico-kontent-nuxt-module --save
npm i rxjs --save (because this is a peer dependency of the Kentico Kontent Delivery SDK)
- Add
kentico-kontent-nuxt-module
tomodules
section ofnuxt.config.js
/*
** Nuxt.js modules
*/
modules: [
'kentico-kontent-nuxt-module'
],
kenticokontent: {
projectId: 'xxxx-xxx-xxxx-xxxx-xxxxx',
enableAdvancedLogging: false,
previewApiKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxx',
globalQueryConfig: {
usePreviewMode: true, // Queries the Delivery Preview API.
useSecureMode: false,
},
baseUrl: 'https://custom.uri/api/KenticoKontentProxy',
secureApiKey: 'xxx',
enableSecuredMode: true
},
- $deliveryClient is now globally available.
this.$deliveryClient.items()
.type('page')
.toPromise()
.then(response => console.log('DeliveryClient Response', response));
By default Nuxt can only work with promises. Therefor you always use the "toPromise" method provided by the Kentico Kontent Delivery SDK! RxJs operator's are not supported at the moment.
When using a static generated deployment you may need to use the items-feed endpoint when generating your site (because the items endpoint has a rate limitation).
this.$deliveryClient.itemsFeedAll()
.toPromise()
.then(response => console.log('DeliveryClient Response', response));
API calls can be "cached" (they will be stored in memory) client side via the "viaCache" method.
const query = this.$deliveryClient.items().type('page');
const cacheSeconds = 30;
this.$deliveryClient.viaCache(query, cacheSeconds)
.then(response => console.log('DeliveryClient Response', response));
If you need to customize the Kentico Kontent Delivery SDK by registering interceptors and changing global config, you have to create a nuxt plugin.
{
modules: [
'kentico-kontent-nuxt-module',
],
plugins: [
'~/plugins/kenticokontentNuxtModule'
]
}
export default function ({ store, $deliveryClient }) {
$deliveryClient.config.globalHeaders = (queryConfig) => {
let headers = [];
headers.push({header: 'Authorization', value: 'bearer ' + store.state.token });
return headers;
}
}
Type resolvers can also be registered by using a nuxt plugin:
import { TypeResolver, ContentItem } from '@kentico/kontent-delivery';
class Page extends ContentItem {
constructor() {
super({
richTextResolver: (item, context) => {
// todo: implement
},
urlSlugResolver: (link, context) => {
// todo: implement
}
});
}
}
export default function ({ store, app, $deliveryClient }) {
$deliveryClient.config.typeResolvers = [
new TypeResolver('page', () => new Page())
]
}