astro 4.4.0 → 4.4.1
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/dist/@types/astro.d.ts +42 -42
- package/dist/assets/build/generate.d.ts +1 -1
- package/dist/assets/build/generate.js +1 -2
- package/dist/cli/add/babel.d.ts +1 -1
- package/dist/cli/add/index.js +1 -1
- package/dist/cli/db/index.js +2 -0
- package/dist/cli/info/index.js +2 -0
- package/dist/cli/preferences/index.js +2 -0
- package/dist/content/types-generator.js +4 -4
- package/dist/core/app/index.js +39 -122
- package/dist/core/app/pipeline.d.ts +7 -0
- package/dist/core/app/pipeline.js +39 -0
- package/dist/core/base-pipeline.d.ts +59 -0
- package/dist/core/base-pipeline.js +27 -0
- package/dist/core/build/generate.d.ts +1 -1
- package/dist/core/build/generate.js +39 -109
- package/dist/core/build/index.js +0 -4
- package/dist/core/build/{buildPipeline.d.ts → pipeline.d.ts} +13 -13
- package/dist/core/build/pipeline.js +180 -0
- package/dist/core/build/types.d.ts +0 -2
- package/dist/core/constants.d.ts +10 -1
- package/dist/core/constants.js +14 -4
- package/dist/core/dev/dev.js +1 -1
- package/dist/core/dev/restart.js +1 -1
- package/dist/core/endpoint/index.d.ts +5 -4
- package/dist/core/endpoint/index.js +7 -34
- package/dist/core/errors/errors-data.d.ts +2 -2
- package/dist/core/messages.js +2 -2
- package/dist/core/middleware/callMiddleware.d.ts +1 -1
- package/dist/core/middleware/callMiddleware.js +2 -9
- package/dist/core/middleware/index.d.ts +2 -2
- package/dist/core/middleware/index.js +74 -9
- package/dist/core/module-loader/vite.js +4 -4
- package/dist/core/preview/index.js +2 -0
- package/dist/core/preview/static-preview-server.js +1 -7
- package/dist/core/redirects/helpers.d.ts +1 -3
- package/dist/core/redirects/helpers.js +0 -29
- package/dist/core/redirects/index.d.ts +2 -1
- package/dist/core/redirects/index.js +3 -3
- package/dist/core/redirects/render.d.ts +2 -0
- package/dist/core/redirects/render.js +33 -0
- package/dist/core/render/index.d.ts +7 -13
- package/dist/core/render/index.js +7 -7
- package/dist/core/render/params-and-props.d.ts +8 -3
- package/dist/core/render/params-and-props.js +24 -16
- package/dist/core/render/result.d.ts +6 -5
- package/dist/core/render/result.js +3 -4
- package/dist/core/render-context.d.ts +32 -0
- package/dist/core/render-context.js +219 -0
- package/dist/core/routing/index.d.ts +0 -1
- package/dist/core/routing/index.js +0 -2
- package/dist/core/routing/params.d.ts +1 -7
- package/dist/core/routing/params.js +0 -15
- package/dist/core/sync/index.js +3 -3
- package/dist/i18n/middleware.d.ts +0 -5
- package/dist/i18n/middleware.js +61 -69
- package/dist/i18n/utils.d.ts +25 -0
- package/dist/{core/render/context.js → i18n/utils.js} +3 -49
- package/dist/prerender/routing.d.ts +1 -1
- package/dist/prerender/routing.js +2 -3
- package/dist/runtime/client/dev-toolbar/apps/astro.js +13 -9
- package/dist/runtime/client/dev-toolbar/apps/audit/a11y.js +2 -2
- package/dist/runtime/server/endpoint.js +2 -2
- package/dist/vite-plugin-astro/hmr.d.ts +1 -0
- package/dist/vite-plugin-astro/hmr.js +7 -4
- package/dist/vite-plugin-astro-server/error.d.ts +2 -2
- package/dist/vite-plugin-astro-server/error.js +2 -5
- package/dist/vite-plugin-astro-server/index.d.ts +0 -6
- package/dist/vite-plugin-astro-server/index.js +0 -19
- package/dist/vite-plugin-astro-server/pipeline.d.ts +19 -0
- package/dist/vite-plugin-astro-server/pipeline.js +117 -0
- package/dist/vite-plugin-astro-server/plugin.js +3 -4
- package/dist/vite-plugin-astro-server/request.d.ts +3 -4
- package/dist/vite-plugin-astro-server/request.js +6 -9
- package/dist/vite-plugin-astro-server/route.d.ts +3 -4
- package/dist/vite-plugin-astro-server/route.js +34 -162
- package/dist/vite-plugin-dev-toolbar/vite-plugin-dev-toolbar.js +3 -3
- package/package.json +3 -8
- package/dist/core/app/ssrPipeline.d.ts +0 -3
- package/dist/core/app/ssrPipeline.js +0 -6
- package/dist/core/build/buildPipeline.js +0 -150
- package/dist/core/pipeline.d.ts +0 -39
- package/dist/core/pipeline.js +0 -107
- package/dist/core/render/context.d.ts +0 -52
- package/dist/core/render/core.d.ts +0 -10
- package/dist/core/render/core.js +0 -65
- package/dist/core/render/environment.d.ts +0 -34
- package/dist/core/render/environment.js +0 -6
- package/dist/runtime/server/consts.d.ts +0 -1
- package/dist/runtime/server/consts.js +0 -4
- package/dist/vite-plugin-astro-server/devPipeline.d.ts +0 -22
- package/dist/vite-plugin-astro-server/devPipeline.js +0 -65
|
@@ -18,7 +18,7 @@ async function handleHotUpdate(ctx, { logger, compile, astroFileToCssAstroDeps,
|
|
|
18
18
|
ctx.server.moduleGraph.invalidateModule(mod);
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
|
-
ctx.server.
|
|
21
|
+
ctx.server.hot.send({ type: "full-reload", path: "*" });
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
}
|
|
@@ -41,8 +41,10 @@ function isStyleOnlyChanged(oldCode, newCode) {
|
|
|
41
41
|
return false;
|
|
42
42
|
const oldStyles = [];
|
|
43
43
|
const newStyles = [];
|
|
44
|
-
oldCode.
|
|
45
|
-
newCode.
|
|
44
|
+
oldCode = oldCode.replace(styleRE, (m) => (oldStyles.push(m), ""));
|
|
45
|
+
newCode = newCode.replace(styleRE, (m) => (newStyles.push(m), ""));
|
|
46
|
+
if (oldCode !== newCode)
|
|
47
|
+
return false;
|
|
46
48
|
return oldStyles.length === newStyles.length && !isArrayEqual(oldStyles, newStyles);
|
|
47
49
|
}
|
|
48
50
|
function isArrayEqual(a, b) {
|
|
@@ -60,5 +62,6 @@ function getShortName(file, root) {
|
|
|
60
62
|
return file.startsWith(appendForwardSlash(root)) ? path.posix.relative(root, file) : file;
|
|
61
63
|
}
|
|
62
64
|
export {
|
|
63
|
-
handleHotUpdate
|
|
65
|
+
handleHotUpdate,
|
|
66
|
+
isStyleOnlyChanged
|
|
64
67
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ModuleLoader } from '../core/module-loader/index.js';
|
|
2
2
|
import type { AstroConfig } from '../@types/astro.js';
|
|
3
|
-
import type DevPipeline from './
|
|
4
|
-
export declare function recordServerError(loader: ModuleLoader, config: AstroConfig,
|
|
3
|
+
import type { DevPipeline } from './pipeline.js';
|
|
4
|
+
export declare function recordServerError(loader: ModuleLoader, config: AstroConfig, { logger }: DevPipeline, _err: unknown): {
|
|
5
5
|
error: Error;
|
|
6
6
|
errorWithMetadata: import("../core/errors/errors.js").ErrorWithMetadata;
|
|
7
7
|
};
|
|
@@ -2,7 +2,7 @@ import { collectErrorMetadata } from "../core/errors/dev/index.js";
|
|
|
2
2
|
import { createSafeError, AstroErrorData } from "../core/errors/index.js";
|
|
3
3
|
import { formatErrorMessage } from "../core/messages.js";
|
|
4
4
|
import { eventError, telemetry } from "../events/index.js";
|
|
5
|
-
function recordServerError(loader, config,
|
|
5
|
+
function recordServerError(loader, config, { logger }, _err) {
|
|
6
6
|
const err = createSafeError(_err);
|
|
7
7
|
try {
|
|
8
8
|
loader.fixStacktrace(err);
|
|
@@ -12,10 +12,7 @@ function recordServerError(loader, config, pipeline, _err) {
|
|
|
12
12
|
if (errorWithMetadata.name !== AstroErrorData.UnhandledRejection.name) {
|
|
13
13
|
telemetry.record(eventError({ cmd: "dev", err: errorWithMetadata, isFatal: false }));
|
|
14
14
|
}
|
|
15
|
-
|
|
16
|
-
null,
|
|
17
|
-
formatErrorMessage(errorWithMetadata, pipeline.logger.level() === "debug")
|
|
18
|
-
);
|
|
15
|
+
logger.error(null, formatErrorMessage(errorWithMetadata, logger.level() === "debug"));
|
|
19
16
|
return {
|
|
20
17
|
error: err,
|
|
21
18
|
errorWithMetadata
|
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
import type { ComponentInstance } from '../@types/astro.js';
|
|
2
|
-
import type DevPipeline from './devPipeline.js';
|
|
3
|
-
export declare function preload({ pipeline, filePath, }: {
|
|
4
|
-
pipeline: DevPipeline;
|
|
5
|
-
filePath: URL;
|
|
6
|
-
}): Promise<ComponentInstance>;
|
|
7
1
|
export { createController, runWithErrorHandling } from './controller.js';
|
|
8
2
|
export { default as vitePluginAstroServer } from './plugin.js';
|
|
9
3
|
export { handleRequest } from './request.js';
|
|
@@ -1,28 +1,9 @@
|
|
|
1
|
-
import { enhanceViteSSRError } from "../core/errors/dev/index.js";
|
|
2
|
-
import { AggregateError, CSSError, MarkdownError } from "../core/errors/index.js";
|
|
3
|
-
import { viteID } from "../core/util.js";
|
|
4
|
-
async function preload({
|
|
5
|
-
pipeline,
|
|
6
|
-
filePath
|
|
7
|
-
}) {
|
|
8
|
-
await pipeline.loadRenderers();
|
|
9
|
-
try {
|
|
10
|
-
const mod = await pipeline.getModuleLoader().import(viteID(filePath));
|
|
11
|
-
return mod;
|
|
12
|
-
} catch (error) {
|
|
13
|
-
if (MarkdownError.is(error) || CSSError.is(error) || AggregateError.is(error)) {
|
|
14
|
-
throw error;
|
|
15
|
-
}
|
|
16
|
-
throw enhanceViteSSRError({ error, filePath, loader: pipeline.getModuleLoader() });
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
1
|
import { createController, runWithErrorHandling } from "./controller.js";
|
|
20
2
|
import { default as default2 } from "./plugin.js";
|
|
21
3
|
import { handleRequest } from "./request.js";
|
|
22
4
|
export {
|
|
23
5
|
createController,
|
|
24
6
|
handleRequest,
|
|
25
|
-
preload,
|
|
26
7
|
runWithErrorHandling,
|
|
27
8
|
default2 as vitePluginAstroServer
|
|
28
9
|
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { AstroSettings, ComponentInstance, RouteData, SSRLoadedRenderer, SSRManifest } from '../@types/astro.js';
|
|
2
|
+
import type { Logger } from '../core/logger/core.js';
|
|
3
|
+
import type { ModuleLoader } from '../core/module-loader/index.js';
|
|
4
|
+
import { Pipeline } from '../core/render/index.js';
|
|
5
|
+
import type { HeadElements } from '../core/base-pipeline.js';
|
|
6
|
+
export declare class DevPipeline extends Pipeline {
|
|
7
|
+
readonly loader: ModuleLoader;
|
|
8
|
+
readonly logger: Logger;
|
|
9
|
+
readonly manifest: SSRManifest;
|
|
10
|
+
readonly settings: AstroSettings;
|
|
11
|
+
readonly config: import("../@types/astro.js").AstroConfig;
|
|
12
|
+
renderers: SSRLoadedRenderer[];
|
|
13
|
+
private constructor();
|
|
14
|
+
static create({ loader, logger, manifest, settings, }: Pick<DevPipeline, 'loader' | 'logger' | 'manifest' | 'settings'>): DevPipeline;
|
|
15
|
+
headElements(routeData: RouteData): Promise<HeadElements>;
|
|
16
|
+
componentMetadata(routeData: RouteData): Promise<Map<string, import("../@types/astro.js").SSRComponentMetadata>>;
|
|
17
|
+
preload(filePath: URL): Promise<ComponentInstance>;
|
|
18
|
+
clearRouteCache(): void;
|
|
19
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import url from "node:url";
|
|
2
|
+
import { Pipeline, loadRenderer } from "../core/render/index.js";
|
|
3
|
+
import { isPage, resolveIdToUrl, viteID } from "../core/util.js";
|
|
4
|
+
import { isServerLikeOutput } from "../prerender/utils.js";
|
|
5
|
+
import { createResolve } from "./resolve.js";
|
|
6
|
+
import { AggregateError, CSSError, MarkdownError } from "../core/errors/index.js";
|
|
7
|
+
import { enhanceViteSSRError } from "../core/errors/dev/index.js";
|
|
8
|
+
import { getScriptsForURL } from "./scripts.js";
|
|
9
|
+
import { ASTRO_VERSION } from "../core/constants.js";
|
|
10
|
+
import { getInfoOutput } from "../cli/info/index.js";
|
|
11
|
+
import { PAGE_SCRIPT_ID } from "../vite-plugin-scripts/index.js";
|
|
12
|
+
import { getStylesForURL } from "./css.js";
|
|
13
|
+
import { getComponentMetadata } from "./metadata.js";
|
|
14
|
+
class DevPipeline extends Pipeline {
|
|
15
|
+
constructor(loader, logger, manifest, settings, config = settings.config) {
|
|
16
|
+
const mode = "development";
|
|
17
|
+
const resolve = createResolve(loader, config.root);
|
|
18
|
+
const serverLike = isServerLikeOutput(config);
|
|
19
|
+
const streaming = true;
|
|
20
|
+
super(logger, manifest, mode, [], resolve, serverLike, streaming);
|
|
21
|
+
this.loader = loader;
|
|
22
|
+
this.logger = logger;
|
|
23
|
+
this.manifest = manifest;
|
|
24
|
+
this.settings = settings;
|
|
25
|
+
this.config = config;
|
|
26
|
+
}
|
|
27
|
+
// renderers are loaded on every request,
|
|
28
|
+
// so it needs to be mutable here unlike in other environments
|
|
29
|
+
renderers = new Array();
|
|
30
|
+
static create({
|
|
31
|
+
loader,
|
|
32
|
+
logger,
|
|
33
|
+
manifest,
|
|
34
|
+
settings
|
|
35
|
+
}) {
|
|
36
|
+
return new DevPipeline(loader, logger, manifest, settings);
|
|
37
|
+
}
|
|
38
|
+
async headElements(routeData) {
|
|
39
|
+
const {
|
|
40
|
+
config: { root },
|
|
41
|
+
loader,
|
|
42
|
+
mode,
|
|
43
|
+
settings
|
|
44
|
+
} = this;
|
|
45
|
+
const filePath = new URL(`./${routeData.component}`, root);
|
|
46
|
+
const { scripts } = await getScriptsForURL(filePath, root, loader);
|
|
47
|
+
if (isPage(filePath, settings) && mode === "development") {
|
|
48
|
+
scripts.add({
|
|
49
|
+
props: { type: "module", src: "/@vite/client" },
|
|
50
|
+
children: ""
|
|
51
|
+
});
|
|
52
|
+
if (settings.config.devToolbar.enabled && await settings.preferences.get("devToolbar.enabled")) {
|
|
53
|
+
const src = await resolveIdToUrl(loader, "astro/runtime/client/dev-toolbar/entrypoint.js");
|
|
54
|
+
scripts.add({ props: { type: "module", src }, children: "" });
|
|
55
|
+
const additionalMetadata = {
|
|
56
|
+
root: url.fileURLToPath(settings.config.root),
|
|
57
|
+
version: ASTRO_VERSION,
|
|
58
|
+
debugInfo: await getInfoOutput({ userConfig: settings.config, print: false })
|
|
59
|
+
};
|
|
60
|
+
const children = `window.__astro_dev_toolbar__ = ${JSON.stringify(additionalMetadata)}`;
|
|
61
|
+
scripts.add({ props: {}, children });
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
for (const script of settings.scripts) {
|
|
65
|
+
if (script.stage === "head-inline") {
|
|
66
|
+
scripts.add({
|
|
67
|
+
props: {},
|
|
68
|
+
children: script.content
|
|
69
|
+
});
|
|
70
|
+
} else if (script.stage === "page" && isPage(filePath, settings)) {
|
|
71
|
+
scripts.add({
|
|
72
|
+
props: { type: "module", src: `/@id/${PAGE_SCRIPT_ID}` },
|
|
73
|
+
children: ""
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const links = /* @__PURE__ */ new Set();
|
|
78
|
+
const { urls, styles: _styles } = await getStylesForURL(filePath, loader);
|
|
79
|
+
for (const href of urls) {
|
|
80
|
+
links.add({ props: { rel: "stylesheet", href }, children: "" });
|
|
81
|
+
}
|
|
82
|
+
const styles = /* @__PURE__ */ new Set();
|
|
83
|
+
for (const { id, url: src, content } of _styles) {
|
|
84
|
+
scripts.add({ props: { type: "module", src }, children: "" });
|
|
85
|
+
styles.add({ props: { "data-vite-dev-id": id }, children: content });
|
|
86
|
+
}
|
|
87
|
+
return { scripts, styles, links };
|
|
88
|
+
}
|
|
89
|
+
componentMetadata(routeData) {
|
|
90
|
+
const {
|
|
91
|
+
config: { root },
|
|
92
|
+
loader
|
|
93
|
+
} = this;
|
|
94
|
+
const filePath = new URL(`./${routeData.component}`, root);
|
|
95
|
+
return getComponentMetadata(filePath, loader);
|
|
96
|
+
}
|
|
97
|
+
async preload(filePath) {
|
|
98
|
+
const { loader } = this;
|
|
99
|
+
const renderers__ = this.settings.renderers.map((r) => loadRenderer(r, loader));
|
|
100
|
+
const renderers_ = await Promise.all(renderers__);
|
|
101
|
+
this.renderers = renderers_.filter((r) => Boolean(r));
|
|
102
|
+
try {
|
|
103
|
+
return await loader.import(viteID(filePath));
|
|
104
|
+
} catch (error) {
|
|
105
|
+
if (MarkdownError.is(error) || CSSError.is(error) || AggregateError.is(error)) {
|
|
106
|
+
throw error;
|
|
107
|
+
}
|
|
108
|
+
throw enhanceViteSSRError({ error, filePath, loader });
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
clearRouteCache() {
|
|
112
|
+
this.routeCache.clearAll();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
export {
|
|
116
|
+
DevPipeline
|
|
117
|
+
};
|
|
@@ -3,7 +3,7 @@ import { createViteLoader } from "../core/module-loader/index.js";
|
|
|
3
3
|
import { createRouteManifest } from "../core/routing/index.js";
|
|
4
4
|
import { baseMiddleware } from "./base.js";
|
|
5
5
|
import { createController } from "./controller.js";
|
|
6
|
-
import DevPipeline from "./
|
|
6
|
+
import { DevPipeline } from "./pipeline.js";
|
|
7
7
|
import { handleRequest } from "./request.js";
|
|
8
8
|
import { AstroError, AstroErrorData } from "../core/errors/index.js";
|
|
9
9
|
import { getViteErrorPayload } from "../core/errors/dev/index.js";
|
|
@@ -21,7 +21,7 @@ function createVitePluginAstroServer({
|
|
|
21
21
|
configureServer(viteServer) {
|
|
22
22
|
const loader = createViteLoader(viteServer);
|
|
23
23
|
const manifest = createDevelopmentManifest(settings);
|
|
24
|
-
const pipeline =
|
|
24
|
+
const pipeline = DevPipeline.create({ loader, logger, manifest, settings });
|
|
25
25
|
let manifestData = createRouteManifest({ settings, fsMod }, logger);
|
|
26
26
|
const controller = createController({ loader });
|
|
27
27
|
const localStorage = new AsyncLocalStorage();
|
|
@@ -68,8 +68,7 @@ function createVitePluginAstroServer({
|
|
|
68
68
|
manifestData,
|
|
69
69
|
controller,
|
|
70
70
|
incomingRequest: request,
|
|
71
|
-
incomingResponse: response
|
|
72
|
-
manifest
|
|
71
|
+
incomingResponse: response
|
|
73
72
|
});
|
|
74
73
|
});
|
|
75
74
|
});
|
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
/// <reference types="node" resolution-mode="require"/>
|
|
2
2
|
import type http from 'node:http';
|
|
3
|
-
import type { ManifestData
|
|
3
|
+
import type { ManifestData } from '../@types/astro.js';
|
|
4
4
|
import type { DevServerController } from './controller.js';
|
|
5
|
-
import type DevPipeline from './
|
|
5
|
+
import type { DevPipeline } from './pipeline.js';
|
|
6
6
|
type HandleRequest = {
|
|
7
7
|
pipeline: DevPipeline;
|
|
8
8
|
manifestData: ManifestData;
|
|
9
9
|
controller: DevServerController;
|
|
10
10
|
incomingRequest: http.IncomingMessage;
|
|
11
11
|
incomingResponse: http.ServerResponse;
|
|
12
|
-
manifest: SSRManifest;
|
|
13
12
|
};
|
|
14
13
|
/** The main logic to route dev server requests to pages in Astro. */
|
|
15
|
-
export declare function handleRequest({ pipeline, manifestData, controller, incomingRequest, incomingResponse,
|
|
14
|
+
export declare function handleRequest({ pipeline, manifestData, controller, incomingRequest, incomingResponse, }: HandleRequest): Promise<void>;
|
|
16
15
|
export {};
|
|
@@ -9,12 +9,10 @@ async function handleRequest({
|
|
|
9
9
|
manifestData,
|
|
10
10
|
controller,
|
|
11
11
|
incomingRequest,
|
|
12
|
-
incomingResponse
|
|
13
|
-
manifest
|
|
12
|
+
incomingResponse
|
|
14
13
|
}) {
|
|
15
|
-
const config = pipeline
|
|
16
|
-
const
|
|
17
|
-
const origin = `${moduleLoader.isHttps() ? "https" : "http"}://${incomingRequest.headers.host}`;
|
|
14
|
+
const { config, loader } = pipeline;
|
|
15
|
+
const origin = `${loader.isHttps() ? "https" : "http"}://${incomingRequest.headers.host}`;
|
|
18
16
|
const buildingToSSR = isServerLikeOutput(config);
|
|
19
17
|
const url = new URL(collapseDuplicateSlashes(origin + incomingRequest.url));
|
|
20
18
|
let pathname;
|
|
@@ -56,13 +54,12 @@ async function handleRequest({
|
|
|
56
54
|
pipeline,
|
|
57
55
|
manifestData,
|
|
58
56
|
incomingRequest,
|
|
59
|
-
incomingResponse
|
|
60
|
-
manifest
|
|
57
|
+
incomingResponse
|
|
61
58
|
});
|
|
62
59
|
},
|
|
63
60
|
onError(_err) {
|
|
64
|
-
const { error, errorWithMetadata } = recordServerError(
|
|
65
|
-
handle500Response(
|
|
61
|
+
const { error, errorWithMetadata } = recordServerError(loader, config, pipeline, _err);
|
|
62
|
+
handle500Response(loader, incomingResponse, errorWithMetadata);
|
|
66
63
|
return error;
|
|
67
64
|
}
|
|
68
65
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" resolution-mode="require"/>
|
|
2
2
|
import type http from 'node:http';
|
|
3
|
-
import type { ComponentInstance, ManifestData, RouteData
|
|
4
|
-
import type DevPipeline from './
|
|
3
|
+
import type { ComponentInstance, ManifestData, RouteData } from '../@types/astro.js';
|
|
4
|
+
import type { DevPipeline } from './pipeline.js';
|
|
5
5
|
type AsyncReturnType<T extends (...args: any) => Promise<any>> = T extends (...args: any) => Promise<infer R> ? R : any;
|
|
6
6
|
export interface MatchedRoute {
|
|
7
7
|
route: RouteData;
|
|
@@ -20,9 +20,8 @@ type HandleRoute = {
|
|
|
20
20
|
manifestData: ManifestData;
|
|
21
21
|
incomingRequest: http.IncomingMessage;
|
|
22
22
|
incomingResponse: http.ServerResponse;
|
|
23
|
-
manifest: SSRManifest;
|
|
24
23
|
status?: 404 | 500;
|
|
25
24
|
pipeline: DevPipeline;
|
|
26
25
|
};
|
|
27
|
-
export declare function handleRoute({ matchedRoute, url, pathname, status, body, origin, pipeline, manifestData, incomingRequest, incomingResponse,
|
|
26
|
+
export declare function handleRoute({ matchedRoute, url, pathname, status, body, origin, pipeline, manifestData, incomingRequest, incomingResponse, }: HandleRoute): Promise<void>;
|
|
28
27
|
export {};
|
|
@@ -1,29 +1,15 @@
|
|
|
1
|
-
import { fileURLToPath } from "node:url";
|
|
2
|
-
import { getInfoOutput } from "../cli/info/index.js";
|
|
3
|
-
import { ASTRO_VERSION } from "../core/constants.js";
|
|
4
1
|
import { AstroErrorData, isAstroError } from "../core/errors/index.js";
|
|
5
2
|
import { req } from "../core/messages.js";
|
|
6
|
-
import { sequence } from "../core/middleware/index.js";
|
|
7
3
|
import { loadMiddleware } from "../core/middleware/loadMiddleware.js";
|
|
8
|
-
import {
|
|
9
|
-
createRenderContext,
|
|
10
|
-
getParamsAndProps
|
|
11
|
-
} from "../core/render/index.js";
|
|
4
|
+
import { getProps } from "../core/render/index.js";
|
|
12
5
|
import { createRequest } from "../core/request.js";
|
|
13
6
|
import { matchAllRoutes } from "../core/routing/index.js";
|
|
14
|
-
import { isPage, resolveIdToUrl } from "../core/util.js";
|
|
15
7
|
import { normalizeTheLocale } from "../i18n/index.js";
|
|
16
|
-
import { createI18nMiddleware, i18nPipelineHook } from "../i18n/middleware.js";
|
|
17
8
|
import { getSortedPreloadedMatches } from "../prerender/routing.js";
|
|
18
9
|
import { isServerLikeOutput } from "../prerender/utils.js";
|
|
19
|
-
import { PAGE_SCRIPT_ID } from "../vite-plugin-scripts/index.js";
|
|
20
|
-
import { getStylesForURL } from "./css.js";
|
|
21
|
-
import { preload } from "./index.js";
|
|
22
|
-
import { getComponentMetadata } from "./metadata.js";
|
|
23
10
|
import { handle404Response, writeSSRResult, writeWebResponse } from "./response.js";
|
|
24
|
-
import {
|
|
25
|
-
import {
|
|
26
|
-
const clientLocalsSymbol = Symbol.for("astro.locals");
|
|
11
|
+
import { REROUTE_DIRECTIVE_HEADER, clientLocalsSymbol } from "../core/constants.js";
|
|
12
|
+
import { RenderContext } from "../core/render-context.js";
|
|
27
13
|
function isLoggedRequest(url) {
|
|
28
14
|
return url !== "/favicon.ico";
|
|
29
15
|
}
|
|
@@ -32,23 +18,18 @@ function getCustom404Route(manifestData) {
|
|
|
32
18
|
return manifestData.routes.find((r) => route404.test(r.route));
|
|
33
19
|
}
|
|
34
20
|
async function matchRoute(pathname, manifestData, pipeline) {
|
|
35
|
-
const
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
const preloadedMatches = await getSortedPreloadedMatches({
|
|
39
|
-
pipeline,
|
|
40
|
-
matches,
|
|
41
|
-
settings: pipeline.getSettings()
|
|
42
|
-
});
|
|
21
|
+
const { config, logger, routeCache, serverLike, settings } = pipeline;
|
|
22
|
+
const matches = matchAllRoutes(pathname, manifestData);
|
|
23
|
+
const preloadedMatches = await getSortedPreloadedMatches({ pipeline, matches, settings });
|
|
43
24
|
for await (const { preloadedComponent, route: maybeRoute, filePath } of preloadedMatches) {
|
|
44
25
|
try {
|
|
45
|
-
await
|
|
26
|
+
await getProps({
|
|
46
27
|
mod: preloadedComponent,
|
|
47
|
-
|
|
28
|
+
routeData: maybeRoute,
|
|
48
29
|
routeCache,
|
|
49
30
|
pathname,
|
|
50
31
|
logger,
|
|
51
|
-
|
|
32
|
+
serverLike
|
|
52
33
|
});
|
|
53
34
|
return {
|
|
54
35
|
route: maybeRoute,
|
|
@@ -70,7 +51,7 @@ async function matchRoute(pathname, manifestData, pipeline) {
|
|
|
70
51
|
}
|
|
71
52
|
if (matches.length) {
|
|
72
53
|
const possibleRoutes = matches.flatMap((route) => route.component);
|
|
73
|
-
|
|
54
|
+
logger.warn(
|
|
74
55
|
"router",
|
|
75
56
|
`${AstroErrorData.NoMatchingStaticPathFound.message(
|
|
76
57
|
pathname
|
|
@@ -81,8 +62,8 @@ ${AstroErrorData.NoMatchingStaticPathFound.hint(possibleRoutes)}`
|
|
|
81
62
|
}
|
|
82
63
|
const custom404 = getCustom404Route(manifestData);
|
|
83
64
|
if (custom404) {
|
|
84
|
-
const filePath = new URL(`./${custom404.component}`,
|
|
85
|
-
const preloadedComponent = await preload(
|
|
65
|
+
const filePath = new URL(`./${custom404.component}`, config.root);
|
|
66
|
+
const preloadedComponent = await pipeline.preload(filePath);
|
|
86
67
|
return {
|
|
87
68
|
route: custom404,
|
|
88
69
|
filePath,
|
|
@@ -103,14 +84,10 @@ async function handleRoute({
|
|
|
103
84
|
pipeline,
|
|
104
85
|
manifestData,
|
|
105
86
|
incomingRequest,
|
|
106
|
-
incomingResponse
|
|
107
|
-
manifest
|
|
87
|
+
incomingResponse
|
|
108
88
|
}) {
|
|
109
89
|
const timeStart = performance.now();
|
|
110
|
-
const
|
|
111
|
-
const config = pipeline.getConfig();
|
|
112
|
-
const moduleLoader = pipeline.getModuleLoader();
|
|
113
|
-
const { logger } = env;
|
|
90
|
+
const { config, loader, logger } = pipeline;
|
|
114
91
|
if (!matchedRoute && !config.i18n) {
|
|
115
92
|
if (isLoggedRequest(pathname)) {
|
|
116
93
|
logger.info(null, req({ url: pathname, method: incomingRequest.method, statusCode: 404 }));
|
|
@@ -123,7 +100,7 @@ async function handleRoute({
|
|
|
123
100
|
let mod = void 0;
|
|
124
101
|
let options = void 0;
|
|
125
102
|
let route;
|
|
126
|
-
const middleware = await loadMiddleware(
|
|
103
|
+
const middleware = (await loadMiddleware(loader)).onRequest;
|
|
127
104
|
if (!matchedRoute) {
|
|
128
105
|
if (config.i18n) {
|
|
129
106
|
const locales = config.i18n.locales;
|
|
@@ -169,15 +146,12 @@ async function handleRoute({
|
|
|
169
146
|
fallbackRoutes: [],
|
|
170
147
|
isIndex: false
|
|
171
148
|
};
|
|
172
|
-
renderContext =
|
|
173
|
-
|
|
149
|
+
renderContext = RenderContext.create({
|
|
150
|
+
pipeline,
|
|
174
151
|
pathname,
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
route
|
|
178
|
-
locales: manifest.i18n?.locales,
|
|
179
|
-
routing: manifest.i18n?.routing,
|
|
180
|
-
defaultLocale: manifest.i18n?.defaultLocale
|
|
152
|
+
middleware,
|
|
153
|
+
request,
|
|
154
|
+
routeData: route
|
|
181
155
|
});
|
|
182
156
|
} else {
|
|
183
157
|
return handle404Response(origin, incomingRequest, incomingResponse);
|
|
@@ -186,69 +160,40 @@ async function handleRoute({
|
|
|
186
160
|
const filePath = matchedRoute.filePath;
|
|
187
161
|
const { preloadedComponent } = matchedRoute;
|
|
188
162
|
route = matchedRoute.route;
|
|
163
|
+
const locals = Reflect.get(incomingRequest, clientLocalsSymbol);
|
|
189
164
|
request = createRequest({
|
|
190
165
|
url,
|
|
166
|
+
// Headers are only available when using SSR.
|
|
191
167
|
headers: buildingToSSR ? incomingRequest.headers : new Headers(),
|
|
192
168
|
method: incomingRequest.method,
|
|
193
169
|
body,
|
|
194
170
|
logger,
|
|
195
171
|
ssr: buildingToSSR,
|
|
196
|
-
clientAddress: buildingToSSR ? incomingRequest.socket.remoteAddress : void 0
|
|
197
|
-
locals: Reflect.get(incomingRequest, clientLocalsSymbol)
|
|
198
|
-
// Allows adapters to pass in locals in dev mode.
|
|
172
|
+
clientAddress: buildingToSSR ? incomingRequest.socket.remoteAddress : void 0
|
|
199
173
|
});
|
|
200
174
|
for (const [name, value] of Object.entries(config.server.headers ?? {})) {
|
|
201
175
|
if (value)
|
|
202
176
|
incomingResponse.setHeader(name, value);
|
|
203
177
|
}
|
|
204
178
|
options = {
|
|
205
|
-
|
|
179
|
+
pipeline,
|
|
206
180
|
filePath,
|
|
207
181
|
preload: preloadedComponent,
|
|
208
182
|
pathname,
|
|
209
183
|
request,
|
|
210
|
-
route
|
|
211
|
-
middleware
|
|
184
|
+
route
|
|
212
185
|
};
|
|
213
|
-
mod =
|
|
214
|
-
|
|
186
|
+
mod = preloadedComponent;
|
|
187
|
+
renderContext = RenderContext.create({
|
|
188
|
+
locals,
|
|
215
189
|
pipeline,
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
request: options.request,
|
|
221
|
-
pathname: options.pathname,
|
|
222
|
-
scripts,
|
|
223
|
-
links,
|
|
224
|
-
styles,
|
|
225
|
-
componentMetadata: metadata,
|
|
226
|
-
route: options.route,
|
|
227
|
-
mod,
|
|
228
|
-
env,
|
|
229
|
-
locales: i18n?.locales,
|
|
230
|
-
routing: i18n?.routing,
|
|
231
|
-
defaultLocale: i18n?.defaultLocale
|
|
190
|
+
pathname,
|
|
191
|
+
middleware,
|
|
192
|
+
request,
|
|
193
|
+
routeData: route
|
|
232
194
|
});
|
|
233
195
|
}
|
|
234
|
-
|
|
235
|
-
if (config.i18n) {
|
|
236
|
-
const i18Middleware = createI18nMiddleware(
|
|
237
|
-
manifest.i18n,
|
|
238
|
-
config.base,
|
|
239
|
-
config.trailingSlash,
|
|
240
|
-
config.build.format
|
|
241
|
-
);
|
|
242
|
-
if (i18Middleware) {
|
|
243
|
-
pipeline.setMiddlewareFunction(sequence(i18Middleware, onRequest));
|
|
244
|
-
pipeline.onBeforeRenderRoute(i18nPipelineHook);
|
|
245
|
-
} else {
|
|
246
|
-
pipeline.setMiddlewareFunction(onRequest);
|
|
247
|
-
}
|
|
248
|
-
} else {
|
|
249
|
-
pipeline.setMiddlewareFunction(onRequest);
|
|
250
|
-
}
|
|
251
|
-
let response = await pipeline.renderRoute(renderContext, mod);
|
|
196
|
+
let response = await renderContext.render(mod);
|
|
252
197
|
if (isLoggedRequest(pathname)) {
|
|
253
198
|
const timeEnd = performance.now();
|
|
254
199
|
logger.info(
|
|
@@ -274,8 +219,7 @@ async function handleRoute({
|
|
|
274
219
|
pipeline,
|
|
275
220
|
manifestData,
|
|
276
221
|
incomingRequest,
|
|
277
|
-
incomingResponse
|
|
278
|
-
manifest
|
|
222
|
+
incomingResponse
|
|
279
223
|
});
|
|
280
224
|
}
|
|
281
225
|
if (route.type === "endpoint") {
|
|
@@ -294,78 +238,6 @@ async function handleRoute({
|
|
|
294
238
|
}
|
|
295
239
|
await writeSSRResult(request, response, incomingResponse);
|
|
296
240
|
}
|
|
297
|
-
async function getScriptsAndStyles({ pipeline, filePath }) {
|
|
298
|
-
const moduleLoader = pipeline.getModuleLoader();
|
|
299
|
-
const settings = pipeline.getSettings();
|
|
300
|
-
const mode = pipeline.getEnvironment().mode;
|
|
301
|
-
const { scripts } = await getScriptsForURL(filePath, settings.config.root, moduleLoader);
|
|
302
|
-
if (isPage(filePath, settings) && mode === "development") {
|
|
303
|
-
scripts.add({
|
|
304
|
-
props: { type: "module", src: "/@vite/client" },
|
|
305
|
-
children: ""
|
|
306
|
-
});
|
|
307
|
-
if (settings.config.devToolbar.enabled && await settings.preferences.get("devToolbar.enabled")) {
|
|
308
|
-
scripts.add({
|
|
309
|
-
props: {
|
|
310
|
-
type: "module",
|
|
311
|
-
src: await resolveIdToUrl(moduleLoader, "astro/runtime/client/dev-toolbar/entrypoint.js")
|
|
312
|
-
},
|
|
313
|
-
children: ""
|
|
314
|
-
});
|
|
315
|
-
const additionalMetadata = {
|
|
316
|
-
root: fileURLToPath(settings.config.root),
|
|
317
|
-
version: ASTRO_VERSION,
|
|
318
|
-
debugInfo: await getInfoOutput({ userConfig: settings.config, print: false })
|
|
319
|
-
};
|
|
320
|
-
scripts.add({
|
|
321
|
-
props: {},
|
|
322
|
-
children: `window.__astro_dev_toolbar__ = ${JSON.stringify(additionalMetadata)}`
|
|
323
|
-
});
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
for (const script of settings.scripts) {
|
|
327
|
-
if (script.stage === "head-inline") {
|
|
328
|
-
scripts.add({
|
|
329
|
-
props: {},
|
|
330
|
-
children: script.content
|
|
331
|
-
});
|
|
332
|
-
} else if (script.stage === "page" && isPage(filePath, settings)) {
|
|
333
|
-
scripts.add({
|
|
334
|
-
props: { type: "module", src: `/@id/${PAGE_SCRIPT_ID}` },
|
|
335
|
-
children: ""
|
|
336
|
-
});
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
const { urls: styleUrls, styles: importedStyles } = await getStylesForURL(filePath, moduleLoader);
|
|
340
|
-
let links = /* @__PURE__ */ new Set();
|
|
341
|
-
[...styleUrls].forEach((href) => {
|
|
342
|
-
links.add({
|
|
343
|
-
props: {
|
|
344
|
-
rel: "stylesheet",
|
|
345
|
-
href
|
|
346
|
-
},
|
|
347
|
-
children: ""
|
|
348
|
-
});
|
|
349
|
-
});
|
|
350
|
-
let styles = /* @__PURE__ */ new Set();
|
|
351
|
-
importedStyles.forEach(({ id, url, content }) => {
|
|
352
|
-
scripts.add({
|
|
353
|
-
props: {
|
|
354
|
-
type: "module",
|
|
355
|
-
src: url
|
|
356
|
-
},
|
|
357
|
-
children: ""
|
|
358
|
-
});
|
|
359
|
-
styles.add({
|
|
360
|
-
props: {
|
|
361
|
-
"data-vite-dev-id": id
|
|
362
|
-
},
|
|
363
|
-
children: content
|
|
364
|
-
});
|
|
365
|
-
});
|
|
366
|
-
const metadata = await getComponentMetadata(filePath, moduleLoader);
|
|
367
|
-
return { scripts, styles, links, metadata };
|
|
368
|
-
}
|
|
369
241
|
function getStatus(matchedRoute) {
|
|
370
242
|
if (!matchedRoute)
|
|
371
243
|
return 404;
|
|
@@ -20,20 +20,20 @@ function astroDevToolbar({ settings, logger }) {
|
|
|
20
20
|
}
|
|
21
21
|
},
|
|
22
22
|
configureServer(server) {
|
|
23
|
-
server.
|
|
23
|
+
server.hot.on("astro:devtoolbar:error:load", (args) => {
|
|
24
24
|
logger.error(
|
|
25
25
|
"toolbar",
|
|
26
26
|
`Failed to load dev toolbar app from ${args.entrypoint}: ${args.error}`
|
|
27
27
|
);
|
|
28
28
|
});
|
|
29
|
-
server.
|
|
29
|
+
server.hot.on("astro:devtoolbar:error:init", (args) => {
|
|
30
30
|
logger.error(
|
|
31
31
|
"toolbar",
|
|
32
32
|
`Failed to initialize dev toolbar app ${args.app.name} (${args.app.id}):
|
|
33
33
|
${args.error}`
|
|
34
34
|
);
|
|
35
35
|
});
|
|
36
|
-
server.
|
|
36
|
+
server.hot.on("astro:devtoolbar:app:toggled", (args) => {
|
|
37
37
|
clearTimeout(telemetryTimeout);
|
|
38
38
|
telemetryTimeout = setTimeout(() => {
|
|
39
39
|
let nameToRecord = args?.app?.id;
|