Skip to content

Commit

Permalink
deprecate Astro.canonicalURL & add Astro.url (#1033)
Browse files Browse the repository at this point in the history
* deprecate Astro.canonicalURL, add Astro.url

* respond to chris and sarah reviews

* fix bad link
  • Loading branch information
FredKSchott authored Jul 22, 2022
1 parent ea05233 commit 92666e4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
11 changes: 11 additions & 0 deletions src/pages/en/migrate.md
Original file line number Diff line number Diff line change
Expand Up @@ -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! 🎉
Expand Down
40 changes: 30 additions & 10 deletions src/pages/en/reference/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,14 @@ const data = await Astro.glob<CustomDataFile>('../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);
---
<h1>Origin {url.origin}</h1>
<p>Received a {Astro.request.method} request to "{Astro.request.url}".</p>
<p>Received request headers: <code>{JSON.stringify(Object.fromEntries(Astro.request.headers))}</code>
```

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.
Expand All @@ -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`

<Since v="1.0.0-rc" />

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
<h1>The current URL is: {Astro.url}</h1>
<h1>The current URL pathname is: {Astro.url.pathname}</h1>
<h1>The current URL origin is: {Astro.url.origin}</h1>
```

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);
---
<h1>Welcome to {path}</h1>
<link rel="canonical" href={canonicalURL} />
<meta property="og:image" content={socialImageURL} />
```

### `Astro.clientAddress`
Expand Down

0 comments on commit 92666e4

Please sign in to comment.