astro 3.5.5 → 3.5.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/components/ViewTransitions.astro +16 -8
- package/dist/@types/astro.d.ts +11 -1
- package/dist/cli/add/index.js +9 -1
- package/dist/core/app/index.js +22 -5
- package/dist/core/build/buildPipeline.js +5 -7
- package/dist/core/build/generate.js +81 -72
- package/dist/core/build/internal.d.ts +0 -6
- package/dist/core/build/internal.js +3 -16
- package/dist/core/build/page-data.js +18 -46
- package/dist/core/build/static-build.js +7 -9
- package/dist/core/build/types.d.ts +1 -1
- package/dist/core/constants.js +1 -1
- package/dist/core/dev/dev.js +1 -1
- package/dist/core/endpoint/index.d.ts +4 -2
- package/dist/core/endpoint/index.js +22 -4
- package/dist/core/messages.js +2 -2
- package/dist/core/middleware/index.js +3 -1
- package/dist/core/pipeline.js +4 -8
- package/dist/core/render/context.d.ts +3 -0
- package/dist/core/render/context.js +18 -1
- package/dist/core/render/core.js +3 -1
- package/dist/core/render/result.d.ts +2 -0
- package/dist/core/render/result.js +23 -1
- package/dist/core/routing/manifest/create.js +25 -20
- package/dist/core/routing/manifest/serialization.js +7 -1
- package/dist/core/routing/match.js +4 -1
- package/dist/core/routing/params.js +1 -1
- package/dist/i18n/middleware.js +2 -2
- package/dist/vite-plugin-astro-server/route.js +9 -3
- package/package.json +2 -2
|
@@ -30,6 +30,7 @@ const { fallback = 'animate', handleForms } = Astro.props;
|
|
|
30
30
|
import type { Options } from 'astro:transitions/client';
|
|
31
31
|
import { supportsViewTransitions, navigate } from 'astro:transitions/client';
|
|
32
32
|
// NOTE: import from `astro/prefetch` as `astro:prefetch` requires the `prefetch` config to be enabled
|
|
33
|
+
// @ts-ignore
|
|
33
34
|
import { init } from 'astro/prefetch';
|
|
34
35
|
|
|
35
36
|
export type Fallback = 'none' | 'animate' | 'swap';
|
|
@@ -42,27 +43,34 @@ const { fallback = 'animate', handleForms } = Astro.props;
|
|
|
42
43
|
return 'animate';
|
|
43
44
|
}
|
|
44
45
|
|
|
45
|
-
function isReloadEl(el: HTMLElement): boolean {
|
|
46
|
+
function isReloadEl(el: HTMLElement | SVGAElement): boolean {
|
|
46
47
|
return el.dataset.astroReload !== undefined;
|
|
47
48
|
}
|
|
48
49
|
|
|
49
50
|
if (supportsViewTransitions || getFallback() !== 'none') {
|
|
50
51
|
document.addEventListener('click', (ev) => {
|
|
51
52
|
let link = ev.target;
|
|
52
|
-
if (link instanceof Element
|
|
53
|
-
link = link.closest('a');
|
|
53
|
+
if (link instanceof Element) {
|
|
54
|
+
link = link.closest('a, area');
|
|
54
55
|
}
|
|
56
|
+
if (
|
|
57
|
+
!(link instanceof HTMLAnchorElement) &&
|
|
58
|
+
!(link instanceof SVGAElement) &&
|
|
59
|
+
!(link instanceof HTMLAreaElement)
|
|
60
|
+
)
|
|
61
|
+
return;
|
|
55
62
|
// This check verifies that the click is happening on an anchor
|
|
56
63
|
// that is going to another page within the same origin. Basically it determines
|
|
57
64
|
// same-origin navigation, but omits special key combos for new tabs, etc.
|
|
65
|
+
const linkTarget = link instanceof HTMLElement ? link.target : link.target.baseVal;
|
|
66
|
+
const href = link instanceof HTMLElement ? link.href : link.href.baseVal;
|
|
67
|
+
const origin = new URL(href, location.href).origin;
|
|
58
68
|
if (
|
|
59
|
-
!link ||
|
|
60
|
-
!(link instanceof HTMLAnchorElement) ||
|
|
61
69
|
isReloadEl(link) ||
|
|
62
70
|
link.hasAttribute('download') ||
|
|
63
71
|
!link.href ||
|
|
64
|
-
(
|
|
65
|
-
|
|
72
|
+
(linkTarget && linkTarget !== '_self') ||
|
|
73
|
+
origin !== location.origin ||
|
|
66
74
|
ev.button !== 0 || // left clicks only
|
|
67
75
|
ev.metaKey || // new tab (mac)
|
|
68
76
|
ev.ctrlKey || // new tab (windows)
|
|
@@ -75,7 +83,7 @@ const { fallback = 'animate', handleForms } = Astro.props;
|
|
|
75
83
|
return;
|
|
76
84
|
}
|
|
77
85
|
ev.preventDefault();
|
|
78
|
-
navigate(
|
|
86
|
+
navigate(href, {
|
|
79
87
|
history: link.dataset.astroHistory === 'replace' ? 'replace' : 'auto',
|
|
80
88
|
});
|
|
81
89
|
});
|
package/dist/@types/astro.d.ts
CHANGED
|
@@ -1903,6 +1903,10 @@ interface AstroSharedContext<Props extends Record<string, any> = Record<string,
|
|
|
1903
1903
|
* The list of locales computed from the `Accept-Language` header of the browser, sorted by quality value (**SSR Only**).
|
|
1904
1904
|
*/
|
|
1905
1905
|
preferredLocaleList: string[] | undefined;
|
|
1906
|
+
/**
|
|
1907
|
+
* The current locale computed from the URL of the request. It matches the locales in `i18n.locales`, and returns `undefined` otherwise.
|
|
1908
|
+
*/
|
|
1909
|
+
currentLocale: string | undefined;
|
|
1906
1910
|
}
|
|
1907
1911
|
export interface APIContext<Props extends Record<string, any> = Record<string, any>, APIParams extends Record<string, string | undefined> = Record<string, string | undefined>> extends AstroSharedContext<Props, Params> {
|
|
1908
1912
|
site: URL | undefined;
|
|
@@ -2025,6 +2029,10 @@ export interface APIContext<Props extends Record<string, any> = Record<string, a
|
|
|
2025
2029
|
* [quality value]: https://developer.mozilla.org/en-US/docs/Glossary/Quality_values
|
|
2026
2030
|
*/
|
|
2027
2031
|
preferredLocaleList: string[] | undefined;
|
|
2032
|
+
/**
|
|
2033
|
+
* The current locale computed from the URL of the request. It matches the locales in `i18n.locales`, and returns `undefined` otherwise.
|
|
2034
|
+
*/
|
|
2035
|
+
currentLocale: string | undefined;
|
|
2028
2036
|
}
|
|
2029
2037
|
export type EndpointOutput = {
|
|
2030
2038
|
body: Body;
|
|
@@ -2178,14 +2186,16 @@ export interface RouteData {
|
|
|
2178
2186
|
prerender: boolean;
|
|
2179
2187
|
redirect?: RedirectConfig;
|
|
2180
2188
|
redirectRoute?: RouteData;
|
|
2189
|
+
fallbackRoutes: RouteData[];
|
|
2181
2190
|
}
|
|
2182
2191
|
export type RedirectRouteData = RouteData & {
|
|
2183
2192
|
redirect: string;
|
|
2184
2193
|
};
|
|
2185
|
-
export type SerializedRouteData = Omit<RouteData, 'generate' | 'pattern' | 'redirectRoute'> & {
|
|
2194
|
+
export type SerializedRouteData = Omit<RouteData, 'generate' | 'pattern' | 'redirectRoute' | 'fallbackRoutes'> & {
|
|
2186
2195
|
generate: undefined;
|
|
2187
2196
|
pattern: string;
|
|
2188
2197
|
redirectRoute: SerializedRouteData | undefined;
|
|
2198
|
+
fallbackRoutes: SerializedRouteData[];
|
|
2189
2199
|
_meta: {
|
|
2190
2200
|
trailingSlash: AstroConfig['trailingSlash'];
|
|
2191
2201
|
};
|
package/dist/cli/add/index.js
CHANGED
|
@@ -608,8 +608,10 @@ async function fetchPackageJson(scope, name, tag) {
|
|
|
608
608
|
const res = await fetch(`${registry}/${packageName}/${tag}`);
|
|
609
609
|
if (res.status >= 200 && res.status < 300) {
|
|
610
610
|
return await res.json();
|
|
611
|
-
} else {
|
|
611
|
+
} else if (res.status === 404) {
|
|
612
612
|
return new Error();
|
|
613
|
+
} else {
|
|
614
|
+
return new Error(`Failed to fetch ${registry}/${packageName}/${tag} - GET ${res.status}`);
|
|
613
615
|
}
|
|
614
616
|
}
|
|
615
617
|
async function validateIntegrations(integrations) {
|
|
@@ -629,6 +631,9 @@ async function validateIntegrations(integrations) {
|
|
|
629
631
|
} else {
|
|
630
632
|
const firstPartyPkgCheck = await fetchPackageJson("@astrojs", name, tag);
|
|
631
633
|
if (firstPartyPkgCheck instanceof Error) {
|
|
634
|
+
if (firstPartyPkgCheck.message) {
|
|
635
|
+
spinner.warn(yellow(firstPartyPkgCheck.message));
|
|
636
|
+
}
|
|
632
637
|
spinner.warn(
|
|
633
638
|
yellow(`${bold(integration)} is not an official Astro package. Use at your own risk!`)
|
|
634
639
|
);
|
|
@@ -655,6 +660,9 @@ async function validateIntegrations(integrations) {
|
|
|
655
660
|
if (pkgType === "third-party") {
|
|
656
661
|
const thirdPartyPkgCheck = await fetchPackageJson(scope, name, tag);
|
|
657
662
|
if (thirdPartyPkgCheck instanceof Error) {
|
|
663
|
+
if (thirdPartyPkgCheck.message) {
|
|
664
|
+
spinner.warn(yellow(thirdPartyPkgCheck.message));
|
|
665
|
+
}
|
|
658
666
|
throw new Error(`Unable to fetch ${bold(integration)}. Does the package exist?`);
|
|
659
667
|
} else {
|
|
660
668
|
pkgJson = thirdPartyPkgCheck;
|
package/dist/core/app/index.js
CHANGED
|
@@ -96,6 +96,11 @@ class App {
|
|
|
96
96
|
}
|
|
97
97
|
return pathname;
|
|
98
98
|
}
|
|
99
|
+
#getPathnameFromRequest(request) {
|
|
100
|
+
const url = new URL(request.url);
|
|
101
|
+
const pathname = prependForwardSlash(this.removeBase(url.pathname));
|
|
102
|
+
return pathname;
|
|
103
|
+
}
|
|
99
104
|
match(request, _opts = {}) {
|
|
100
105
|
const url = new URL(request.url);
|
|
101
106
|
if (this.#manifest.assets.has(url.pathname))
|
|
@@ -117,7 +122,8 @@ class App {
|
|
|
117
122
|
return this.#renderError(request, { status: 404 });
|
|
118
123
|
}
|
|
119
124
|
Reflect.set(request, clientLocalsSymbol, locals ?? {});
|
|
120
|
-
const
|
|
125
|
+
const pathname = this.#getPathnameFromRequest(request);
|
|
126
|
+
const defaultStatus = this.#getDefaultStatusCode(routeData, pathname);
|
|
121
127
|
const mod = await this.#getModuleForRoute(routeData);
|
|
122
128
|
const pageModule = await mod.page();
|
|
123
129
|
const url = new URL(request.url);
|
|
@@ -188,7 +194,9 @@ class App {
|
|
|
188
194
|
status,
|
|
189
195
|
env: this.#pipeline.env,
|
|
190
196
|
mod: handler,
|
|
191
|
-
locales: this.#manifest.i18n
|
|
197
|
+
locales: this.#manifest.i18n?.locales,
|
|
198
|
+
routingStrategy: this.#manifest.i18n?.routingStrategy,
|
|
199
|
+
defaultLocale: this.#manifest.i18n?.defaultLocale
|
|
192
200
|
});
|
|
193
201
|
} else {
|
|
194
202
|
const pathname = prependForwardSlash(this.removeBase(url.pathname));
|
|
@@ -220,7 +228,9 @@ class App {
|
|
|
220
228
|
status,
|
|
221
229
|
mod,
|
|
222
230
|
env: this.#pipeline.env,
|
|
223
|
-
locales: this.#manifest.i18n
|
|
231
|
+
locales: this.#manifest.i18n?.locales,
|
|
232
|
+
routingStrategy: this.#manifest.i18n?.routingStrategy,
|
|
233
|
+
defaultLocale: this.#manifest.i18n?.defaultLocale
|
|
224
234
|
});
|
|
225
235
|
}
|
|
226
236
|
}
|
|
@@ -293,8 +303,15 @@ class App {
|
|
|
293
303
|
headers: new Headers(Array.from(headers))
|
|
294
304
|
});
|
|
295
305
|
}
|
|
296
|
-
#getDefaultStatusCode(
|
|
297
|
-
|
|
306
|
+
#getDefaultStatusCode(routeData, pathname) {
|
|
307
|
+
if (!routeData.pattern.exec(pathname)) {
|
|
308
|
+
for (const fallbackRoute of routeData.fallbackRoutes) {
|
|
309
|
+
if (fallbackRoute.pattern.test(pathname)) {
|
|
310
|
+
return 302;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
const route = removeTrailingForwardSlash(routeData.route);
|
|
298
315
|
if (route.endsWith("/404"))
|
|
299
316
|
return 404;
|
|
300
317
|
if (route.endsWith("/500"))
|
|
@@ -128,13 +128,11 @@ class BuildPipeline extends Pipeline {
|
|
|
128
128
|
pages.set(pageData, filePath);
|
|
129
129
|
}
|
|
130
130
|
}
|
|
131
|
-
for (const [path,
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
pages.set(pageData, path);
|
|
137
|
-
}
|
|
131
|
+
for (const [path, pageData] of this.#internals.pagesByComponent.entries()) {
|
|
132
|
+
if (routeIsRedirect(pageData.route)) {
|
|
133
|
+
pages.set(pageData, path);
|
|
134
|
+
} else if (routeIsFallback(pageData.route) && (i18nHasFallback(this.getConfig()) || routeIsFallback(pageData.route) && pageData.route.route === "/")) {
|
|
135
|
+
pages.set(pageData, path);
|
|
138
136
|
}
|
|
139
137
|
}
|
|
140
138
|
return pages;
|
|
@@ -194,15 +194,40 @@ ${bgGreen(black(` generating optimized images `))}`);
|
|
|
194
194
|
});
|
|
195
195
|
}
|
|
196
196
|
async function generatePage(pageData, ssrEntry, builtPaths, pipeline) {
|
|
197
|
-
let timeStart = performance.now();
|
|
198
197
|
const logger = pipeline.getLogger();
|
|
199
198
|
const config = pipeline.getConfig();
|
|
200
|
-
const
|
|
201
|
-
const linkIds = [];
|
|
202
|
-
const scripts = pageInfo?.hoistedScript ?? null;
|
|
203
|
-
const styles = pageData.styles.sort(cssOrder).map(({ sheet }) => sheet).reduce(mergeInlineCss, []);
|
|
199
|
+
const manifest = pipeline.getManifest();
|
|
204
200
|
const pageModulePromise = ssrEntry.page;
|
|
205
201
|
const onRequest = ssrEntry.onRequest;
|
|
202
|
+
const pageInfo = getPageDataByComponent(pipeline.getInternals(), pageData.route.component);
|
|
203
|
+
const hoistedScripts = pageInfo?.hoistedScript ?? null;
|
|
204
|
+
const moduleStyles = pageData.styles.sort(cssOrder).map(({ sheet }) => sheet).reduce(mergeInlineCss, []);
|
|
205
|
+
const links = /* @__PURE__ */ new Set();
|
|
206
|
+
const styles = createStylesheetElementSet(moduleStyles, manifest.base, manifest.assetsPrefix);
|
|
207
|
+
const scripts = createModuleScriptsSet(
|
|
208
|
+
hoistedScripts ? [hoistedScripts] : [],
|
|
209
|
+
manifest.base,
|
|
210
|
+
manifest.assetsPrefix
|
|
211
|
+
);
|
|
212
|
+
if (pipeline.getSettings().scripts.some((script) => script.stage === "page")) {
|
|
213
|
+
const hashedFilePath = pipeline.getInternals().entrySpecifierToBundleMap.get(PAGE_SCRIPT_ID);
|
|
214
|
+
if (typeof hashedFilePath !== "string") {
|
|
215
|
+
throw new Error(`Cannot find the built path for ${PAGE_SCRIPT_ID}`);
|
|
216
|
+
}
|
|
217
|
+
const src = createAssetLink(hashedFilePath, manifest.base, manifest.assetsPrefix);
|
|
218
|
+
scripts.add({
|
|
219
|
+
props: { type: "module", src },
|
|
220
|
+
children: ""
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
for (const script of pipeline.getSettings().scripts) {
|
|
224
|
+
if (script.stage === "head-inline") {
|
|
225
|
+
scripts.add({
|
|
226
|
+
props: {},
|
|
227
|
+
children: script.content
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
}
|
|
206
231
|
const i18nMiddleware = createI18nMiddleware(
|
|
207
232
|
pipeline.getManifest().i18n,
|
|
208
233
|
pipeline.getManifest().base,
|
|
@@ -234,42 +259,44 @@ async function generatePage(pageData, ssrEntry, builtPaths, pipeline) {
|
|
|
234
259
|
);
|
|
235
260
|
return;
|
|
236
261
|
}
|
|
237
|
-
const
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
262
|
+
for (const route of eachRouteInRouteData(pageData)) {
|
|
263
|
+
const paths = await getPathsForRoute(route, pageModule, pipeline, builtPaths);
|
|
264
|
+
let timeStart = performance.now();
|
|
265
|
+
let prevTimeEnd = timeStart;
|
|
266
|
+
for (let i = 0; i < paths.length; i++) {
|
|
267
|
+
const path = paths[i];
|
|
268
|
+
pipeline.getEnvironment().logger.debug("build", `Generating: ${path}`);
|
|
269
|
+
await generatePath(path, pipeline, route, links, scripts, styles, pageModule);
|
|
270
|
+
const timeEnd = performance.now();
|
|
271
|
+
const timeChange = getTimeStat(prevTimeEnd, timeEnd);
|
|
272
|
+
const timeIncrease = `(+${timeChange})`;
|
|
273
|
+
const filePath = getOutputFilename(pipeline.getConfig(), path, pageData.route.type);
|
|
274
|
+
const lineIcon = i === paths.length - 1 ? "\u2514\u2500" : "\u251C\u2500";
|
|
275
|
+
logger.info(null, ` ${cyan(lineIcon)} ${dim(filePath)} ${dim(timeIncrease)}`);
|
|
276
|
+
prevTimeEnd = timeEnd;
|
|
277
|
+
}
|
|
249
278
|
}
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
const timeEnd = performance.now();
|
|
256
|
-
const timeChange = getTimeStat(prevTimeEnd, timeEnd);
|
|
257
|
-
const timeIncrease = `(+${timeChange})`;
|
|
258
|
-
const filePath = getOutputFilename(pipeline.getConfig(), path, pageData.route.type);
|
|
259
|
-
const lineIcon = i === paths.length - 1 ? "\u2514\u2500" : "\u251C\u2500";
|
|
260
|
-
logger.info(null, ` ${cyan(lineIcon)} ${dim(filePath)} ${dim(timeIncrease)}`);
|
|
261
|
-
prevTimeEnd = timeEnd;
|
|
279
|
+
}
|
|
280
|
+
function* eachRouteInRouteData(data) {
|
|
281
|
+
yield data.route;
|
|
282
|
+
for (const fallbackRoute of data.route.fallbackRoutes) {
|
|
283
|
+
yield fallbackRoute;
|
|
262
284
|
}
|
|
263
285
|
}
|
|
264
|
-
async function getPathsForRoute(
|
|
286
|
+
async function getPathsForRoute(route, mod, pipeline, builtPaths) {
|
|
265
287
|
const opts = pipeline.getStaticBuildOptions();
|
|
266
288
|
const logger = pipeline.getLogger();
|
|
267
289
|
let paths = [];
|
|
268
|
-
if (
|
|
269
|
-
paths.push(
|
|
270
|
-
builtPaths.add(
|
|
290
|
+
if (route.pathname) {
|
|
291
|
+
paths.push(route.pathname);
|
|
292
|
+
builtPaths.add(route.pathname);
|
|
293
|
+
for (const virtualRoute of route.fallbackRoutes) {
|
|
294
|
+
if (virtualRoute.pathname) {
|
|
295
|
+
paths.push(virtualRoute.pathname);
|
|
296
|
+
builtPaths.add(virtualRoute.pathname);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
271
299
|
} else {
|
|
272
|
-
const route = pageData.route;
|
|
273
300
|
const staticPaths = await callGetStaticPaths({
|
|
274
301
|
mod,
|
|
275
302
|
route,
|
|
@@ -353,38 +380,18 @@ function getUrlForPath(pathname, base, origin, format, routeType) {
|
|
|
353
380
|
const url = new URL(buildPathname, origin);
|
|
354
381
|
return url;
|
|
355
382
|
}
|
|
356
|
-
async function generatePath(pathname,
|
|
383
|
+
async function generatePath(pathname, pipeline, route, links, scripts, styles, mod) {
|
|
357
384
|
const manifest = pipeline.getManifest();
|
|
358
|
-
const
|
|
359
|
-
if (pageData.route.type === "page") {
|
|
360
|
-
addPageName(pathname, pipeline.getStaticBuildOptions());
|
|
361
|
-
}
|
|
385
|
+
const logger = pipeline.getLogger();
|
|
362
386
|
pipeline.getEnvironment().logger.debug("build", `Generating: ${pathname}`);
|
|
363
|
-
const
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
);
|
|
369
|
-
const styles = createStylesheetElementSet(_styles, manifest.base, manifest.assetsPrefix);
|
|
370
|
-
if (pipeline.getSettings().scripts.some((script) => script.stage === "page")) {
|
|
371
|
-
const hashedFilePath = pipeline.getInternals().entrySpecifierToBundleMap.get(PAGE_SCRIPT_ID);
|
|
372
|
-
if (typeof hashedFilePath !== "string") {
|
|
373
|
-
throw new Error(`Cannot find the built path for ${PAGE_SCRIPT_ID}`);
|
|
374
|
-
}
|
|
375
|
-
const src = createAssetLink(hashedFilePath, manifest.base, manifest.assetsPrefix);
|
|
376
|
-
scripts.add({
|
|
377
|
-
props: { type: "module", src },
|
|
378
|
-
children: ""
|
|
379
|
-
});
|
|
387
|
+
const icon = route.type === "page" || route.type === "redirect" || route.type === "fallback" ? green("\u25B6") : magenta("\u03BB");
|
|
388
|
+
if (isRelativePath(route.component)) {
|
|
389
|
+
logger.info(null, `${icon} ${route.route}`);
|
|
390
|
+
} else {
|
|
391
|
+
logger.info(null, `${icon} ${route.component}`);
|
|
380
392
|
}
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
scripts.add({
|
|
384
|
-
props: {},
|
|
385
|
-
children: script.content
|
|
386
|
-
});
|
|
387
|
-
}
|
|
393
|
+
if (route.type === "page") {
|
|
394
|
+
addPageName(pathname, pipeline.getStaticBuildOptions());
|
|
388
395
|
}
|
|
389
396
|
const ssr = isServerLikeOutput(pipeline.getConfig());
|
|
390
397
|
const url = getUrlForPath(
|
|
@@ -392,7 +399,7 @@ async function generatePath(pathname, gopts, pipeline) {
|
|
|
392
399
|
pipeline.getConfig().base,
|
|
393
400
|
pipeline.getStaticBuildOptions().origin,
|
|
394
401
|
pipeline.getConfig().build.format,
|
|
395
|
-
|
|
402
|
+
route.type
|
|
396
403
|
);
|
|
397
404
|
const request = createRequest({
|
|
398
405
|
url,
|
|
@@ -408,10 +415,12 @@ async function generatePath(pathname, gopts, pipeline) {
|
|
|
408
415
|
scripts,
|
|
409
416
|
styles,
|
|
410
417
|
links,
|
|
411
|
-
route
|
|
418
|
+
route,
|
|
412
419
|
env: pipeline.getEnvironment(),
|
|
413
420
|
mod,
|
|
414
|
-
locales: i18n
|
|
421
|
+
locales: i18n?.locales,
|
|
422
|
+
routingStrategy: i18n?.routingStrategy,
|
|
423
|
+
defaultLocale: i18n?.defaultLocale
|
|
415
424
|
});
|
|
416
425
|
let body;
|
|
417
426
|
let encoding;
|
|
@@ -420,7 +429,7 @@ async function generatePath(pathname, gopts, pipeline) {
|
|
|
420
429
|
response = await pipeline.renderRoute(renderContext, mod);
|
|
421
430
|
} catch (err) {
|
|
422
431
|
if (!AstroError.is(err) && !err.id && typeof err === "object") {
|
|
423
|
-
err.id =
|
|
432
|
+
err.id = route.component;
|
|
424
433
|
}
|
|
425
434
|
throw err;
|
|
426
435
|
}
|
|
@@ -444,8 +453,8 @@ async function generatePath(pathname, gopts, pipeline) {
|
|
|
444
453
|
if (pipeline.getConfig().compressHTML === true) {
|
|
445
454
|
body = body.replaceAll("\n", "");
|
|
446
455
|
}
|
|
447
|
-
if (
|
|
448
|
-
|
|
456
|
+
if (route.type !== "redirect") {
|
|
457
|
+
route.redirect = location.toString();
|
|
449
458
|
}
|
|
450
459
|
} else {
|
|
451
460
|
if (!response.body)
|
|
@@ -453,9 +462,9 @@ async function generatePath(pathname, gopts, pipeline) {
|
|
|
453
462
|
body = Buffer.from(await response.arrayBuffer());
|
|
454
463
|
encoding = response.headers.get("X-Astro-Encoding") ?? "utf-8";
|
|
455
464
|
}
|
|
456
|
-
const outFolder = getOutFolder(pipeline.getConfig(), pathname,
|
|
457
|
-
const outFile = getOutFile(pipeline.getConfig(), outFolder, pathname,
|
|
458
|
-
|
|
465
|
+
const outFolder = getOutFolder(pipeline.getConfig(), pathname, route.type);
|
|
466
|
+
const outFile = getOutFile(pipeline.getConfig(), outFolder, pathname, route.type);
|
|
467
|
+
route.distURL = outFile;
|
|
459
468
|
await fs.promises.mkdir(outFolder, { recursive: true });
|
|
460
469
|
await fs.promises.writeFile(outFile, body, encoding);
|
|
461
470
|
}
|
|
@@ -20,14 +20,8 @@ export interface BuildInternals {
|
|
|
20
20
|
pageToBundleMap: Map<string, string>;
|
|
21
21
|
/**
|
|
22
22
|
* A map for page-specific information.
|
|
23
|
-
* // TODO: Remove in Astro 4.0
|
|
24
|
-
* @deprecated
|
|
25
23
|
*/
|
|
26
24
|
pagesByComponent: Map<string, PageBuildData>;
|
|
27
|
-
/**
|
|
28
|
-
* TODO: Use this in Astro 4.0
|
|
29
|
-
*/
|
|
30
|
-
pagesByComponents: Map<string, PageBuildData[]>;
|
|
31
25
|
/**
|
|
32
26
|
* A map for page-specific output.
|
|
33
27
|
*/
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { prependForwardSlash, removeFileExtension } from "../path.js";
|
|
2
|
-
import { routeIsFallback } from "../redirects/helpers.js";
|
|
3
2
|
import { viteID } from "../util.js";
|
|
4
3
|
import {
|
|
5
4
|
ASTRO_PAGE_RESOLVED_MODULE_ID,
|
|
@@ -18,7 +17,6 @@ function createBuildInternals() {
|
|
|
18
17
|
entrySpecifierToBundleMap: /* @__PURE__ */ new Map(),
|
|
19
18
|
pageToBundleMap: /* @__PURE__ */ new Map(),
|
|
20
19
|
pagesByComponent: /* @__PURE__ */ new Map(),
|
|
21
|
-
pagesByComponents: /* @__PURE__ */ new Map(),
|
|
22
20
|
pageOptionsByPage: /* @__PURE__ */ new Map(),
|
|
23
21
|
pagesByViteID: /* @__PURE__ */ new Map(),
|
|
24
22
|
pagesByClientOnly: /* @__PURE__ */ new Map(),
|
|
@@ -35,16 +33,7 @@ function createBuildInternals() {
|
|
|
35
33
|
}
|
|
36
34
|
function trackPageData(internals, component, pageData, componentModuleId, componentURL) {
|
|
37
35
|
pageData.moduleSpecifier = componentModuleId;
|
|
38
|
-
|
|
39
|
-
internals.pagesByComponent.set(component, pageData);
|
|
40
|
-
}
|
|
41
|
-
const list = internals.pagesByComponents.get(component);
|
|
42
|
-
if (list) {
|
|
43
|
-
list.push(pageData);
|
|
44
|
-
internals.pagesByComponents.set(component, list);
|
|
45
|
-
} else {
|
|
46
|
-
internals.pagesByComponents.set(component, [pageData]);
|
|
47
|
-
}
|
|
36
|
+
internals.pagesByComponent.set(component, pageData);
|
|
48
37
|
internals.pagesByViteID.set(viteID(componentURL), pageData);
|
|
49
38
|
}
|
|
50
39
|
function trackClientOnlyPageDatas(internals, pageData, clientOnlys) {
|
|
@@ -105,10 +94,8 @@ function* eachPageData(internals) {
|
|
|
105
94
|
yield* internals.pagesByComponent.values();
|
|
106
95
|
}
|
|
107
96
|
function* eachPageFromAllPages(allPages) {
|
|
108
|
-
for (const [path,
|
|
109
|
-
|
|
110
|
-
yield [path, pageData];
|
|
111
|
-
}
|
|
97
|
+
for (const [path, pageData] of Object.entries(allPages)) {
|
|
98
|
+
yield [path, pageData];
|
|
112
99
|
}
|
|
113
100
|
}
|
|
114
101
|
function* eachPageDataFromEntryPoint(internals) {
|
|
@@ -21,29 +21,15 @@ async function collectPagesData(opts) {
|
|
|
21
21
|
clearInterval(routeCollectionLogTimeout);
|
|
22
22
|
}, 1e4);
|
|
23
23
|
builtPaths.add(route.pathname);
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
});
|
|
34
|
-
} else {
|
|
35
|
-
allPages[route.component] = [
|
|
36
|
-
{
|
|
37
|
-
component: route.component,
|
|
38
|
-
route,
|
|
39
|
-
moduleSpecifier: "",
|
|
40
|
-
styles: [],
|
|
41
|
-
propagatedStyles: /* @__PURE__ */ new Map(),
|
|
42
|
-
propagatedScripts: /* @__PURE__ */ new Map(),
|
|
43
|
-
hoistedScript: void 0
|
|
44
|
-
}
|
|
45
|
-
];
|
|
46
|
-
}
|
|
24
|
+
allPages[route.component] = {
|
|
25
|
+
component: route.component,
|
|
26
|
+
route,
|
|
27
|
+
moduleSpecifier: "",
|
|
28
|
+
styles: [],
|
|
29
|
+
propagatedStyles: /* @__PURE__ */ new Map(),
|
|
30
|
+
propagatedScripts: /* @__PURE__ */ new Map(),
|
|
31
|
+
hoistedScript: void 0
|
|
32
|
+
};
|
|
47
33
|
clearInterval(routeCollectionLogTimeout);
|
|
48
34
|
if (settings.config.output === "static") {
|
|
49
35
|
const html = `${route.pathname}`.replace(/\/?$/, "/index.html");
|
|
@@ -56,29 +42,15 @@ async function collectPagesData(opts) {
|
|
|
56
42
|
}
|
|
57
43
|
continue;
|
|
58
44
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
});
|
|
69
|
-
} else {
|
|
70
|
-
allPages[route.component] = [
|
|
71
|
-
{
|
|
72
|
-
component: route.component,
|
|
73
|
-
route,
|
|
74
|
-
moduleSpecifier: "",
|
|
75
|
-
styles: [],
|
|
76
|
-
propagatedStyles: /* @__PURE__ */ new Map(),
|
|
77
|
-
propagatedScripts: /* @__PURE__ */ new Map(),
|
|
78
|
-
hoistedScript: void 0
|
|
79
|
-
}
|
|
80
|
-
];
|
|
81
|
-
}
|
|
45
|
+
allPages[route.component] = {
|
|
46
|
+
component: route.component,
|
|
47
|
+
route,
|
|
48
|
+
moduleSpecifier: "",
|
|
49
|
+
styles: [],
|
|
50
|
+
propagatedStyles: /* @__PURE__ */ new Map(),
|
|
51
|
+
propagatedScripts: /* @__PURE__ */ new Map(),
|
|
52
|
+
hoistedScript: void 0
|
|
53
|
+
};
|
|
82
54
|
}
|
|
83
55
|
clearInterval(dataCollectionLogTimeout);
|
|
84
56
|
return { assets, allPages };
|
|
@@ -39,14 +39,12 @@ async function viteBuild(opts) {
|
|
|
39
39
|
settings.timer.start("SSR build");
|
|
40
40
|
const pageInput = /* @__PURE__ */ new Set();
|
|
41
41
|
const internals = createBuildInternals();
|
|
42
|
-
for (const [component,
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
pageInput.add(astroModuleId);
|
|
49
|
-
}
|
|
42
|
+
for (const [component, pageData] of Object.entries(allPages)) {
|
|
43
|
+
const astroModuleURL = new URL("./" + component, settings.config.root);
|
|
44
|
+
const astroModuleId = prependForwardSlash(component);
|
|
45
|
+
trackPageData(internals, component, pageData, astroModuleId, astroModuleURL);
|
|
46
|
+
if (!routeIsRedirect(pageData.route)) {
|
|
47
|
+
pageInput.add(astroModuleId);
|
|
50
48
|
}
|
|
51
49
|
}
|
|
52
50
|
if (settings.config?.vite?.build?.emptyOutDir !== false) {
|
|
@@ -108,7 +106,7 @@ async function ssrBuild(opts, internals, input, container) {
|
|
|
108
106
|
const { allPages, settings, viteConfig } = opts;
|
|
109
107
|
const ssr = isServerLikeOutput(settings.config);
|
|
110
108
|
const out = getOutputDirectory(settings.config);
|
|
111
|
-
const routes = Object.values(allPages).
|
|
109
|
+
const routes = Object.values(allPages).flatMap((pageData) => pageData.route);
|
|
112
110
|
const isContentCache = !ssr && settings.config.experimental.contentCollectionCache;
|
|
113
111
|
const { lastVitePlugins, vitePlugins } = await container.runBeforeHook("server", input);
|
|
114
112
|
const viteBuildConfig = {
|
|
@@ -29,7 +29,7 @@ export interface PageBuildData {
|
|
|
29
29
|
sheet: StylesheetAsset;
|
|
30
30
|
}>;
|
|
31
31
|
}
|
|
32
|
-
export type AllPagesData = Record<ComponentPath, PageBuildData
|
|
32
|
+
export type AllPagesData = Record<ComponentPath, PageBuildData>;
|
|
33
33
|
/** Options for the static build */
|
|
34
34
|
export interface StaticBuildOptions {
|
|
35
35
|
allPages: AllPagesData;
|
package/dist/core/constants.js
CHANGED
package/dist/core/dev/dev.js
CHANGED
|
@@ -20,7 +20,7 @@ async function dev(inlineConfig) {
|
|
|
20
20
|
base: restart.container.settings.config.base
|
|
21
21
|
})
|
|
22
22
|
);
|
|
23
|
-
const currentVersion = "3.5.
|
|
23
|
+
const currentVersion = "3.5.6";
|
|
24
24
|
if (currentVersion.includes("-")) {
|
|
25
25
|
logger.warn(null, msg.prerelease({ currentVersion }));
|
|
26
26
|
}
|
|
@@ -8,16 +8,18 @@ type CreateAPIContext = {
|
|
|
8
8
|
props: Record<string, any>;
|
|
9
9
|
adapterName?: string;
|
|
10
10
|
locales: string[] | undefined;
|
|
11
|
+
routingStrategy: 'prefix-always' | 'prefix-other-locales' | undefined;
|
|
12
|
+
defaultLocale: string | undefined;
|
|
11
13
|
};
|
|
12
14
|
/**
|
|
13
15
|
* Creates a context that holds all the information needed to handle an Astro endpoint.
|
|
14
16
|
*
|
|
15
17
|
* @param {CreateAPIContext} payload
|
|
16
18
|
*/
|
|
17
|
-
export declare function createAPIContext({ request, params, site, props, adapterName, locales, }: CreateAPIContext): APIContext;
|
|
19
|
+
export declare function createAPIContext({ request, params, site, props, adapterName, locales, routingStrategy, defaultLocale, }: CreateAPIContext): APIContext;
|
|
18
20
|
type ResponseParameters = ConstructorParameters<typeof Response>;
|
|
19
21
|
export declare class ResponseWithEncoding extends Response {
|
|
20
22
|
constructor(body: ResponseParameters[0], init: ResponseParameters[1], encoding?: BufferEncoding);
|
|
21
23
|
}
|
|
22
|
-
export declare function callEndpoint<MiddlewareResult = Response | EndpointOutput>(mod: EndpointHandler, env: Environment, ctx: RenderContext, onRequest: MiddlewareHandler<MiddlewareResult> | undefined
|
|
24
|
+
export declare function callEndpoint<MiddlewareResult = Response | EndpointOutput>(mod: EndpointHandler, env: Environment, ctx: RenderContext, onRequest: MiddlewareHandler<MiddlewareResult> | undefined): Promise<Response>;
|
|
23
25
|
export {};
|
|
@@ -4,7 +4,11 @@ import { ASTRO_VERSION } from "../constants.js";
|
|
|
4
4
|
import { AstroCookies, attachCookiesToResponse } from "../cookies/index.js";
|
|
5
5
|
import { AstroError, AstroErrorData } from "../errors/index.js";
|
|
6
6
|
import { callMiddleware } from "../middleware/callMiddleware.js";
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
computeCurrentLocale,
|
|
9
|
+
computePreferredLocale,
|
|
10
|
+
computePreferredLocaleList
|
|
11
|
+
} from "../render/context.js";
|
|
8
12
|
import {} from "../render/index.js";
|
|
9
13
|
const encoder = new TextEncoder();
|
|
10
14
|
const clientAddressSymbol = Symbol.for("astro.clientAddress");
|
|
@@ -15,10 +19,13 @@ function createAPIContext({
|
|
|
15
19
|
site,
|
|
16
20
|
props,
|
|
17
21
|
adapterName,
|
|
18
|
-
locales
|
|
22
|
+
locales,
|
|
23
|
+
routingStrategy,
|
|
24
|
+
defaultLocale
|
|
19
25
|
}) {
|
|
20
26
|
let preferredLocale = void 0;
|
|
21
27
|
let preferredLocaleList = void 0;
|
|
28
|
+
let currentLocale = void 0;
|
|
22
29
|
const context = {
|
|
23
30
|
cookies: new AstroCookies(request),
|
|
24
31
|
request,
|
|
@@ -55,6 +62,15 @@ function createAPIContext({
|
|
|
55
62
|
}
|
|
56
63
|
return void 0;
|
|
57
64
|
},
|
|
65
|
+
get currentLocale() {
|
|
66
|
+
if (currentLocale) {
|
|
67
|
+
return currentLocale;
|
|
68
|
+
}
|
|
69
|
+
if (locales) {
|
|
70
|
+
currentLocale = computeCurrentLocale(request, locales, routingStrategy, defaultLocale);
|
|
71
|
+
}
|
|
72
|
+
return currentLocale;
|
|
73
|
+
},
|
|
58
74
|
url: new URL(request.url),
|
|
59
75
|
get clientAddress() {
|
|
60
76
|
if (clientAddressSymbol in request) {
|
|
@@ -106,14 +122,16 @@ class ResponseWithEncoding extends Response {
|
|
|
106
122
|
}
|
|
107
123
|
}
|
|
108
124
|
}
|
|
109
|
-
async function callEndpoint(mod, env, ctx, onRequest
|
|
125
|
+
async function callEndpoint(mod, env, ctx, onRequest) {
|
|
110
126
|
const context = createAPIContext({
|
|
111
127
|
request: ctx.request,
|
|
112
128
|
params: ctx.params,
|
|
113
129
|
props: ctx.props,
|
|
114
130
|
site: env.site,
|
|
115
131
|
adapterName: env.adapterName,
|
|
116
|
-
|
|
132
|
+
routingStrategy: ctx.routingStrategy,
|
|
133
|
+
defaultLocale: ctx.defaultLocale,
|
|
134
|
+
locales: ctx.locales
|
|
117
135
|
});
|
|
118
136
|
let response;
|
|
119
137
|
if (onRequest) {
|
package/dist/core/messages.js
CHANGED
|
@@ -50,7 +50,7 @@ function serverStart({
|
|
|
50
50
|
base,
|
|
51
51
|
isRestart = false
|
|
52
52
|
}) {
|
|
53
|
-
const version = "3.5.
|
|
53
|
+
const version = "3.5.6";
|
|
54
54
|
const localPrefix = `${dim("\u2503")} Local `;
|
|
55
55
|
const networkPrefix = `${dim("\u2503")} Network `;
|
|
56
56
|
const emptyPrefix = " ".repeat(11);
|
|
@@ -235,7 +235,7 @@ function printHelp({
|
|
|
235
235
|
message.push(
|
|
236
236
|
linebreak(),
|
|
237
237
|
` ${bgGreen(black(` ${commandName} `))} ${green(
|
|
238
|
-
`v${"3.5.
|
|
238
|
+
`v${"3.5.6"}`
|
|
239
239
|
)} ${headline}`
|
|
240
240
|
);
|
|
241
241
|
}
|
|
@@ -9,7 +9,9 @@ function createContext({ request, params, userDefinedLocales = [] }) {
|
|
|
9
9
|
params: params ?? {},
|
|
10
10
|
props: {},
|
|
11
11
|
site: void 0,
|
|
12
|
-
locales: userDefinedLocales
|
|
12
|
+
locales: userDefinedLocales,
|
|
13
|
+
defaultLocale: void 0,
|
|
14
|
+
routingStrategy: void 0
|
|
13
15
|
});
|
|
14
16
|
}
|
|
15
17
|
function isLocalsSerializable(value) {
|
package/dist/core/pipeline.js
CHANGED
|
@@ -89,7 +89,9 @@ class Pipeline {
|
|
|
89
89
|
props: renderContext.props,
|
|
90
90
|
site: env.site,
|
|
91
91
|
adapterName: env.adapterName,
|
|
92
|
-
locales: renderContext.locales
|
|
92
|
+
locales: renderContext.locales,
|
|
93
|
+
routingStrategy: renderContext.routingStrategy,
|
|
94
|
+
defaultLocale: renderContext.defaultLocale
|
|
93
95
|
});
|
|
94
96
|
switch (renderContext.route.type) {
|
|
95
97
|
case "page":
|
|
@@ -119,13 +121,7 @@ class Pipeline {
|
|
|
119
121
|
}
|
|
120
122
|
}
|
|
121
123
|
case "endpoint": {
|
|
122
|
-
return await callEndpoint(
|
|
123
|
-
mod,
|
|
124
|
-
env,
|
|
125
|
-
renderContext,
|
|
126
|
-
onRequest,
|
|
127
|
-
renderContext.locales
|
|
128
|
-
);
|
|
124
|
+
return await callEndpoint(mod, env, renderContext, onRequest);
|
|
129
125
|
}
|
|
130
126
|
default:
|
|
131
127
|
throw new Error(`Couldn't find route of type [${renderContext.route.type}]`);
|
|
@@ -16,6 +16,8 @@ export interface RenderContext {
|
|
|
16
16
|
props: Props;
|
|
17
17
|
locals?: object;
|
|
18
18
|
locales: string[] | undefined;
|
|
19
|
+
defaultLocale: string | undefined;
|
|
20
|
+
routingStrategy: 'prefix-always' | 'prefix-other-locales' | undefined;
|
|
19
21
|
}
|
|
20
22
|
export type CreateRenderContextArgs = Partial<Omit<RenderContext, 'params' | 'props' | 'locals'>> & {
|
|
21
23
|
route: RouteData;
|
|
@@ -45,4 +47,5 @@ export declare function parseLocale(header: string): BrowserLocale[];
|
|
|
45
47
|
*/
|
|
46
48
|
export declare function computePreferredLocale(request: Request, locales: string[]): string | undefined;
|
|
47
49
|
export declare function computePreferredLocaleList(request: Request, locales: string[]): string[];
|
|
50
|
+
export declare function computeCurrentLocale(request: Request, locales: string[], routingStrategy: 'prefix-always' | 'prefix-other-locales' | undefined, defaultLocale: string | undefined): undefined | string;
|
|
48
51
|
export {};
|
|
@@ -18,7 +18,9 @@ async function createRenderContext(options) {
|
|
|
18
18
|
pathname,
|
|
19
19
|
params,
|
|
20
20
|
props,
|
|
21
|
-
locales: options.locales
|
|
21
|
+
locales: options.locales,
|
|
22
|
+
routingStrategy: options.routingStrategy,
|
|
23
|
+
defaultLocale: options.defaultLocale
|
|
22
24
|
};
|
|
23
25
|
Object.defineProperty(context, "locals", {
|
|
24
26
|
enumerable: true,
|
|
@@ -124,7 +126,22 @@ function computePreferredLocaleList(request, locales) {
|
|
|
124
126
|
}
|
|
125
127
|
return result;
|
|
126
128
|
}
|
|
129
|
+
function computeCurrentLocale(request, locales, routingStrategy, defaultLocale) {
|
|
130
|
+
const requestUrl = new URL(request.url);
|
|
131
|
+
for (const segment of requestUrl.pathname.split("/")) {
|
|
132
|
+
for (const locale of locales) {
|
|
133
|
+
if (normalizeTheLocale(locale) === normalizeTheLocale(segment)) {
|
|
134
|
+
return locale;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (routingStrategy === "prefix-other-locales") {
|
|
139
|
+
return defaultLocale;
|
|
140
|
+
}
|
|
141
|
+
return void 0;
|
|
142
|
+
}
|
|
127
143
|
export {
|
|
144
|
+
computeCurrentLocale,
|
|
128
145
|
computePreferredLocale,
|
|
129
146
|
computePreferredLocaleList,
|
|
130
147
|
createRenderContext,
|
package/dist/core/render/core.js
CHANGED
|
@@ -43,7 +43,9 @@ async function renderPage({ mod, renderContext, env, cookies }) {
|
|
|
43
43
|
status: renderContext.status ?? 200,
|
|
44
44
|
cookies,
|
|
45
45
|
locals: renderContext.locals ?? {},
|
|
46
|
-
locales: renderContext.locales
|
|
46
|
+
locales: renderContext.locales,
|
|
47
|
+
defaultLocale: renderContext.defaultLocale,
|
|
48
|
+
routingStrategy: renderContext.routingStrategy
|
|
47
49
|
});
|
|
48
50
|
if (mod.frontmatter && typeof mod.frontmatter === "object" && "draft" in mod.frontmatter) {
|
|
49
51
|
env.logger.warn(
|
|
@@ -31,5 +31,7 @@ export interface CreateResultArgs {
|
|
|
31
31
|
locals: App.Locals;
|
|
32
32
|
cookies?: AstroCookies;
|
|
33
33
|
locales: string[] | undefined;
|
|
34
|
+
defaultLocale: string | undefined;
|
|
35
|
+
routingStrategy: 'prefix-always' | 'prefix-other-locales' | undefined;
|
|
34
36
|
}
|
|
35
37
|
export declare function createResult(args: CreateResultArgs): SSRResult;
|
|
@@ -3,7 +3,11 @@ import { renderJSX } from "../../runtime/server/jsx.js";
|
|
|
3
3
|
import { chunkToString } from "../../runtime/server/render/index.js";
|
|
4
4
|
import { AstroCookies } from "../cookies/index.js";
|
|
5
5
|
import { AstroError, AstroErrorData } from "../errors/index.js";
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
computeCurrentLocale,
|
|
8
|
+
computePreferredLocale,
|
|
9
|
+
computePreferredLocaleList
|
|
10
|
+
} from "./context.js";
|
|
7
11
|
const clientAddressSymbol = Symbol.for("astro.clientAddress");
|
|
8
12
|
const responseSentSymbol = Symbol.for("astro.responseSent");
|
|
9
13
|
function getFunctionExpression(slot) {
|
|
@@ -91,6 +95,7 @@ function createResult(args) {
|
|
|
91
95
|
let cookies = args.cookies;
|
|
92
96
|
let preferredLocale = void 0;
|
|
93
97
|
let preferredLocaleList = void 0;
|
|
98
|
+
let currentLocale = void 0;
|
|
94
99
|
const result = {
|
|
95
100
|
styles: args.styles ?? /* @__PURE__ */ new Set(),
|
|
96
101
|
scripts: args.scripts ?? /* @__PURE__ */ new Set(),
|
|
@@ -149,6 +154,23 @@ function createResult(args) {
|
|
|
149
154
|
}
|
|
150
155
|
return void 0;
|
|
151
156
|
},
|
|
157
|
+
get currentLocale() {
|
|
158
|
+
if (currentLocale) {
|
|
159
|
+
return currentLocale;
|
|
160
|
+
}
|
|
161
|
+
if (args.locales) {
|
|
162
|
+
currentLocale = computeCurrentLocale(
|
|
163
|
+
request,
|
|
164
|
+
args.locales,
|
|
165
|
+
args.routingStrategy,
|
|
166
|
+
args.defaultLocale
|
|
167
|
+
);
|
|
168
|
+
if (currentLocale) {
|
|
169
|
+
return currentLocale;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return void 0;
|
|
173
|
+
},
|
|
152
174
|
params,
|
|
153
175
|
props,
|
|
154
176
|
locals,
|
|
@@ -236,7 +236,8 @@ function createRouteManifest({ settings, cwd, fsMod }, logger) {
|
|
|
236
236
|
component,
|
|
237
237
|
generate,
|
|
238
238
|
pathname: pathname || void 0,
|
|
239
|
-
prerender
|
|
239
|
+
prerender,
|
|
240
|
+
fallbackRoutes: []
|
|
240
241
|
});
|
|
241
242
|
}
|
|
242
243
|
});
|
|
@@ -290,7 +291,8 @@ This route collides with: "${collision.component}".`
|
|
|
290
291
|
component,
|
|
291
292
|
generate,
|
|
292
293
|
pathname: pathname || void 0,
|
|
293
|
-
prerender: prerenderInjected ?? prerender
|
|
294
|
+
prerender: prerenderInjected ?? prerender,
|
|
295
|
+
fallbackRoutes: []
|
|
294
296
|
});
|
|
295
297
|
});
|
|
296
298
|
Object.entries(settings.config.redirects).forEach(([from, to]) => {
|
|
@@ -315,7 +317,8 @@ This route collides with: "${collision.component}".`
|
|
|
315
317
|
pathname: pathname || void 0,
|
|
316
318
|
prerender: false,
|
|
317
319
|
redirect: to,
|
|
318
|
-
redirectRoute: routes.find((r) => r.route === to)
|
|
320
|
+
redirectRoute: routes.find((r) => r.route === to),
|
|
321
|
+
fallbackRoutes: []
|
|
319
322
|
};
|
|
320
323
|
const lastSegmentIsDynamic = (r) => !!r.segments.at(-1)?.at(-1)?.dynamic;
|
|
321
324
|
const redirBase = path.posix.dirname(route);
|
|
@@ -414,33 +417,35 @@ This route collides with: "${collision.component}".`
|
|
|
414
417
|
if (!hasRoute) {
|
|
415
418
|
let pathname;
|
|
416
419
|
let route;
|
|
417
|
-
if (fallbackToLocale === i18n.defaultLocale) {
|
|
420
|
+
if (fallbackToLocale === i18n.defaultLocale && i18n.routingStrategy === "prefix-other-locales") {
|
|
418
421
|
if (fallbackToRoute.pathname) {
|
|
419
422
|
pathname = `/${fallbackFromLocale}${fallbackToRoute.pathname}`;
|
|
420
423
|
}
|
|
421
424
|
route = `/${fallbackFromLocale}${fallbackToRoute.route}`;
|
|
422
425
|
} else {
|
|
423
|
-
pathname = fallbackToRoute.pathname?.replace(
|
|
424
|
-
|
|
425
|
-
`/${fallbackFromLocale}`
|
|
426
|
-
);
|
|
427
|
-
route = fallbackToRoute.route.replace(
|
|
428
|
-
`/${fallbackToLocale}`,
|
|
429
|
-
`/${fallbackFromLocale}`
|
|
430
|
-
);
|
|
426
|
+
pathname = fallbackToRoute.pathname?.replace(`/${fallbackToLocale}/`, `/${fallbackFromLocale}/`).replace(`/${fallbackToLocale}`, `/${fallbackFromLocale}`);
|
|
427
|
+
route = fallbackToRoute.route.replace(`/${fallbackToLocale}`, `/${fallbackFromLocale}`).replace(`/${fallbackToLocale}/`, `/${fallbackFromLocale}/`);
|
|
431
428
|
}
|
|
432
429
|
const segments = removeLeadingForwardSlash(route).split(path.posix.sep).filter(Boolean).map((s) => {
|
|
433
430
|
validateSegment(s);
|
|
434
431
|
return getParts(s, route);
|
|
435
432
|
});
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
433
|
+
const generate = getRouteGenerator(segments, config.trailingSlash);
|
|
434
|
+
const index = routes.findIndex((r) => r === fallbackToRoute);
|
|
435
|
+
if (index) {
|
|
436
|
+
const fallbackRoute = {
|
|
437
|
+
...fallbackToRoute,
|
|
438
|
+
pathname,
|
|
439
|
+
route,
|
|
440
|
+
segments,
|
|
441
|
+
generate,
|
|
442
|
+
pattern: getPattern(segments, config, config.trailingSlash),
|
|
443
|
+
type: "fallback",
|
|
444
|
+
fallbackRoutes: []
|
|
445
|
+
};
|
|
446
|
+
const routeData = routes[index];
|
|
447
|
+
routeData.fallbackRoutes.push(fallbackRoute);
|
|
448
|
+
}
|
|
444
449
|
}
|
|
445
450
|
}
|
|
446
451
|
}
|
|
@@ -5,6 +5,9 @@ function serializeRouteData(routeData, trailingSlash) {
|
|
|
5
5
|
generate: void 0,
|
|
6
6
|
pattern: routeData.pattern.source,
|
|
7
7
|
redirectRoute: routeData.redirectRoute ? serializeRouteData(routeData.redirectRoute, trailingSlash) : void 0,
|
|
8
|
+
fallbackRoutes: routeData.fallbackRoutes.map((fallbackRoute) => {
|
|
9
|
+
return serializeRouteData(fallbackRoute, trailingSlash);
|
|
10
|
+
}),
|
|
8
11
|
_meta: { trailingSlash }
|
|
9
12
|
};
|
|
10
13
|
}
|
|
@@ -20,7 +23,10 @@ function deserializeRouteData(rawRouteData) {
|
|
|
20
23
|
segments: rawRouteData.segments,
|
|
21
24
|
prerender: rawRouteData.prerender,
|
|
22
25
|
redirect: rawRouteData.redirect,
|
|
23
|
-
redirectRoute: rawRouteData.redirectRoute ? deserializeRouteData(rawRouteData.redirectRoute) : void 0
|
|
26
|
+
redirectRoute: rawRouteData.redirectRoute ? deserializeRouteData(rawRouteData.redirectRoute) : void 0,
|
|
27
|
+
fallbackRoutes: rawRouteData.fallbackRoutes.map((fallback) => {
|
|
28
|
+
return deserializeRouteData(fallback);
|
|
29
|
+
})
|
|
24
30
|
};
|
|
25
31
|
}
|
|
26
32
|
export {
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
function matchRoute(pathname, manifest) {
|
|
2
|
-
|
|
2
|
+
const decodedPathname = decodeURI(pathname);
|
|
3
|
+
return manifest.routes.find((route) => {
|
|
4
|
+
return route.pattern.test(decodedPathname) || route.fallbackRoutes.some((fallbackRoute) => fallbackRoute.pattern.test(decodedPathname));
|
|
5
|
+
});
|
|
3
6
|
}
|
|
4
7
|
function matchAllRoutes(pathname, manifest) {
|
|
5
8
|
return manifest.routes.filter((route) => route.pattern.test(pathname));
|
|
@@ -16,7 +16,7 @@ function getParams(array) {
|
|
|
16
16
|
}
|
|
17
17
|
function stringifyParams(params, route) {
|
|
18
18
|
const validatedParams = Object.entries(params).reduce((acc, next) => {
|
|
19
|
-
validateGetStaticPathsParameter(next, route.
|
|
19
|
+
validateGetStaticPathsParameter(next, route.route);
|
|
20
20
|
const [key, value] = next;
|
|
21
21
|
if (value !== void 0) {
|
|
22
22
|
acc[key] = typeof value === "string" ? trimSlashes(value) : value.toString();
|
package/dist/i18n/middleware.js
CHANGED
|
@@ -23,7 +23,7 @@ function createI18nMiddleware(i18n, base, trailingSlash) {
|
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
const url = context.url;
|
|
26
|
-
const { locales, defaultLocale, fallback } = i18n;
|
|
26
|
+
const { locales, defaultLocale, fallback, routingStrategy } = i18n;
|
|
27
27
|
const response = await next();
|
|
28
28
|
if (response instanceof Response) {
|
|
29
29
|
const separators = url.pathname.split("/");
|
|
@@ -56,7 +56,7 @@ function createI18nMiddleware(i18n, base, trailingSlash) {
|
|
|
56
56
|
if (urlLocale && fallbackKeys.includes(urlLocale)) {
|
|
57
57
|
const fallbackLocale = fallback[urlLocale];
|
|
58
58
|
let newPathname;
|
|
59
|
-
if (fallbackLocale === defaultLocale) {
|
|
59
|
+
if (fallbackLocale === defaultLocale && routingStrategy === "prefix-other-locales") {
|
|
60
60
|
newPathname = url.pathname.replace(`/${urlLocale}`, ``);
|
|
61
61
|
} else {
|
|
62
62
|
newPathname = url.pathname.replace(`/${urlLocale}`, `/${fallbackLocale}`);
|
|
@@ -139,14 +139,18 @@ async function handleRoute({
|
|
|
139
139
|
prerender: false,
|
|
140
140
|
segments: [],
|
|
141
141
|
type: "fallback",
|
|
142
|
-
route: ""
|
|
142
|
+
route: "",
|
|
143
|
+
fallbackRoutes: []
|
|
143
144
|
};
|
|
144
145
|
renderContext = await createRenderContext({
|
|
145
146
|
request,
|
|
146
147
|
pathname,
|
|
147
148
|
env,
|
|
148
149
|
mod,
|
|
149
|
-
route
|
|
150
|
+
route,
|
|
151
|
+
locales: manifest.i18n?.locales,
|
|
152
|
+
routingStrategy: manifest.i18n?.routingStrategy,
|
|
153
|
+
defaultLocale: manifest.i18n?.defaultLocale
|
|
150
154
|
});
|
|
151
155
|
} else {
|
|
152
156
|
return handle404Response(origin, incomingRequest, incomingResponse);
|
|
@@ -197,7 +201,9 @@ async function handleRoute({
|
|
|
197
201
|
route: options.route,
|
|
198
202
|
mod,
|
|
199
203
|
env,
|
|
200
|
-
locales: i18n
|
|
204
|
+
locales: i18n?.locales,
|
|
205
|
+
routingStrategy: i18n?.routingStrategy,
|
|
206
|
+
defaultLocale: i18n?.defaultLocale
|
|
201
207
|
});
|
|
202
208
|
}
|
|
203
209
|
const onRequest = middleware?.onRequest;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "astro",
|
|
3
|
-
"version": "3.5.
|
|
3
|
+
"version": "3.5.6",
|
|
4
4
|
"description": "Astro is a modern site builder with web best practices, performance, and DX front-of-mind.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": "withastro",
|
|
@@ -164,8 +164,8 @@
|
|
|
164
164
|
"which-pm": "^2.1.1",
|
|
165
165
|
"yargs-parser": "^21.1.1",
|
|
166
166
|
"zod": "^3.22.4",
|
|
167
|
-
"@astrojs/internal-helpers": "0.2.1",
|
|
168
167
|
"@astrojs/markdown-remark": "3.5.0",
|
|
168
|
+
"@astrojs/internal-helpers": "0.2.1",
|
|
169
169
|
"@astrojs/telemetry": "3.0.4"
|
|
170
170
|
},
|
|
171
171
|
"optionalDependencies": {
|