mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 00:23:53 +00:00
feat(nuxt): add useRequestURL
helper (#20765)
This commit is contained in:
parent
7f0fe2fbe7
commit
6a052b583b
25
docs/3.api/1.composables/use-request-url.md
Normal file
25
docs/3.api/1.composables/use-request-url.md
Normal file
@ -0,0 +1,25 @@
|
||||
# useRequestURL
|
||||
|
||||
`useRequestURL` is a helper function that returns an [URL object](https://developer.mozilla.org/en-US/docs/Web/API/URL/URL) working on both server-side and client-side.
|
||||
|
||||
::code-group
|
||||
|
||||
```vue [pages/about.vue]
|
||||
<script setup>
|
||||
const url = getRequestURL()
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<p>URL is: {{ url }}</p>
|
||||
<p>Path is: {{ url.pathname }}</p>
|
||||
</template>
|
||||
```
|
||||
|
||||
```html [Result in development]
|
||||
<p>URL is: http://localhost:3000/about</p>
|
||||
<p>Path is: /about</p>
|
||||
```
|
||||
|
||||
::
|
||||
|
||||
You can find the list of the [URL instance properties](https://developer.mozilla.org/en-US/docs/Web/API/URL#instance_properties) on the MDN documentation.
|
@ -59,13 +59,13 @@ Once you've read the [general contribution guide](/docs/community/contribution),
|
||||
|
||||
While working on a PR, you will likely want to check if your changes are working correctly.
|
||||
|
||||
You can modify the example app in `playground/`, and run it with `yarn dev`. Please make sure not to commit it to your branch, but it could be helpful to add some example code to your PR description. This can help reviewers and other Nuxt users understand the feature you've built in-depth.
|
||||
You can modify the example app in `playground/`, and run it with `pnpm dev`. Please make sure not to commit it to your branch, but it could be helpful to add some example code to your PR description. This can help reviewers and other Nuxt users understand the feature you've built in-depth.
|
||||
|
||||
## Testing
|
||||
|
||||
Every new feature should have a corresponding unit test (if possible). The `test` folder in this repository is currently a work in progress, but do your best to create a new test following the example of what's already there.
|
||||
|
||||
Before creating a PR or marking it as ready-to-review, ensure that all tests pass by running `yarn test` locally.
|
||||
Before creating a PR or marking it as ready-to-review, ensure that all tests pass by running `pnpm test:fixtures` locally.
|
||||
|
||||
## Linting
|
||||
|
||||
@ -126,12 +126,6 @@ To contribute to Nuxt, you need to set up a local environment.
|
||||
git checkout -b my-new-branch
|
||||
```
|
||||
|
||||
::js-doc{file=packages/nuxt/src/test.js function=useState}
|
||||
::
|
||||
|
||||
::doc-link{file=packages/nuxt/src/test.js function=useState}
|
||||
::
|
||||
|
||||
### Set Up Documentation Website in Local Environment
|
||||
|
||||
The Nuxt documentation is currently deployed within [nuxt/nuxt.com](https://github.com/nuxt/nuxt.com) as a layer.
|
||||
|
@ -31,3 +31,4 @@ export { preloadComponents, prefetchComponents, preloadRouteComponents } from '.
|
||||
export { isPrerendered, loadPayload, preloadPayload, definePayloadReducer, definePayloadReviver } from './payload'
|
||||
export type { ReloadNuxtAppOptions } from './chunk'
|
||||
export { reloadNuxtApp } from './chunk'
|
||||
export { useRequestURL } from './url'
|
||||
|
14
packages/nuxt/src/app/composables/url.ts
Normal file
14
packages/nuxt/src/app/composables/url.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import { getRequestURL } from 'h3'
|
||||
import { joinURL } from 'ufo'
|
||||
import { useRequestEvent } from './ssr'
|
||||
import { useRuntimeConfig } from '#app'
|
||||
|
||||
export function useRequestURL () {
|
||||
if (process.server) {
|
||||
const { baseURL } = useRuntimeConfig().app
|
||||
const url = getRequestURL(useRequestEvent())
|
||||
url.pathname = joinURL(baseURL, url.pathname)
|
||||
return url
|
||||
}
|
||||
return new URL(window.location.href)
|
||||
}
|
@ -33,6 +33,7 @@ const appPreset = defineUnimportPreset({
|
||||
'useRequestHeaders',
|
||||
'useRequestEvent',
|
||||
'useRequestFetch',
|
||||
'useRequestURL',
|
||||
'setResponseStatus',
|
||||
'setPageLayout',
|
||||
'onNuxtReady',
|
||||
|
@ -372,6 +372,13 @@ describe('pages', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('nuxt composables', () => {
|
||||
it('has useRequestURL()', async () => {
|
||||
const html = await $fetch('/url')
|
||||
expect(html).toContain('path: /url')
|
||||
})
|
||||
})
|
||||
|
||||
describe('rich payloads', () => {
|
||||
it('correctly serializes and revivifies complex types', async () => {
|
||||
const html = await $fetch('/json-payload')
|
||||
|
9
test/fixtures/basic/pages/url.vue
vendored
Normal file
9
test/fixtures/basic/pages/url.vue
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
<script setup>
|
||||
const url = useRequestURL()
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<div>path: {{ url.pathname }}</div>
|
||||
</div>
|
||||
</template>
|
Loading…
Reference in New Issue
Block a user