SR
Click to open github profile
Hooks

useCraftUri

Learn how to use the useCraftUri hook.


This hook 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 = useCraftUri();
Note

To use this hook, 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 = useCraftUri();

// 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 = useCraftUri();

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

Type

export function useCraftUri(): string

Copyright © 2025 Samuel Reichör