Skip to content

Commit b1d87ec

Browse files
florian-lefebvreyanthomasdevsarah11918
authored
feat: deprecate Astro in getStaticPaths (#14432)
Co-authored-by: Yan <61414485+yanthomasdev@users.noreply.github.com> Co-authored-by: Sarah Rainsberger <5098874+sarah11918@users.noreply.github.com>
1 parent ece667a commit b1d87ec

File tree

14 files changed

+253
-132
lines changed

14 files changed

+253
-132
lines changed

.changeset/fast-bushes-fall.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
'astro': major
3+
---
4+
5+
Deprecates `Astro` in `getStaticPaths()`
6+
7+
In Astro 5.x, it was possible to access an `Astro` object inside `getStaticPaths()`. However, despite being typed the same as the `Astro` object accessible in the frontmatter, this object only had `site` and `generator` properties. This could lead to confusion about which `Astro` object properties were available inside `getStaticPaths()`.
8+
9+
Astro 6.0 deprecates this object for `getStaticPaths()` to avoid confusion and improves error handling when attempting to access `Astro` values that are unavailable. Using `Astro.site` or `Astro.generator` within `getStaticPaths()` will now log a deprecation warning, and accessing any other property will throw a specific error with a helpful message. In a future major version, this object will be removed entirely, and accessing `Astro.site` or `Astro.generator` will also throw an error.
10+
11+
#### What should I do?
12+
13+
Update your `getStaticPaths()` function if you were attempting to access any `Astro` properties inside its scope. Remove `Astro.generator` entirely, and replace all occurrences of `Astro.site()` with `import.meta.env.SITE`:
14+
15+
```diff
16+
---
17+
// src/pages/blog/[slug].astro
18+
import { getPages } from "../../../utils/data";
19+
20+
export async function getStaticPaths() {
21+
- console.log(Astro.generator);
22+
- return getPages(Astro.site);
23+
+ return getPages(import.meta.env.SITE);
24+
}
25+
---
26+
```
27+

packages/astro/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
"test:integration": "astro-scripts test \"test/*.test.js\""
107107
},
108108
"dependencies": {
109-
"@astrojs/compiler": "^2.12.2",
109+
"@astrojs/compiler": "0.0.0-next-result-create-astro-20250926081949",
110110
"@astrojs/internal-helpers": "workspace:*",
111111
"@astrojs/markdown-remark": "workspace:*",
112112
"@astrojs/telemetry": "workspace:*",

packages/astro/src/core/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// process.env.PACKAGE_VERSION is injected when we build and publish the astro package.
22
export const ASTRO_VERSION = process.env.PACKAGE_VERSION ?? 'development';
33

4+
export const ASTRO_GENERATOR = `Astro v${ASTRO_VERSION}`;
5+
46
/**
57
* The name for the header used to help rerouting behavior.
68
* When set to "no", astro will NOT try to reroute an error response to the corresponding error page, which is the default behavior that can sometimes lead to loops.

packages/astro/src/core/errors/errors-data.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,6 +1417,19 @@ export const CspNotEnabled = {
14171417
message: "The `experimental.csp` configuration isn't enabled.",
14181418
} satisfies ErrorData;
14191419

1420+
/**
1421+
* @docs
1422+
* @description
1423+
* Unavailable Astro global in getStaticPaths
1424+
* @message
1425+
* The Astro global is not available in getStaticPaths().
1426+
*/
1427+
export const UnavailableAstroGlobal = {
1428+
name: 'UnavailableAstroGlobal',
1429+
title: 'Unavailable Astro global in getStaticPaths()',
1430+
message: (name: string) => `The Astro global is not available in this scope. Please remove "Astro.${name}" from your getStaticPaths() function.`,
1431+
} satisfies ErrorData;
1432+
14201433
/**
14211434
* @docs
14221435
* @kind heading

packages/astro/src/core/middleware/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
} from '../../i18n/utils.js';
77
import type { MiddlewareHandler, Params, RewritePayload } from '../../types/public/common.js';
88
import type { APIContext, AstroSharedContextCsp } from '../../types/public/context.js';
9-
import { ASTRO_VERSION } from '../constants.js';
9+
import { ASTRO_GENERATOR } from '../constants.js';
1010
import { AstroCookies } from '../cookies/index.js';
1111
import { AstroError, AstroErrorData } from '../errors/index.js';
1212
import { getClientIpAddress } from '../routing/request.js';
@@ -73,7 +73,7 @@ function createContext({
7373
request,
7474
params,
7575
site: undefined,
76-
generator: `Astro v${ASTRO_VERSION}`,
76+
generator: ASTRO_GENERATOR,
7777
props: {},
7878
rewrite,
7979
routePattern: '',

packages/astro/src/core/render-context.ts

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,11 @@ import { renderEndpoint } from '../runtime/server/endpoint.js';
1212
import { renderPage } from '../runtime/server/index.js';
1313
import type { ComponentInstance } from '../types/astro.js';
1414
import type { MiddlewareHandler, Props, RewritePayload } from '../types/public/common.js';
15-
import type {
16-
APIContext,
17-
AstroGlobal,
18-
AstroGlobalPartial,
19-
AstroSharedContextCsp,
20-
} from '../types/public/context.js';
15+
import type { APIContext, AstroGlobal, AstroSharedContextCsp } from '../types/public/context.js';
2116
import type { RouteData, SSRResult } from '../types/public/internal.js';
2217
import type { SSRActions } from './app/types.js';
2318
import {
24-
ASTRO_VERSION,
19+
ASTRO_GENERATOR,
2520
REROUTE_DIRECTIVE_HEADER,
2621
REWRITE_DIRECTIVE_HEADER_KEY,
2722
REWRITE_DIRECTIVE_HEADER_VALUE,
@@ -384,7 +379,6 @@ export class RenderContext {
384379
createActionAPIContext(): ActionAPIContext {
385380
const renderContext = this;
386381
const { params, pipeline, url } = this;
387-
const generator = `Astro v${ASTRO_VERSION}`;
388382

389383
const rewrite = async (reroutePayload: RewritePayload) => {
390384
return await this.#executeRewrite(reroutePayload);
@@ -403,7 +397,7 @@ export class RenderContext {
403397
get currentLocale() {
404398
return renderContext.computeCurrentLocale();
405399
},
406-
generator,
400+
generator: ASTRO_GENERATOR,
407401
get locals() {
408402
return renderContext.locals;
409403
},
@@ -532,8 +526,7 @@ export class RenderContext {
532526
compressHTML,
533527
cookies,
534528
/** This function returns the `Astro` faux-global */
535-
createAstro: (astroGlobal, props, slots) =>
536-
this.createAstro(result, astroGlobal, props, slots, ctx),
529+
createAstro: (props, slots) => this.createAstro(result, props, slots, ctx),
537530
links,
538531
params: this.params,
539532
partial,
@@ -588,26 +581,17 @@ export class RenderContext {
588581
*/
589582
createAstro(
590583
result: SSRResult,
591-
astroStaticPartial: AstroGlobalPartial,
592584
props: Record<string, any>,
593585
slotValues: Record<string, any> | null,
594586
apiContext: ActionAPIContext,
595587
): AstroGlobal {
596588
let astroPagePartial;
597589
// During rewriting, we must recompute the Astro global, because we need to purge the previous params/props/etc.
598590
if (this.isRewriting) {
599-
astroPagePartial = this.#astroPagePartial = this.createAstroPagePartial(
600-
result,
601-
astroStaticPartial,
602-
apiContext,
603-
);
591+
astroPagePartial = this.#astroPagePartial = this.createAstroPagePartial(result, apiContext);
604592
} else {
605593
// Create page partial with static partial so they can be cached together.
606-
astroPagePartial = this.#astroPagePartial ??= this.createAstroPagePartial(
607-
result,
608-
astroStaticPartial,
609-
apiContext,
610-
);
594+
astroPagePartial = this.#astroPagePartial ??= this.createAstroPagePartial(result, apiContext);
611595
}
612596
// Create component-level partials. `Astro.self` is added by the compiler.
613597
const astroComponentPartial = { props, self: null };
@@ -638,7 +622,6 @@ export class RenderContext {
638622

639623
createAstroPagePartial(
640624
result: SSRResult,
641-
astroStaticPartial: AstroGlobalPartial,
642625
apiContext: ActionAPIContext,
643626
): Omit<AstroGlobal, 'props' | 'self' | 'slots'> {
644627
const renderContext = this;
@@ -661,7 +644,7 @@ export class RenderContext {
661644
const callAction = createCallAction(apiContext);
662645

663646
return {
664-
generator: astroStaticPartial.generator,
647+
generator: ASTRO_GENERATOR,
665648
routePattern: this.routeData.route,
666649
isPrerendered: this.routeData.prerender,
667650
cookies,
Lines changed: 93 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,99 @@
1-
import { ASTRO_VERSION } from '../../core/constants.js';
2-
import type { AstroGlobalPartial } from '../../types/public/context.js';
1+
import { ASTRO_GENERATOR } from '../../core/constants.js';
2+
import { AstroError, AstroErrorData } from '../../core/errors/index.js';
3+
import type { AstroGlobal } from '../../types/public/context.js';
4+
5+
function createError(name: string) {
6+
return new AstroError({
7+
...AstroErrorData.UnavailableAstroGlobal,
8+
message: AstroErrorData.UnavailableAstroGlobal.message(name),
9+
});
10+
}
311

412
// This is used to create the top-level Astro global; the one that you can use
513
// inside of getStaticPaths. See the `astroGlobalArgs` option for parameter type.
6-
export function createAstro(site: string | undefined): AstroGlobalPartial {
14+
export function createAstro(site: string | undefined): AstroGlobal {
715
return {
8-
site: site ? new URL(site) : undefined,
9-
generator: `Astro v${ASTRO_VERSION}`,
16+
// TODO: throw in Astro 7
17+
get site() {
18+
// This is created inside of the runtime so we don't have access to the Astro logger.
19+
console.warn(
20+
`Astro.site inside getStaticPaths is deprecated and will be removed in a future major version of Astro. Use import.meta.env.SITE instead`,
21+
);
22+
return site ? new URL(site) : undefined;
23+
},
24+
// TODO: throw in Astro 7
25+
get generator() {
26+
// This is created inside of the runtime so we don't have access to the Astro logger.
27+
console.warn(
28+
`Astro.generator inside getStaticPaths is deprecated and will be removed in a future major version of Astro.`,
29+
);
30+
return ASTRO_GENERATOR;
31+
},
32+
get callAction(): any {
33+
throw createError('callAction');
34+
},
35+
get clientAddress(): any {
36+
throw createError('clientAddress');
37+
},
38+
get cookies(): any {
39+
throw createError('cookies');
40+
},
41+
get csp(): any {
42+
throw createError('csp');
43+
},
44+
get currentLocale(): any {
45+
throw createError('currentLocale');
46+
},
47+
get getActionResult(): any {
48+
throw createError('getActionResult');
49+
},
50+
get isPrerendered(): any {
51+
throw createError('isPrerendered');
52+
},
53+
get locals(): any {
54+
throw createError('locals');
55+
},
56+
get originPathname(): any {
57+
throw createError('originPathname');
58+
},
59+
get params(): any {
60+
throw createError('params');
61+
},
62+
get preferredLocale(): any {
63+
throw createError('preferredLocale');
64+
},
65+
get preferredLocaleList(): any {
66+
throw createError('preferredLocaleList');
67+
},
68+
get props(): any {
69+
throw createError('props');
70+
},
71+
get redirect(): any {
72+
throw createError('redirect');
73+
},
74+
get request(): any {
75+
throw createError('request');
76+
},
77+
get response(): any {
78+
throw createError('response');
79+
},
80+
get rewrite(): any {
81+
throw createError('rewrite');
82+
},
83+
get routePattern(): any {
84+
throw createError('routePattern');
85+
},
86+
get self(): any {
87+
throw createError('self');
88+
},
89+
get slots(): any {
90+
throw createError('slots');
91+
},
92+
get url(): any {
93+
throw createError('url');
94+
},
95+
get session(): any {
96+
throw createError('session');
97+
},
1098
};
1199
}

packages/astro/src/types/public/context.ts

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,23 @@ export interface AstroGlobal<
2121
Self = AstroComponentFactory,
2222
// eslint-disable-next-line @typescript-eslint/no-shadow
2323
Params extends Record<string, string | undefined> = Record<string, string | undefined>,
24-
> extends AstroGlobalPartial,
25-
AstroSharedContext<Props, Params> {
24+
> extends AstroSharedContext<Props, Params> {
25+
/**
26+
* Returns a [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL) object built from the [site](https://docs.astro.build/en/reference/configuration-reference/#site) config option
27+
*
28+
* [Astro reference](https://docs.astro.build/en/reference/api-reference/#astrosite)
29+
*/
30+
site: URL | undefined;
31+
/**
32+
* Returns a string with the current version of Astro.
33+
*
34+
* Useful for using `<meta name="generator" content={Astro.generator} />` or crediting Astro in a site footer.
35+
*
36+
* [HTML Specification for `generator`](https://html.spec.whatwg.org/multipage/semantics.html#meta-generator)
37+
*
38+
* [Astro reference](https://docs.astro.build/en/reference/api-reference/#astrogenerator)
39+
*/
40+
generator: string;
2641
/**
2742
* A full URL object of the request URL.
2843
* Equivalent to: `new URL(Astro.request.url)`
@@ -206,25 +221,6 @@ export interface AstroGlobal<
206221
};
207222
}
208223

209-
export interface AstroGlobalPartial {
210-
/**
211-
* Returns a [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL) object built from the [site](https://docs.astro.build/en/reference/configuration-reference/#site) config option
212-
*
213-
* [Astro reference](https://docs.astro.build/en/reference/api-reference/#astrosite)
214-
*/
215-
site: URL | undefined;
216-
/**
217-
* Returns a string with the current version of Astro.
218-
*
219-
* Useful for using `<meta name="generator" content={Astro.generator} />` or crediting Astro in a site footer.
220-
*
221-
* [HTML Specification for `generator`](https://html.spec.whatwg.org/multipage/semantics.html#meta-generator)
222-
*
223-
* [Astro reference](https://docs.astro.build/en/reference/api-reference/#astrogenerator)
224-
*/
225-
generator: string;
226-
}
227-
228224
// Shared types between `Astro` global and API context object
229225
export interface AstroSharedContext<
230226
Props extends Record<string, any> = Record<string, any>,

packages/astro/src/types/public/internal.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { AstroCookies } from '../../core/cookies/cookies.js';
66
import type { AstroComponentInstance, ServerIslandComponent } from '../../runtime/server/index.js';
77
import type { Params } from './common.js';
88
import type { AstroConfig, RedirectConfig } from './config.js';
9-
import type { AstroGlobal, AstroGlobalPartial } from './context.js';
9+
import type { AstroGlobal } from './context.js';
1010
import type { AstroRenderer } from './integrations.js';
1111

1212
export type { SSRActions, SSRManifest, SSRManifestCSP } from '../../core/app/types.js';
@@ -221,11 +221,7 @@ export interface SSRResult {
221221
links: Set<SSRElement>;
222222
componentMetadata: Map<string, SSRComponentMetadata>;
223223
inlinedScripts: Map<string, string>;
224-
createAstro(
225-
Astro: AstroGlobalPartial,
226-
props: Record<string, any>,
227-
slots: Record<string, any> | null,
228-
): AstroGlobal;
224+
createAstro(props: Record<string, any>, slots: Record<string, any> | null): AstroGlobal;
229225
params: Params;
230226
resolve: (s: string) => Promise<string>;
231227
response: AstroGlobal['response'];

0 commit comments

Comments
 (0)