useRequestURL v3.5
Access the incoming request URL with the useRequestURL composable.
useRequestURL is a helper function that returns an URL object working on both server-side and client-side.
When utilizing Hybrid Rendering with cache strategies, all incoming request headers are dropped when handling the cached responses via the Nitro caching layer (meaning
useRequestURL will return localhost for the host).You can define the cache.varies option to specify headers that will be considered when caching and serving the responses, such as host and x-forwarded-host for multi-tenant environments.If you have set
app.baseURL, the path returned on the server is relative to it, so /base/about is served as /about. On the client, useRequestURL reads window.location, which includes the base URL.<script setup lang="ts">
const url = useRequestURL()
</script>
<template>
<p>URL is: {{ url }}</p>
<p>Path is: {{ url.pathname }}</p>
</template>
<p>URL is: http://localhost:3000/about</p>
<p>Path is: /about</p>