Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nuxt): add useRequestURL helper #20765

Merged
merged 14 commits into from May 13, 2023
Merged
25 changes: 25 additions & 0 deletions docs/3.api/1.composables/use-request-url.md
@@ -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.
10 changes: 2 additions & 8 deletions docs/5.community/5.framework-contribution.md
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions packages/nuxt/src/app/composables/index.ts
Expand Up @@ -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 changes: 14 additions & 0 deletions packages/nuxt/src/app/composables/url.ts
@@ -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)
Atinux marked this conversation as resolved.
Show resolved Hide resolved
}
1 change: 1 addition & 0 deletions packages/nuxt/src/imports/presets.ts
Expand Up @@ -33,6 +33,7 @@ const appPreset = defineUnimportPreset({
'useRequestHeaders',
'useRequestEvent',
'useRequestFetch',
'useRequestURL',
'setResponseStatus',
'setPageLayout',
'onNuxtReady',
Expand Down
7 changes: 7 additions & 0 deletions test/basic.test.ts
Expand Up @@ -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')
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/basic/pages/url.vue
@@ -0,0 +1,9 @@
<script setup>
const url = useRequestURL()
</script>

<template>
<div>
<div>path: {{ url.pathname }}</div>
</div>
</template>