Skip to content

Commit

Permalink
Make Astro.request RFC changes (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewp authored Mar 29, 2022
1 parent 1aaa6ae commit 1fea8c8
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/components/UIString.astro
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export interface Props {
key: Keys;
}
---
{useTranslations(Astro)(Astro.props.key)}
{useTranslations(Astro)(Astro.props.key)}
2 changes: 1 addition & 1 deletion src/i18n/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function getLanguageString(key: Keys, lang = 'en'): string | undefined {
* <FrameworkComponent label={t('articleNav.nextPage')} />
*/
export function useTranslations(Astro: Readonly<AstroGlobal>): (key: Keys) => string | undefined {
const lang = getLanguageFromURL(Astro.request.canonicalURL.pathname);
const lang = getLanguageFromURL(Astro.canonicalURL.pathname);
return function getTranslation(key: Keys) {
return getLanguageString(key, lang);
};
Expand Down
7 changes: 4 additions & 3 deletions src/layouts/MainLayout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ import { SITE } from '../config.ts';
import { getLanguageFromURL } from '../util.ts';
const { content = {}, hideRightSidebar = false } = Astro.props;
const currentPage = Astro.request.url.pathname;
const url = new URL(Astro.request.url);
const currentPage = url.pathname;
const currentFile = `src/pages${currentPage.replace(/\/$/, '')}.md`;
const githubEditUrl = `https://github.com/withastro/docs/blob/main/${currentFile}`;
const formatTitle = (content, SITE) => (content.title ? `${content.title} 🚀 ${SITE.title}` : SITE.title);
const lang = getLanguageFromURL(Astro.request.url.pathname);
const lang = getLanguageFromURL(url.pathname);
---

<html dir={content.dir ?? 'ltr'} {lang} class="initial">
<head>
<HeadCommon />
<HeadSEO {content} canonicalURL={Astro.request.canonicalURL} />
<HeadSEO {content} canonicalURL={Astro.canonicalURL} />
<title set:html={formatTitle(content, SITE)} />
<style>
body {
Expand Down
2 changes: 1 addition & 1 deletion src/layouts/SplashLayout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { SITE } from '../config.ts';
import { getLanguageFromURL } from '../util.ts';
const { title, dir = 'ltr' } = Astro.props;
const lang = getLanguageFromURL(Astro.request.url.pathname);
const lang = getLanguageFromURL(new URL(Astro.request.url).pathname);
---

<html {dir} {lang} class="initial">
Expand Down
6 changes: 3 additions & 3 deletions src/pages/de/core-concepts/routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ Angenommen du hast eine Seite `pages/post/[pid].astro`:
```astro
---
// Beispiel: src/pages/post/[pid].astro
const {pid} = Astro.request.params;
const {pid} = Astro.params;
---
<p>Post: {pid}</p>
```

Allen Routen mit z. B. `/post/1`, `/post/abc` etc. werden `pages/post/[pid].astro` entsprechen. Jeder passende Pfad-Parameter wird an die Page-Komponente unter `Astro.request.params` weitergegeben.
Allen Routen mit z. B. `/post/1`, `/post/abc` etc. werden `pages/post/[pid].astro` entsprechen. Jeder passende Pfad-Parameter wird an die Page-Komponente unter `Astro.params` weitergegeben.

Zum Beispiel wird die Route `/post/abc` das folgende `Astro.request.params`-Objekt zur Verfügung halten:
Zum Beispiel wird die Route `/post/abc` das folgende `Astro.params`-Objekt zur Verfügung halten:

```json
{ "pid": "abc" }
Expand Down
6 changes: 3 additions & 3 deletions src/pages/en/reference/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export async function getStaticPaths() {
];
}
const { id } = Astro.request.params;
const { id } = Astro.params;
---
<h1>{id}</h1>
```
Expand All @@ -170,7 +170,7 @@ export async function getStaticPaths() {
});
}
const { id } = Astro.request.params;
const { id } = Astro.params;
const { post } = Astro.props;
---
<h1>{id}: {post.name}</h1>
Expand All @@ -191,7 +191,7 @@ export async function getStaticPaths() {
props: { post };
});
}
const {id} = Astro.request.params;
const {id} = Astro.params;
const {post} = Astro.props;
---
<body>
Expand Down
6 changes: 3 additions & 3 deletions src/pages/es/core-concepts/routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ Considera la siguiente página `pages/post/[pid].astro`:
```astro
---
// Example: src/pages/post/[pid].astro
const {pid} = Astro.request.params;
const {pid} = Astro.params;
---
<p>Artículo: {pid}</p>
```

Cualquier ruta como `/post/1`, `/post/abc`, etc. se corresponderá con `pages/post/[pid].astro`. El parámetro de ruta coincidente se pasará al componente de la página en `Astro.request.params`.
Cualquier ruta como `/post/1`, `/post/abc`, etc. se corresponderá con `pages/post/[pid].astro`. El parámetro de ruta coincidente se pasará al componente de la página en `Astro.params`.

Por ejemplo, la ruta `/post/abc` tendrás disponible el siguiente objeto `Astro.request.params`:
Por ejemplo, la ruta `/post/abc` tendrás disponible el siguiente objeto `Astro.params`:

```json
{ "pid": "abc" }
Expand Down
4 changes: 2 additions & 2 deletions src/pages/es/reference/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export async function getStaticPaths() {
{ params: { id: '2' } }
];
}
const {id} = Astro.request.params;
const {id} = Astro.params;
---
<body><h1>{id}</h1></body>
```
Expand All @@ -160,7 +160,7 @@ export async function getStaticPaths() {
props: { post } };
});
}
const {id} = Astro.request.params;
const {id} = Astro.params;
const {post} = Astro.props;
---
<body><h1>{id}: {post.name}</h1></body>
Expand Down

0 comments on commit 1fea8c8

Please sign in to comment.