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
10 changes: 2 additions & 8 deletions docs/5.community/5.framework-contribution.md
Original file line number Diff line number Diff line change
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 `npm run 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.
Atinux marked this conversation as resolved.
Show resolved Hide resolved

## 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
Original file line number Diff line number Diff line change
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 { getRequestURL } from './url'
9 changes: 9 additions & 0 deletions packages/nuxt/src/app/composables/url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { getRequestURL as getServerRequestURL } from 'h3'
import { useRequestEvent } from './ssr'

export function getRequestURL() {
if (process.server) {
return getServerRequestURL(useRequestEvent())
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const appPreset = defineUnimportPreset({
'useRequestHeaders',
'useRequestEvent',
'useRequestFetch',
'getRequestURL',
'setResponseStatus',
'setPageLayout',
'onNuxtReady',
Expand Down
4 changes: 4 additions & 0 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,10 @@ describe('extends support', () => {
const html = await $fetch('/extends')
expect(html).toContain('test from project')
})
it('has getRequestURL()', async () => {
const html = await $fetch('/url')
expect(html).toContain('path: /test')
})
})

describe('plugins', () => {
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/basic/pages/url.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script setup>
const url = getRequestURL()
</script>

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