Functions
getCraftCurrentSite
Learn how to use the getCraftCurrentSite function.
This function offers an easy way to retrieve the current site based on the siteMap
defined in libs/query-api.ts
. It facilitates multisite support and simplifies querying data from your Craft CMS backend.
Usage
const currentSite = await getCraftCurrentSite();
Note
To use this function, you have to define your sites in the siteMap
property in the libs/query-api.ts
.
By default it returns the current site based on the path
defined in the siteMap property in your libs/query-api.ts
. You can change this behavior by changing the
siteDetectionMode
to origin
. You can read more about that in the configuration docs.
siteDetectionMode: path
// Url: http://localhost:3000/de/happy-trying
//
// craftInit({
// siteDetectionMode: 'path',
// siteMap: [
// {
// handle: 'en',
// origin: 'http://localhost:3000',
// path: '/',
// },
// {
// handle: 'de',
// origin: 'http://localhost:3000',
// path: '/de',
// }
// ],
// })
const currentSite = await getCraftCurrentSite();
// currentSite will be:
// {
// handle: 'de',
// origin: 'http://localhost:3000',
// path: '/de',
// }
siteDetectionMode: origin
// Url: https://en-site.ddev.site/happy-trying
//
// /libs/query-api.ts
// craftInit({
// siteDetectionMode: 'origin',
// siteMap: [
// {
// handle: 'en',
// origin: 'https://en-site.ddev.site',
// path: '/',
// },
// {
// handle: 'de',
// origin: 'https://de-site.ddev.site',
// path: '/',
// }
// ],
// })
const currentSite = await getCraftCurrentSite();
// currentSite will be:
// {
// handle: 'en',
// origin: 'https://en-site.ddev.site',
// path: '/',
// }
No site found
If no current site is available, it will return the first site defined in the siteMap
Array.
Type
export async function getCraftCurrentSite(): Promise<CraftSite>;
type CraftSite = {
handle: string;
origin: string;
path: string;
id?: number;
label?: string;
lang?: string;
primary?: boolean;
};