Skip to content

Commit 16d9874

Browse files
Fix TypeScript errors in "Configuration" & "Routing and navigation" code snippets (#14089)
Co-authored-by: Yan <61414485+yanthomasdev@users.noreply.github.com>
1 parent 67dfe0b commit 16d9874

4 files changed

Lines changed: 15 additions & 10 deletions

File tree

src/content/docs/en/guides/internationalization.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,11 @@ Astro provides helper functions for your middleware so you can control your own
201201
```js title="src/middleware.js"
202202
import { defineMiddleware } from "astro:middleware";
203203
import { redirectToDefaultLocale } from "astro:i18n"; // function available with `manual` routing
204-
export const onRequest = defineMiddleware(async (ctx, next) => {
205-
if (ctx.url.startsWith("/about")) {
204+
export const onRequest = defineMiddleware((ctx, next) => {
205+
if (ctx.url.pathname.startsWith("/about")) {
206206
return next();
207207
} else {
208-
return redirectToDefaultLocale(302);
208+
return redirectToDefaultLocale(ctx, 302);
209209
}
210210
})
211211
```

src/content/docs/en/guides/middleware.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ When you have multiple middleware functions chained via [sequence()](#chaining-m
257257
Calling `next()` with this signature will create a new `Request` object using the old `ctx.request`. This means that trying to consume `Request.body`, either before or after this rewrite, will throw a runtime error. This error is often raised with [Astro Actions that use HTML forms](/en/guides/actions/#call-actions-from-an-html-form-action). In these cases, we recommend handling rewrites from your Astro templates using `Astro.rewrite()` instead of using middleware.
258258

259259
```js title="src/middleware.js"
260+
import { sequence } from "astro:middleware";
261+
260262
// Current URL is https://example.com/blog
261263

262264
// First middleware function

src/content/docs/en/guides/prefetch.mdx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ As some navigation might not always appear as `<a />` links, you can also prefet
100100
import { prefetch } from 'astro:prefetch';
101101
102102
const btn = document.getElementById('btn');
103-
btn.addEventListener('click', () => {
103+
btn?.addEventListener('click', () => {
104104
prefetch('/about');
105105
});
106106
</script>
@@ -158,11 +158,13 @@ To use `prefetch()` programmatically with large sets of links, you can set `eage
158158
<a class="link-moderate" href="/nice-link-20">A Nice Link 20</a>
159159
160160
<script>
161-
import { prefetch } from 'astro:prefetch';
161+
import { prefetch } from "astro:prefetch";
162162
163-
const linkModerate = document.getElementsByClassName('link-moderate');
164-
linkModerate.forEach((link) => prefetch(link.getAttribute('href'), {eagerness: 'moderate'}));
165-
163+
const linkModerate = document.getElementsByClassName("link-moderate");
164+
for (const link of linkModerate) {
165+
const href = link.getAttribute("href");
166+
if (href) prefetch(href, { eagerness: "moderate" });
167+
}
166168
</script>
167169
```
168170

src/content/docs/en/guides/typescript.mdx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,13 +307,14 @@ Astro includes helpers for working with the types returned by your [`getStaticPa
307307

308308
You can get the type of [`Astro.params`](/en/reference/api-reference/#params) with `InferGetStaticParamsType` and the type of [`Astro.props`](/en/reference/api-reference/#props) with `InferGetStaticPropsType` or you can use `GetStaticPaths` to infer both at once:
309309

310-
```astro title="src/pages/posts/[...id].astro" {2-6,18-19} "satisfies GetStaticPaths;"
310+
```astro title="src/pages/posts/[...id].astro" {2-6,19-20} "satisfies GetStaticPaths;"
311311
---
312312
import type {
313313
InferGetStaticParamsType,
314314
InferGetStaticPropsType,
315315
GetStaticPaths,
316316
} from "astro";
317+
import { getCollection } from "astro:content";
317318
318319
export const getStaticPaths = (async () => {
319320
const posts = await getCollection("blog");
@@ -328,7 +329,7 @@ export const getStaticPaths = (async () => {
328329
type Params = InferGetStaticParamsType<typeof getStaticPaths>;
329330
type Props = InferGetStaticPropsType<typeof getStaticPaths>;
330331
331-
const { id } = Astro.params as Params;
332+
const { id } = Astro.params;
332333
// ^? { id: string; }
333334
334335
const { title } = Astro.props;

0 commit comments

Comments
 (0)