astro 4.7.1 → 4.8.0

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.
Files changed (222) hide show
  1. package/client.d.ts +1 -0
  2. package/content-module.template.mjs +2 -0
  3. package/dist/@types/astro.d.ts +219 -3
  4. package/dist/actions/consts.d.ts +3 -0
  5. package/dist/actions/consts.js +8 -0
  6. package/dist/actions/index.d.ts +2 -0
  7. package/dist/actions/index.js +72 -0
  8. package/dist/actions/runtime/middleware.d.ts +7 -0
  9. package/dist/actions/runtime/middleware.js +38 -0
  10. package/dist/actions/runtime/route.d.ts +2 -0
  11. package/dist/actions/runtime/route.js +37 -0
  12. package/dist/actions/runtime/store.d.ts +6 -0
  13. package/dist/actions/runtime/store.js +18 -0
  14. package/dist/actions/runtime/utils.d.ts +4 -0
  15. package/dist/actions/runtime/utils.js +23 -0
  16. package/dist/actions/runtime/virtual/client.d.ts +4 -0
  17. package/dist/actions/runtime/virtual/client.js +20 -0
  18. package/dist/actions/runtime/virtual/server.d.ts +21 -0
  19. package/dist/actions/runtime/virtual/server.js +98 -0
  20. package/dist/actions/runtime/virtual/shared.d.ts +37 -0
  21. package/dist/actions/runtime/virtual/shared.js +104 -0
  22. package/dist/actions/utils.d.ts +2 -0
  23. package/dist/actions/utils.js +18 -0
  24. package/dist/assets/build/generate.js +1 -1
  25. package/dist/assets/internal.js +1 -2
  26. package/dist/assets/services/service.js +2 -4
  27. package/dist/assets/services/sharp.js +2 -4
  28. package/dist/assets/services/squoosh.js +2 -4
  29. package/dist/assets/services/vendor/squoosh/avif/avif_node_dec.js +34 -68
  30. package/dist/assets/services/vendor/squoosh/avif/avif_node_enc.js +39 -78
  31. package/dist/assets/services/vendor/squoosh/mozjpeg/mozjpeg_node_dec.js +32 -64
  32. package/dist/assets/services/vendor/squoosh/mozjpeg/mozjpeg_node_enc.js +32 -64
  33. package/dist/assets/services/vendor/squoosh/png/squoosh_png.js +2 -4
  34. package/dist/assets/services/vendor/squoosh/webp/webp_node_dec.js +29 -58
  35. package/dist/assets/services/vendor/squoosh/webp/webp_node_enc.js +29 -58
  36. package/dist/assets/utils/getAssetsPrefix.js +2 -4
  37. package/dist/assets/utils/remotePattern.js +1 -2
  38. package/dist/assets/utils/remoteProbe.js +1 -2
  39. package/dist/assets/utils/vendor/image-size/types/cur.js +1 -2
  40. package/dist/assets/utils/vendor/image-size/types/icns.js +1 -2
  41. package/dist/assets/utils/vendor/image-size/types/ico.js +2 -4
  42. package/dist/assets/utils/vendor/image-size/types/jp2.js +2 -4
  43. package/dist/assets/utils/vendor/image-size/types/utils.js +4 -8
  44. package/dist/assets/vite-plugin-assets.js +1 -1
  45. package/dist/cli/add/babel.d.ts +1 -1
  46. package/dist/cli/add/imports.js +4 -8
  47. package/dist/cli/add/index.js +23 -46
  48. package/dist/cli/add/wrapper.js +1 -2
  49. package/dist/cli/index.js +1 -2
  50. package/dist/cli/info/index.js +1 -2
  51. package/dist/cli/install-package.js +3 -6
  52. package/dist/cli/throw-and-exit.js +1 -2
  53. package/dist/config/index.d.ts +2 -2
  54. package/dist/config/index.js +2 -2
  55. package/dist/content/index.d.ts +1 -1
  56. package/dist/content/index.js +1 -7
  57. package/dist/content/runtime.d.ts +2 -1
  58. package/dist/content/runtime.js +11 -20
  59. package/dist/content/server-listeners.js +5 -10
  60. package/dist/content/types-generator.js +5 -10
  61. package/dist/content/utils.d.ts +0 -4
  62. package/dist/content/utils.js +4 -15
  63. package/dist/content/vite-plugin-content-assets.d.ts +1 -1
  64. package/dist/content/vite-plugin-content-assets.js +14 -47
  65. package/dist/content/vite-plugin-content-imports.js +6 -11
  66. package/dist/content/vite-plugin-content-virtual-mod.js +7 -14
  67. package/dist/core/app/index.js +11 -46
  68. package/dist/core/app/node.js +4 -3
  69. package/dist/core/app/pipeline.d.ts +7 -2
  70. package/dist/core/app/pipeline.js +70 -2
  71. package/dist/core/app/types.d.ts +1 -0
  72. package/dist/core/base-pipeline.d.ts +16 -1
  73. package/dist/core/build/generate.js +15 -61
  74. package/dist/core/build/index.js +2 -4
  75. package/dist/core/build/internal.d.ts +39 -9
  76. package/dist/core/build/internal.js +43 -54
  77. package/dist/core/build/page-data.js +6 -6
  78. package/dist/core/build/pipeline.d.ts +7 -3
  79. package/dist/core/build/pipeline.js +134 -23
  80. package/dist/core/build/plugins/plugin-analyzer.js +11 -32
  81. package/dist/core/build/plugins/plugin-content.d.ts +1 -0
  82. package/dist/core/build/plugins/plugin-content.js +34 -32
  83. package/dist/core/build/plugins/plugin-css.js +23 -51
  84. package/dist/core/build/plugins/plugin-manifest.js +7 -8
  85. package/dist/core/build/plugins/plugin-pages.d.ts +0 -1
  86. package/dist/core/build/plugins/plugin-pages.js +10 -12
  87. package/dist/core/build/plugins/plugin-ssr.js +15 -13
  88. package/dist/core/build/plugins/util.d.ts +26 -11
  89. package/dist/core/build/plugins/util.js +22 -6
  90. package/dist/core/build/static-build.js +30 -25
  91. package/dist/core/build/types.d.ts +6 -6
  92. package/dist/core/client-directive/build.js +1 -2
  93. package/dist/core/config/config.js +2 -7
  94. package/dist/core/config/logging.js +1 -2
  95. package/dist/core/config/schema.d.ts +92 -60
  96. package/dist/core/config/schema.js +6 -2
  97. package/dist/core/config/settings.js +1 -2
  98. package/dist/core/config/timer.js +4 -8
  99. package/dist/core/constants.js +1 -1
  100. package/dist/core/cookies/cookies.js +3 -6
  101. package/dist/core/dev/dev.js +1 -1
  102. package/dist/core/dev/restart.js +1 -2
  103. package/dist/core/errors/errors-data.d.ts +24 -0
  104. package/dist/core/errors/errors-data.js +13 -2
  105. package/dist/core/errors/errors.js +1 -2
  106. package/dist/core/errors/overlay.js +1 -2
  107. package/dist/core/errors/printer.js +2 -4
  108. package/dist/core/errors/zod-error-map.js +2 -4
  109. package/dist/core/fs/index.js +2 -4
  110. package/dist/core/logger/vite.js +9 -18
  111. package/dist/core/messages.js +2 -2
  112. package/dist/core/middleware/callMiddleware.d.ts +3 -2
  113. package/dist/core/middleware/callMiddleware.js +13 -3
  114. package/dist/core/middleware/index.js +12 -8
  115. package/dist/core/middleware/sequence.js +22 -4
  116. package/dist/core/module-loader/vite.js +1 -2
  117. package/dist/core/render/params-and-props.js +2 -4
  118. package/dist/core/render/slots.js +4 -8
  119. package/dist/core/render-context.d.ts +15 -5
  120. package/dist/core/render-context.js +134 -28
  121. package/dist/core/request.js +1 -2
  122. package/dist/core/routing/manifest/create.js +3 -6
  123. package/dist/core/sync/index.js +10 -3
  124. package/dist/core/util.d.ts +2 -0
  125. package/dist/core/util.js +18 -19
  126. package/dist/i18n/index.js +2 -4
  127. package/dist/i18n/middleware.js +1 -2
  128. package/dist/i18n/utils.js +1 -2
  129. package/dist/i18n/vite-plugin-i18n.js +1 -2
  130. package/dist/integrations/hooks.js +5 -1
  131. package/dist/jsx/babel.d.ts +3 -0
  132. package/dist/jsx/babel.js +9 -18
  133. package/dist/jsx/rehype.d.ts +11 -0
  134. package/dist/jsx/rehype.js +197 -0
  135. package/dist/jsx/server.js +20 -14
  136. package/dist/jsx/transform-options.d.ts +3 -0
  137. package/dist/jsx-runtime/index.js +8 -16
  138. package/dist/preferences/index.js +3 -6
  139. package/dist/preferences/store.js +3 -6
  140. package/dist/prefetch/index.js +8 -16
  141. package/dist/prefetch/vite-plugin-prefetch.js +2 -4
  142. package/dist/prerender/metadata.js +1 -2
  143. package/dist/prerender/routing.js +1 -1
  144. package/dist/prerender/utils.d.ts +0 -1
  145. package/dist/prerender/utils.js +2 -5
  146. package/dist/runtime/client/dev-toolbar/apps/astro.js +5 -10
  147. package/dist/runtime/client/dev-toolbar/apps/audit/index.js +5 -10
  148. package/dist/runtime/client/dev-toolbar/apps/audit/rules/a11y.js +45 -90
  149. package/dist/runtime/client/dev-toolbar/apps/audit/rules/index.js +1 -2
  150. package/dist/runtime/client/dev-toolbar/apps/audit/rules/perf.js +16 -32
  151. package/dist/runtime/client/dev-toolbar/apps/audit/ui/audit-list-window.js +2 -4
  152. package/dist/runtime/client/dev-toolbar/apps/audit/ui/audit-ui.js +3 -6
  153. package/dist/runtime/client/dev-toolbar/apps/utils/highlight.js +1 -2
  154. package/dist/runtime/client/dev-toolbar/apps/utils/window.js +4 -8
  155. package/dist/runtime/client/dev-toolbar/entrypoint.js +8 -11
  156. package/dist/runtime/client/dev-toolbar/helpers.js +2 -4
  157. package/dist/runtime/client/dev-toolbar/toolbar.js +11 -22
  158. package/dist/runtime/client/dev-toolbar/ui-library/badge.js +1 -2
  159. package/dist/runtime/client/dev-toolbar/ui-library/button.d.ts +5 -0
  160. package/dist/runtime/client/dev-toolbar/ui-library/button.js +26 -5
  161. package/dist/runtime/client/dev-toolbar/ui-library/index.d.ts +1 -0
  162. package/dist/runtime/client/dev-toolbar/ui-library/index.js +2 -0
  163. package/dist/runtime/client/dev-toolbar/ui-library/radio-checkbox.d.ts +13 -0
  164. package/dist/runtime/client/dev-toolbar/ui-library/radio-checkbox.js +109 -0
  165. package/dist/runtime/client/visible.js +1 -2
  166. package/dist/runtime/server/astro-component.js +2 -4
  167. package/dist/runtime/server/astro-island.js +7 -14
  168. package/dist/runtime/server/astro-island.prebuilt-dev.d.ts +1 -1
  169. package/dist/runtime/server/astro-island.prebuilt-dev.js +1 -1
  170. package/dist/runtime/server/astro-island.prebuilt.d.ts +1 -1
  171. package/dist/runtime/server/astro-island.prebuilt.js +1 -1
  172. package/dist/runtime/server/index.js +3 -6
  173. package/dist/runtime/server/jsx.js +1 -2
  174. package/dist/runtime/server/render/any.js +1 -2
  175. package/dist/runtime/server/render/astro/instance.js +1 -2
  176. package/dist/runtime/server/render/astro/render.js +5 -10
  177. package/dist/runtime/server/render/component.js +6 -11
  178. package/dist/runtime/server/render/dom.js +1 -2
  179. package/dist/runtime/server/render/page.js +1 -2
  180. package/dist/runtime/server/render/script.js +1 -2
  181. package/dist/runtime/server/render/slot.js +1 -2
  182. package/dist/runtime/server/render/tags.js +2 -4
  183. package/dist/runtime/server/render/util.js +2 -4
  184. package/dist/runtime/server/shorthash.js +1 -2
  185. package/dist/runtime/server/transition.js +4 -8
  186. package/dist/runtime/server/util.js +1 -2
  187. package/dist/transitions/events.d.ts +3 -3
  188. package/dist/transitions/events.js +5 -4
  189. package/dist/transitions/router.js +22 -113
  190. package/dist/transitions/swap-functions.d.ts +12 -0
  191. package/dist/transitions/swap-functions.js +105 -0
  192. package/dist/vite-plugin-astro/compile.js +1 -2
  193. package/dist/vite-plugin-astro/hmr.js +5 -10
  194. package/dist/vite-plugin-astro/index.js +2 -4
  195. package/dist/vite-plugin-astro-server/pipeline.d.ts +8 -3
  196. package/dist/vite-plugin-astro-server/pipeline.js +59 -11
  197. package/dist/vite-plugin-astro-server/plugin.js +6 -6
  198. package/dist/vite-plugin-astro-server/response.js +1 -2
  199. package/dist/vite-plugin-astro-server/route.js +36 -42
  200. package/dist/vite-plugin-astro-server/vite.js +1 -2
  201. package/dist/vite-plugin-config-alias/index.js +7 -14
  202. package/dist/vite-plugin-head/index.js +3 -6
  203. package/dist/vite-plugin-html/index.js +1 -2
  204. package/dist/vite-plugin-html/transform/escape.js +2 -4
  205. package/dist/vite-plugin-html/transform/slots.js +1 -2
  206. package/dist/vite-plugin-html/transform/utils.js +1 -2
  207. package/dist/vite-plugin-inject-env-ts/index.js +37 -11
  208. package/dist/vite-plugin-integrations-container/index.js +3 -6
  209. package/dist/vite-plugin-load-fallback/index.js +1 -2
  210. package/dist/vite-plugin-markdown/index.js +1 -2
  211. package/dist/vite-plugin-mdx/index.d.ts +3 -0
  212. package/dist/vite-plugin-mdx/tag.d.ts +2 -0
  213. package/dist/vite-plugin-mdx/tag.js +3 -6
  214. package/dist/vite-plugin-mdx/transform-jsx.d.ts +3 -0
  215. package/dist/vite-plugin-mdx/transform-jsx.js +1 -2
  216. package/dist/vite-plugin-scanner/index.js +4 -6
  217. package/dist/vite-plugin-scanner/scan.js +2 -4
  218. package/dist/vite-plugin-scripts/page-ssr.js +3 -6
  219. package/package.json +18 -14
  220. package/templates/actions.mjs +61 -0
  221. package/types/actions.d.ts +3 -0
  222. package/types/content.d.ts +2 -2
@@ -10,8 +10,7 @@ class AstroTimer {
10
10
  * Start a timer for a scope with a given name.
11
11
  */
12
12
  start(name) {
13
- if (!this.enabled)
14
- return;
13
+ if (!this.enabled) return;
15
14
  globalThis.gc?.();
16
15
  this.ongoingTimers.set(name, {
17
16
  startTime: performance.now(),
@@ -22,11 +21,9 @@ class AstroTimer {
22
21
  * End a timer for a scope with a given name.
23
22
  */
24
23
  end(name) {
25
- if (!this.enabled)
26
- return;
24
+ if (!this.enabled) return;
27
25
  const stat = this.ongoingTimers.get(name);
28
- if (!stat)
29
- return;
26
+ if (!stat) return;
30
27
  globalThis.gc?.();
31
28
  const endHeap = process.memoryUsage().heapUsed;
32
29
  this.stats[name] = {
@@ -40,8 +37,7 @@ class AstroTimer {
40
37
  * Write stats to `process.env.ASTRO_TIMER_PATH`
41
38
  */
42
39
  writeStats() {
43
- if (!this.enabled)
44
- return;
40
+ if (!this.enabled) return;
45
41
  fs.writeFileSync(process.env.ASTRO_TIMER_PATH, JSON.stringify(this.stats, null, 2));
46
42
  }
47
43
  }
@@ -1,4 +1,4 @@
1
- const ASTRO_VERSION = "4.7.1";
1
+ const ASTRO_VERSION = "4.8.0";
2
2
  const REROUTE_DIRECTIVE_HEADER = "X-Astro-Reroute";
3
3
  const ROUTE_TYPE_HEADER = "X-Astro-Route-Type";
4
4
  const DEFAULT_404_COMPONENT = "astro-default-404";
@@ -17,10 +17,8 @@ class AstroCookie {
17
17
  return Number(this.value);
18
18
  }
19
19
  boolean() {
20
- if (this.value === "false")
21
- return false;
22
- if (this.value === "0")
23
- return false;
20
+ if (this.value === "false") return false;
21
+ if (this.value === "0") return false;
24
22
  return Boolean(this.value);
25
23
  }
26
24
  }
@@ -145,8 +143,7 @@ class AstroCookies {
145
143
  * @returns
146
144
  */
147
145
  *headers() {
148
- if (this.#outgoing == null)
149
- return;
146
+ if (this.#outgoing == null) return;
150
147
  for (const [, value] of this.#outgoing) {
151
148
  yield value[1];
152
149
  }
@@ -19,7 +19,7 @@ async function dev(inlineConfig) {
19
19
  await telemetry.record([]);
20
20
  const restart = await createContainerWithAutomaticRestart({ inlineConfig, fs });
21
21
  const logger = restart.container.logger;
22
- const currentVersion = "4.7.1";
22
+ const currentVersion = "4.8.0";
23
23
  const isPrerelease = currentVersion.includes("-");
24
24
  if (!isPrerelease) {
25
25
  try {
@@ -22,8 +22,7 @@ async function createRestartedContainer(container, settings) {
22
22
  const configRE = /.*astro.config.(?:mjs|cjs|js|ts)$/;
23
23
  const preferencesRE = /.*\.astro\/settings.json$/;
24
24
  function shouldRestartContainer({ settings, inlineConfig, restartInFlight }, changedFile) {
25
- if (restartInFlight)
26
- return false;
25
+ if (restartInFlight) return false;
27
26
  let shouldRestart = false;
28
27
  if (inlineConfig.configFile) {
29
28
  shouldRestart = vite.normalizePath(inlineConfig.configFile) === vite.normalizePath(changedFile);
@@ -56,6 +56,19 @@ export declare const ClientAddressNotAvailable: {
56
56
  title: string;
57
57
  message: (adapterName: string) => string;
58
58
  };
59
+ /**
60
+ * @docs
61
+ * @see
62
+ * - [Opting-in to pre-rendering](https://docs.astro.build/en/guides/server-side-rendering/#opting-in-to-pre-rendering-in-server-mode)
63
+ * - [Astro.clientAddress](https://docs.astro.build/en/reference/api-reference/#astroclientaddress)
64
+ * @description
65
+ * The `Astro.clientAddress` property cannot be used inside prerendered routes.
66
+ */
67
+ export declare const PrerenderClientAddressNotAvailable: {
68
+ name: string;
69
+ title: string;
70
+ message: string;
71
+ };
59
72
  /**
60
73
  * @docs
61
74
  * @see
@@ -1034,6 +1047,17 @@ export declare const i18nNotEnabled: {
1034
1047
  message: string;
1035
1048
  hint: string;
1036
1049
  };
1050
+ /**
1051
+ * @docs
1052
+ * @description
1053
+ *
1054
+ * Astro couldn't find a route matching the one provided by the user
1055
+ */
1056
+ export declare const RouteNotFound: {
1057
+ name: string;
1058
+ title: string;
1059
+ message: string;
1060
+ };
1037
1061
  /**
1038
1062
  * @docs
1039
1063
  * @kind heading
@@ -14,6 +14,11 @@ const ClientAddressNotAvailable = {
14
14
  title: "`Astro.clientAddress` is not available in current adapter.",
15
15
  message: (adapterName) => `\`Astro.clientAddress\` is not available in the \`${adapterName}\` adapter. File an issue with the adapter to add support.`
16
16
  };
17
+ const PrerenderClientAddressNotAvailable = {
18
+ name: "PrerenderClientAddressNotAvailable",
19
+ title: "`Astro.clientAddress` cannot be used inside prerendered routes.",
20
+ message: `\`Astro.clientAddress\` cannot be used inside prerendered routes`
21
+ };
17
22
  const StaticClientAddressNotAvailable = {
18
23
  name: "StaticClientAddressNotAvailable",
19
24
  title: "`Astro.clientAddress` is not available in static mode.",
@@ -128,8 +133,7 @@ const InvalidPrerenderExport = {
128
133
  message(prefix, suffix, isHydridOutput) {
129
134
  const defaultExpectedValue = isHydridOutput ? "false" : "true";
130
135
  let msg = `A \`prerender\` export has been detected, but its value cannot be statically analyzed.`;
131
- if (prefix !== "const")
132
- msg += `
136
+ if (prefix !== "const") msg += `
133
137
  Expected \`const\` declaration but got \`${prefix}\`.`;
134
138
  if (suffix !== "true")
135
139
  msg += `
@@ -371,6 +375,11 @@ const i18nNotEnabled = {
371
375
  message: "The `astro:i18n` module can not be used without enabling i18n in your Astro config.",
372
376
  hint: "See https://docs.astro.build/en/guides/internationalization for a guide on setting up i18n."
373
377
  };
378
+ const RouteNotFound = {
379
+ name: "RouteNotFound",
380
+ title: "Route not found.",
381
+ message: `Astro could not find a route that matches the one you requested.`
382
+ };
374
383
  const UnknownCSSError = {
375
384
  name: "UnknownCSSError",
376
385
  title: "Unknown CSS Error."
@@ -567,10 +576,12 @@ export {
567
576
  NoPrerenderedRoutesWithDomains,
568
577
  OnlyResponseCanBeReturned,
569
578
  PageNumberParamNotFound,
579
+ PrerenderClientAddressNotAvailable,
570
580
  PrerenderDynamicEndpointPathCollide,
571
581
  RedirectWithNoLocation,
572
582
  ReservedSlotName,
573
583
  ResponseSentError,
584
+ RouteNotFound,
574
585
  StaticClientAddressNotAvailable,
575
586
  StaticRedirectNotAvailable,
576
587
  UnhandledRejection,
@@ -13,8 +13,7 @@ class AstroError extends Error {
13
13
  super(message, options);
14
14
  this.title = title;
15
15
  this.name = name;
16
- if (message)
17
- this.message = message;
16
+ if (message) this.message = message;
18
17
  this.stack = stack ? stack : this.stack;
19
18
  this.loc = location;
20
19
  this.hint = hint;
@@ -668,8 +668,7 @@ class ErrorOverlay extends HTMLElement {
668
668
  const el = this.root.querySelector(selector);
669
669
  if (html) {
670
670
  text = text.split(" ").map((v) => {
671
- if (!v.startsWith("https://"))
672
- return v;
671
+ if (!v.startsWith("https://")) return v;
673
672
  if (v.endsWith("."))
674
673
  return `<a target="_blank" href="${v.slice(0, -1)}">${v.slice(0, -1)}</a>.`;
675
674
  return `<a target="_blank" href="${v}">${v}</a>`;
@@ -6,14 +6,12 @@ function codeFrame(src, loc) {
6
6
  const lines = normalizeLF(src).split("\n").map((ln) => ln.replace(/\t/g, " "));
7
7
  const visibleLines = [];
8
8
  for (let n = -2; n <= 2; n++) {
9
- if (lines[loc.line + n])
10
- visibleLines.push(loc.line + n);
9
+ if (lines[loc.line + n]) visibleLines.push(loc.line + n);
11
10
  }
12
11
  let gutterWidth = 0;
13
12
  for (const lineNo of visibleLines) {
14
13
  let w = `> ${lineNo}`;
15
- if (w.length > gutterWidth)
16
- gutterWidth = w.length;
14
+ if (w.length > gutterWidth) gutterWidth = w.length;
17
15
  }
18
16
  let output = "";
19
17
  for (const lineNo of visibleLines) {
@@ -51,8 +51,7 @@ const errorMap = (baseError, ctx) => {
51
51
  }
52
52
  };
53
53
  const getTypeOrLiteralMsg = (error) => {
54
- if (error.received === "undefined")
55
- return "Required";
54
+ if (error.received === "undefined") return "Required";
56
55
  const expectedDeduped = new Set(error.expected);
57
56
  switch (error.code) {
58
57
  case "invalid_type":
@@ -67,8 +66,7 @@ const getTypeOrLiteralMsg = (error) => {
67
66
  };
68
67
  const prefix = (key, msg) => key.length ? `**${key}**: ${msg}` : msg;
69
68
  const unionExpectedVals = (expectedVals) => [...expectedVals].map((expectedVal, idx) => {
70
- if (idx === 0)
71
- return JSON.stringify(expectedVal);
69
+ if (idx === 0) return JSON.stringify(expectedVal);
72
70
  const sep = " | ";
73
71
  return `${sep}${JSON.stringify(expectedVal)}`;
74
72
  }).join("");
@@ -9,8 +9,7 @@ function removeDir(_dir) {
9
9
  }
10
10
  function removeEmptyDirs(root) {
11
11
  const dir = fileURLToPath(root);
12
- if (!fs.statSync(dir).isDirectory())
13
- return;
12
+ if (!fs.statSync(dir).isDirectory()) return;
14
13
  let files = fs.readdirSync(dir);
15
14
  if (files.length > 0) {
16
15
  files.map((file) => {
@@ -25,8 +24,7 @@ function removeEmptyDirs(root) {
25
24
  }
26
25
  function emptyDir(_dir, skip) {
27
26
  const dir = fileURLToPath(_dir);
28
- if (!fs.existsSync(dir))
29
- return void 0;
27
+ if (!fs.existsSync(dir)) return void 0;
30
28
  for (const file of fs.readdirSync(dir)) {
31
29
  if (skip?.has(file)) {
32
30
  continue;
@@ -19,17 +19,14 @@ function createViteLogger(astroLogger, viteLogLevel = "info") {
19
19
  const logger = {
20
20
  hasWarned: false,
21
21
  info(msg) {
22
- if (!isLogLevelEnabled(viteLogLevel, "info"))
23
- return;
22
+ if (!isLogLevelEnabled(viteLogLevel, "info")) return;
24
23
  const stripped = stripAnsi(msg);
25
24
  let m;
26
25
  if (m = vitePageReloadMsg.exec(stripped)) {
27
- if (isAstroSrcFile(m[1]))
28
- return;
26
+ if (isAstroSrcFile(m[1])) return;
29
27
  astroLogger.info("watch", m[1]);
30
28
  } else if (m = viteHmrUpdateMsg.exec(stripped)) {
31
- if (isAstroSrcFile(m[1]))
32
- return;
29
+ if (isAstroSrcFile(m[1])) return;
33
30
  astroLogger.info("watch", m[1]);
34
31
  } else if (viteBuildMsg.test(stripped) || viteShortcutTitleMsg.test(stripped)) {
35
32
  } else if (viteShortcutHelpMsg.test(stripped)) {
@@ -40,29 +37,23 @@ function createViteLogger(astroLogger, viteLogLevel = "info") {
40
37
  }
41
38
  },
42
39
  warn(msg) {
43
- if (!isLogLevelEnabled(viteLogLevel, "warn"))
44
- return;
40
+ if (!isLogLevelEnabled(viteLogLevel, "warn")) return;
45
41
  logger.hasWarned = true;
46
42
  astroLogger.warn("vite", msg);
47
43
  },
48
44
  warnOnce(msg) {
49
- if (!isLogLevelEnabled(viteLogLevel, "warn"))
50
- return;
51
- if (warnedMessages.has(msg))
52
- return;
45
+ if (!isLogLevelEnabled(viteLogLevel, "warn")) return;
46
+ if (warnedMessages.has(msg)) return;
53
47
  logger.hasWarned = true;
54
48
  astroLogger.warn("vite", msg);
55
49
  warnedMessages.add(msg);
56
50
  },
57
51
  error(msg, opts) {
58
- if (!isLogLevelEnabled(viteLogLevel, "error"))
59
- return;
52
+ if (!isLogLevelEnabled(viteLogLevel, "error")) return;
60
53
  logger.hasWarned = true;
61
54
  const err = opts?.error;
62
- if (err)
63
- loggedErrors.add(err);
64
- if (err && isAstroError(err))
65
- return;
55
+ if (err) loggedErrors.add(err);
56
+ if (err && isAstroError(err)) return;
66
57
  if (msg.includes("Error when evaluating SSR module") || msg.includes("Pre-transform error:")) {
67
58
  astroLogger.debug("vite", msg);
68
59
  return;
@@ -37,7 +37,7 @@ function serverStart({
37
37
  host,
38
38
  base
39
39
  }) {
40
- const version = "4.7.1";
40
+ const version = "4.8.0";
41
41
  const localPrefix = `${dim("\u2503")} Local `;
42
42
  const networkPrefix = `${dim("\u2503")} Network `;
43
43
  const emptyPrefix = " ".repeat(11);
@@ -269,7 +269,7 @@ function printHelp({
269
269
  message.push(
270
270
  linebreak(),
271
271
  ` ${bgGreen(black(` ${commandName} `))} ${green(
272
- `v${"4.7.1"}`
272
+ `v${"4.8.0"}`
273
273
  )} ${headline}`
274
274
  );
275
275
  }
@@ -1,4 +1,5 @@
1
- import type { APIContext, MiddlewareHandler } from '../../@types/astro.js';
1
+ import type { APIContext, MiddlewareHandler, RewritePayload } from '../../@types/astro.js';
2
+ import type { Logger } from '../logger/core.js';
2
3
  /**
3
4
  * Utility function that is in charge of calling the middleware.
4
5
  *
@@ -33,4 +34,4 @@ import type { APIContext, MiddlewareHandler } from '../../@types/astro.js';
33
34
  * @param apiContext The API context
34
35
  * @param responseFunction A callback function that should return a promise with the response
35
36
  */
36
- export declare function callMiddleware(onRequest: MiddlewareHandler, apiContext: APIContext, responseFunction: () => Promise<Response> | Response): Promise<Response>;
37
+ export declare function callMiddleware(onRequest: MiddlewareHandler, apiContext: APIContext, responseFunction: (apiContext: APIContext, rewritePayload?: RewritePayload) => Promise<Response> | Response, enableRerouting: boolean, logger: Logger): Promise<Response>;
@@ -1,10 +1,20 @@
1
1
  import { AstroError, AstroErrorData } from "../errors/index.js";
2
- async function callMiddleware(onRequest, apiContext, responseFunction) {
2
+ async function callMiddleware(onRequest, apiContext, responseFunction, enableRerouting, logger) {
3
3
  let nextCalled = false;
4
4
  let responseFunctionPromise = void 0;
5
- const next = async () => {
5
+ const next = async (payload) => {
6
6
  nextCalled = true;
7
- responseFunctionPromise = responseFunction();
7
+ if (!enableRerouting && payload) {
8
+ logger.warn(
9
+ "router",
10
+ "The rewrite API is experimental. To use this feature, add the `rewriting` flag to the `experimental` object in your Astro config."
11
+ );
12
+ }
13
+ if (enableRerouting) {
14
+ responseFunctionPromise = responseFunction(apiContext, payload);
15
+ } else {
16
+ responseFunctionPromise = responseFunction(apiContext);
17
+ }
8
18
  return responseFunctionPromise;
9
19
  };
10
20
  let middlewarePromise = onRequest(apiContext, next);
@@ -1,14 +1,13 @@
1
+ import { createGetActionResult } from "../../actions/utils.js";
1
2
  import {
2
3
  computeCurrentLocale,
3
4
  computePreferredLocale,
4
5
  computePreferredLocaleList
5
6
  } from "../../i18n/utils.js";
6
- import { ASTRO_VERSION } from "../constants.js";
7
+ import { ASTRO_VERSION, clientAddressSymbol, clientLocalsSymbol } from "../constants.js";
7
8
  import { AstroCookies } from "../cookies/index.js";
8
9
  import { AstroError, AstroErrorData } from "../errors/index.js";
9
10
  import { sequence } from "./sequence.js";
10
- const clientAddressSymbol = Symbol.for("astro.clientAddress");
11
- const clientLocalsSymbol = Symbol.for("astro.locals");
12
11
  function defineMiddleware(fn) {
13
12
  return fn;
14
13
  }
@@ -22,13 +21,17 @@ function createContext({
22
21
  let currentLocale = void 0;
23
22
  const url = new URL(request.url);
24
23
  const route = url.pathname;
25
- return {
24
+ const rewrite = (_reroutePayload) => {
25
+ return Promise.resolve(new Response(null));
26
+ };
27
+ const context = {
26
28
  cookies: new AstroCookies(request),
27
29
  request,
28
30
  params,
29
31
  site: void 0,
30
32
  generator: `Astro v${ASTRO_VERSION}`,
31
33
  props: {},
34
+ rewrite,
32
35
  redirect(path, status) {
33
36
  return new Response(null, {
34
37
  status: status || 302,
@@ -73,6 +76,9 @@ function createContext({
73
76
  }
74
77
  }
75
78
  };
79
+ return Object.assign(context, {
80
+ getActionResult: createGetActionResult(context.locals)
81
+ });
76
82
  }
77
83
  function isLocalsSerializable(value) {
78
84
  let type = typeof value;
@@ -91,11 +97,9 @@ function isLocalsSerializable(value) {
91
97
  return result;
92
98
  }
93
99
  function isPlainObject(value) {
94
- if (typeof value !== "object" || value === null)
95
- return false;
100
+ if (typeof value !== "object" || value === null) return false;
96
101
  let proto = Object.getPrototypeOf(value);
97
- if (proto === null)
98
- return true;
102
+ if (proto === null) return true;
99
103
  let baseProto = proto;
100
104
  while (Object.getPrototypeOf(baseProto) !== null) {
101
105
  baseProto = Object.getPrototypeOf(baseProto);
@@ -1,22 +1,40 @@
1
+ import { AstroCookies } from "../cookies/cookies.js";
1
2
  import { defineMiddleware } from "./index.js";
2
3
  function sequence(...handlers) {
3
4
  const filtered = handlers.filter((h) => !!h);
4
5
  const length = filtered.length;
5
6
  if (!length) {
6
- const handler = defineMiddleware((context, next) => {
7
+ return defineMiddleware((_context, next) => {
7
8
  return next();
8
9
  });
9
- return handler;
10
10
  }
11
11
  return defineMiddleware((context, next) => {
12
+ let carriedPayload = void 0;
12
13
  return applyHandle(0, context);
13
14
  function applyHandle(i, handleContext) {
14
15
  const handle = filtered[i];
15
- const result = handle(handleContext, async () => {
16
+ const result = handle(handleContext, async (payload) => {
16
17
  if (i < length - 1) {
18
+ if (payload) {
19
+ let newRequest;
20
+ if (payload instanceof Request) {
21
+ newRequest = payload;
22
+ } else if (payload instanceof URL) {
23
+ newRequest = new Request(payload, handleContext.request);
24
+ } else {
25
+ newRequest = new Request(
26
+ new URL(payload, handleContext.url.origin),
27
+ handleContext.request
28
+ );
29
+ }
30
+ carriedPayload = payload;
31
+ handleContext.request = newRequest;
32
+ handleContext.url = new URL(newRequest.url);
33
+ handleContext.cookies = new AstroCookies(newRequest);
34
+ }
17
35
  return applyHandle(i + 1, handleContext);
18
36
  } else {
19
- return next();
37
+ return next(payload ?? carriedPayload);
20
38
  }
21
39
  });
22
40
  return result;
@@ -5,8 +5,7 @@ function createViteLoader(viteServer) {
5
5
  let isTsconfigUpdated = false;
6
6
  function isTsconfigUpdate(filePath) {
7
7
  const result = path.basename(filePath) === "tsconfig.json";
8
- if (result)
9
- isTsconfigUpdated = true;
8
+ if (result) isTsconfigUpdated = true;
10
9
  return result;
11
10
  }
12
11
  viteServer.watcher.on("add", (...args) => {
@@ -34,11 +34,9 @@ async function getProps(opts) {
34
34
  return props;
35
35
  }
36
36
  function getParams(route, pathname) {
37
- if (!route.params.length)
38
- return {};
37
+ if (!route.params.length) return {};
39
38
  const paramsMatch = route.pattern.exec(decodeURIComponent(pathname));
40
- if (!paramsMatch)
41
- return {};
39
+ if (!paramsMatch) return {};
42
40
  const params = {};
43
41
  route.params.forEach((key, i) => {
44
42
  if (key.startsWith("...")) {
@@ -4,11 +4,9 @@ import { chunkToString } from "../../runtime/server/render/index.js";
4
4
  import { isRenderInstruction } from "../../runtime/server/render/instruction.js";
5
5
  import { AstroError, AstroErrorData } from "../errors/index.js";
6
6
  function getFunctionExpression(slot) {
7
- if (!slot)
8
- return;
7
+ if (!slot) return;
9
8
  const expressions = slot?.expressions?.filter((e) => isRenderInstruction(e) === false);
10
- if (expressions?.length !== 1)
11
- return;
9
+ if (expressions?.length !== 1) return;
12
10
  return expressions[0];
13
11
  }
14
12
  class Slots {
@@ -37,13 +35,11 @@ class Slots {
37
35
  }
38
36
  }
39
37
  has(name) {
40
- if (!this.#slots)
41
- return false;
38
+ if (!this.#slots) return false;
42
39
  return Boolean(this.#slots[name]);
43
40
  }
44
41
  async render(name, args = []) {
45
- if (!this.#slots || !this.has(name))
46
- return;
42
+ if (!this.#slots || !this.has(name)) return;
47
43
  const result = this.#result;
48
44
  if (!Array.isArray(args)) {
49
45
  this.#logger.warn(
@@ -1,4 +1,5 @@
1
1
  import type { APIContext, AstroGlobal, AstroGlobalPartial, ComponentInstance, MiddlewareHandler, RouteData, SSRResult } from '../@types/astro.js';
2
+ import type { ActionAPIContext } from '../actions/runtime/store.js';
2
3
  import { AstroCookies } from './cookies/index.js';
3
4
  import { type Pipeline } from './render/index.js';
4
5
  /**
@@ -11,13 +12,21 @@ export declare class RenderContext {
11
12
  locals: App.Locals;
12
13
  readonly middleware: MiddlewareHandler;
13
14
  readonly pathname: string;
14
- readonly request: Request;
15
- readonly routeData: RouteData;
15
+ request: Request;
16
+ routeData: RouteData;
16
17
  status: number;
17
- readonly cookies: AstroCookies;
18
- readonly params: import("../@types/astro.js").Params;
19
- readonly url: URL;
18
+ protected cookies: AstroCookies;
19
+ params: import("../@types/astro.js").Params;
20
+ protected url: URL;
20
21
  private constructor();
22
+ /**
23
+ * A flag that tells the render content if the rewriting was triggered
24
+ */
25
+ isRewriting: boolean;
26
+ /**
27
+ * A safety net in case of loops
28
+ */
29
+ counter: number;
21
30
  static create({ locals, middleware, pathname, pipeline, request, routeData, status, }: Pick<RenderContext, 'pathname' | 'pipeline' | 'request' | 'routeData'> & Partial<Pick<RenderContext, 'locals' | 'middleware' | 'status'>>): RenderContext;
22
31
  /**
23
32
  * The main function of the RenderContext.
@@ -32,6 +41,7 @@ export declare class RenderContext {
32
41
  */
33
42
  render(componentInstance: ComponentInstance | undefined): Promise<Response>;
34
43
  createAPIContext(props: APIContext['props']): APIContext;
44
+ createActionAPIContext(): ActionAPIContext;
35
45
  createResult(mod: ComponentInstance): Promise<SSRResult>;
36
46
  /**
37
47
  * The Astro global is sourced in 3 different phases: