SR
Click to open github profile
Functions

getCraftUri

Learn how to use the getCraftUri function.


This function provides an easy way to retrieve the current URI based on the defined siteMap within the libs/query-api.ts. It enhances multisite support and simplifies querying data from your Craft CMS backend.

Usage

const uri = await getCraftUri();
Note

To use this function, you must 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 uri = await getCraftUri();

// uri will result in: "happy-trying"

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 uri = await getCraftUri();

// uri will result in: "happy-trying"

Type

export async function getCraftUri(): Promise<string>

Copyright © 2025 Samuel Reichör