From 92666e4853eb21770c4cf07d732fcc8d65c61fff Mon Sep 17 00:00:00 2001 From: "Fred K. Schott" Date: Thu, 21 Jul 2022 22:05:29 -0700 Subject: [PATCH] deprecate Astro.canonicalURL & add Astro.url (#1033) * deprecate Astro.canonicalURL, add Astro.url * respond to chris and sarah reviews * fix bad link --- src/pages/en/migrate.md | 11 +++++++ src/pages/en/reference/api-reference.md | 40 ++++++++++++++++++------- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/pages/en/migrate.md b/src/pages/en/migrate.md index 6bd8331d0ede6..0ebc517043c81 100644 --- a/src/pages/en/migrate.md +++ b/src/pages/en/migrate.md @@ -25,6 +25,17 @@ npm install astro@next--rc Astro v1.0 RC has upgraded from Vite 2 to [Vite 3](https://vitejs.dev/). We've handled most of the upgrade for you inside of Astro, however some subtle Vite behaviors may still change between versions. Refer to the official [Vite Migration Guide](https://vitejs.dev/guide/migration.html#general-changes) if you run into trouble. +### Deprecated: `Astro.canonicalURL` + +You can now use the new [`Astro.url`](/en/reference/api-reference/#astrourl) helper to construct your own canonical URL from the current page/request URL. + +```js +// Before: +const canonicalURL = Astro.canonicalURL; +// After: +const canonicalURL = new URL(Astro.url.pathname, Astro.site); +``` + ## Astro 1.0 Beta On April 4, 2022 we released the Astro 1.0 Beta! 🎉 diff --git a/src/pages/en/reference/api-reference.md b/src/pages/en/reference/api-reference.md index 8397dce405dc3..7133c6af9e171 100644 --- a/src/pages/en/reference/api-reference.md +++ b/src/pages/en/reference/api-reference.md @@ -97,15 +97,14 @@ const data = await Astro.glob('../data/**/*.js'); ### `Astro.request` -`Astro.request` is a standard [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) object. It can be used to get the `url`, `headers`, `method`, and even body of the request. Use `new URL(Astro.request.url)` to get a URL object. +`Astro.request` is a standard [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) object. It can be used to get the `url`, `headers`, `method`, and even body of the request. ```astro ---- -const url = new URL(Astro.request.url); ---- -

Origin {url.origin}

+

Received a {Astro.request.method} request to "{Astro.request.url}".

+

Received request headers: {JSON.stringify(Object.fromEntries(Astro.request.headers))} ``` +See also: [`Astro.url`](#astrourl) ### `Astro.response` `Astro.response` is a standard [ResponseInit](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response#init) object. It is used to set the `status`, `statusText`, and `headers` for a page's response. @@ -129,16 +128,37 @@ Astro.response.headers.set('Set-Cookie', 'a=b; Path=/;'); ### `Astro.canonicalURL` -The [canonical URL][canonical] of the current page. If the `site` option is set, the site's origin will be the origin of this URL. +:::caution[Deprecated] +Use [`Astro.url`](#astrourl) to construct your own canonical URL. +::: + +The [canonical URL][canonical] of the current page. + +### `Astro.url` + + + +A [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL) object consructed from the current `Astro.request.url` URL string value. Useful for interacting with individual properties of the request URL, like pathname and origin. -You can also use the `canonicalURL` to grab the current page's `pathname`. +Equivalent to doing `new URL(Astro.request.url)`. + +```astro +

The current URL is: {Astro.url}

+

The current URL pathname is: {Astro.url.pathname}

+

The current URL origin is: {Astro.url.origin}

+``` + +You can also use `Astro.url` to create new URLs by passing it as an argument to [`new URL()`](https://developer.mozilla.org/en-US/docs/Web/API/URL/URL). ```astro --- -const path = Astro.canonicalURL.pathname; +// Example: Construct a canonical URL using your production domain +const canonicalURL = new URL(Astro.url.pathname, Astro.site); +// Example: Construct a URL for SEO meta tags using your current domain +const socialImageURL = new URL('/images/preview.png', Astro.url); --- - -

Welcome to {path}

+ + ``` ### `Astro.clientAddress`