From 464600cd20a075a9eefac0c6e169b822257500f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Wed, 10 May 2023 18:40:11 +0200 Subject: [PATCH 01/12] feat(composables): add getRequestURL() --- docs/5.community/5.framework-contribution.md | 8 +------- packages/nuxt/src/app/composables/index.ts | 1 + packages/nuxt/src/app/composables/url.ts | 9 +++++++++ packages/nuxt/src/imports/presets.ts | 1 + test/basic.test.ts | 4 ++++ test/fixtures/basic/pages/url.vue | 9 +++++++++ 6 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 packages/nuxt/src/app/composables/url.ts create mode 100644 test/fixtures/basic/pages/url.vue diff --git a/docs/5.community/5.framework-contribution.md b/docs/5.community/5.framework-contribution.md index 44872e9e02d7..a936dadcc704 100644 --- a/docs/5.community/5.framework-contribution.md +++ b/docs/5.community/5.framework-contribution.md @@ -65,7 +65,7 @@ You can modify the example app in `playground/`, and run it with `yarn dev`. Ple 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` 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. diff --git a/packages/nuxt/src/app/composables/index.ts b/packages/nuxt/src/app/composables/index.ts index e40cb76ce612..ae0c17b4fed7 100644 --- a/packages/nuxt/src/app/composables/index.ts +++ b/packages/nuxt/src/app/composables/index.ts @@ -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' diff --git a/packages/nuxt/src/app/composables/url.ts b/packages/nuxt/src/app/composables/url.ts new file mode 100644 index 000000000000..1d6f6dead80c --- /dev/null +++ b/packages/nuxt/src/app/composables/url.ts @@ -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) +} diff --git a/packages/nuxt/src/imports/presets.ts b/packages/nuxt/src/imports/presets.ts index 52c124782187..fdd1204182e5 100644 --- a/packages/nuxt/src/imports/presets.ts +++ b/packages/nuxt/src/imports/presets.ts @@ -33,6 +33,7 @@ const appPreset = defineUnimportPreset({ 'useRequestHeaders', 'useRequestEvent', 'useRequestFetch', + 'getRequestURL', 'setResponseStatus', 'setPageLayout', 'onNuxtReady', diff --git a/test/basic.test.ts b/test/basic.test.ts index af4a2f0fc6bd..f7bbff17910c 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -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', () => { diff --git a/test/fixtures/basic/pages/url.vue b/test/fixtures/basic/pages/url.vue new file mode 100644 index 000000000000..9861da2d7873 --- /dev/null +++ b/test/fixtures/basic/pages/url.vue @@ -0,0 +1,9 @@ + + + From 50f43b92b09871b9c155c7e15a64fbbb7ba74c0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Wed, 10 May 2023 18:41:00 +0200 Subject: [PATCH 02/12] Update 5.framework-contribution.md --- docs/5.community/5.framework-contribution.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/5.community/5.framework-contribution.md b/docs/5.community/5.framework-contribution.md index a936dadcc704..ad85357b18f8 100644 --- a/docs/5.community/5.framework-contribution.md +++ b/docs/5.community/5.framework-contribution.md @@ -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. ## 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 `pnpm 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 From 582a255b509e417c27125810d236b555669dda23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Wed, 10 May 2023 18:44:00 +0200 Subject: [PATCH 03/12] Update docs/5.community/5.framework-contribution.md Co-authored-by: Daniel Roe --- docs/5.community/5.framework-contribution.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/5.community/5.framework-contribution.md b/docs/5.community/5.framework-contribution.md index ad85357b18f8..a15fb759b1b7 100644 --- a/docs/5.community/5.framework-contribution.md +++ b/docs/5.community/5.framework-contribution.md @@ -59,7 +59,7 @@ 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 `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. +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 From 5af5fee7a762dd1d0dbc9e9ce20f4f4671e99d3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Wed, 10 May 2023 18:55:09 +0200 Subject: [PATCH 04/12] docs: add get-request-url --- docs/3.api/3.utils/get-request-url.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 docs/3.api/3.utils/get-request-url.md diff --git a/docs/3.api/3.utils/get-request-url.md b/docs/3.api/3.utils/get-request-url.md new file mode 100644 index 000000000000..248f6e573b29 --- /dev/null +++ b/docs/3.api/3.utils/get-request-url.md @@ -0,0 +1,21 @@ +# getRequestURL + + +`getRequestURL` is a helper function that return an [URL object](https://developer.mozilla.org/en-US/docs/Web/API/URL/URL) working on both server-side and client-side rendering. + +::code-group +```vue [pages/about.vue] + + + +``` +```html [result] +

Path is: /about

+``` +:: + +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. \ No newline at end of file From f3dc96d0d6277e38a6290c12c001000a87267c5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Wed, 10 May 2023 18:55:40 +0200 Subject: [PATCH 05/12] fix test --- test/basic.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/basic.test.ts b/test/basic.test.ts index f7bbff17910c..5eb0a50125dd 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -840,7 +840,7 @@ describe('extends support', () => { }) it('has getRequestURL()', async () => { const html = await $fetch('/url') - expect(html).toContain('path: /test') + expect(html).toContain('path: /url') }) }) From 4288b8bded3672e796a74306a58d7db9946f9b83 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Wed, 10 May 2023 16:57:15 +0000 Subject: [PATCH 06/12] [autofix.ci] apply automated fixes --- packages/nuxt/src/app/composables/url.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/nuxt/src/app/composables/url.ts b/packages/nuxt/src/app/composables/url.ts index 1d6f6dead80c..db7432e811fd 100644 --- a/packages/nuxt/src/app/composables/url.ts +++ b/packages/nuxt/src/app/composables/url.ts @@ -1,7 +1,7 @@ import { getRequestURL as getServerRequestURL } from 'h3' import { useRequestEvent } from './ssr' -export function getRequestURL() { +export function getRequestURL () { if (process.server) { return getServerRequestURL(useRequestEvent()) } From 828447147f0e0085a383cabadcfecf34c04e86cc Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Wed, 10 May 2023 16:58:08 +0000 Subject: [PATCH 07/12] [autofix.ci] apply automated fixes (attempt 2/3) --- docs/3.api/3.utils/get-request-url.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/3.api/3.utils/get-request-url.md b/docs/3.api/3.utils/get-request-url.md index 248f6e573b29..2291096834eb 100644 --- a/docs/3.api/3.utils/get-request-url.md +++ b/docs/3.api/3.utils/get-request-url.md @@ -1,9 +1,9 @@ # getRequestURL - `getRequestURL` is a helper function that return an [URL object](https://developer.mozilla.org/en-US/docs/Web/API/URL/URL) working on both server-side and client-side rendering. ::code-group + ```vue [pages/about.vue]