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
@@ -32,8 +32,7 @@ const dictionary = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWX
32
32
  const binary = dictionary.length;
33
33
  function bitwise(str) {
34
34
  let hash = 0;
35
- if (str.length === 0)
36
- return hash;
35
+ if (str.length === 0) return hash;
37
36
  for (let i = 0; i < str.length; i++) {
38
37
  const ch = str.charCodeAt(i);
39
38
  hash = (hash << 5) - hash + ch;
@@ -15,12 +15,9 @@ function createTransitionScope(result, hash) {
15
15
  return `astro-${hash}-${num}`;
16
16
  }
17
17
  const getAnimations = (name) => {
18
- if (name === "fade")
19
- return fade();
20
- if (name === "slide")
21
- return slide();
22
- if (typeof name === "object")
23
- return name;
18
+ if (name === "fade") return fade();
19
+ if (name === "slide") return slide();
20
+ if (typeof name === "object") return name;
24
21
  };
25
22
  const addPairs = (animations, stylesheet) => {
26
23
  for (const [direction, images] of Object.entries(animations)) {
@@ -46,8 +43,7 @@ function renderTransition(result, hash, animationName, transitionName) {
46
43
  if (typeof (transitionName ?? "") !== "string") {
47
44
  throw new Error(`Invalid transition name {${transitionName}}`);
48
45
  }
49
- if (!animationName)
50
- animationName = "fade";
46
+ if (!animationName) animationName = "fade";
51
47
  const scope = createTransitionScope(result, hash);
52
48
  const name = transitionName ? cssesc(reEncode(transitionName), { isIdentifier: true }) : scope;
53
49
  const sheet = new ViewTransitionStyleSheet(scope, name);
@@ -6,8 +6,7 @@ async function* streamAsyncIterator(stream) {
6
6
  try {
7
7
  while (true) {
8
8
  const { done, value } = await reader.read();
9
- if (done)
10
- return;
9
+ if (done) return;
11
10
  yield value;
12
11
  }
13
12
  } finally {
@@ -16,7 +16,7 @@ declare class BeforeEvent extends Event {
16
16
  readonly sourceElement: Element | undefined;
17
17
  readonly info: any;
18
18
  newDocument: Document;
19
- signal: AbortSignal;
19
+ readonly signal: AbortSignal;
20
20
  constructor(type: string, eventInitDict: EventInit | undefined, from: URL, to: URL, direction: Direction | string, navigationType: NavigationTypeString, sourceElement: Element | undefined, info: any, newDocument: Document, signal: AbortSignal);
21
21
  }
22
22
  export declare const isTransitionBeforePreparationEvent: (value: any) => value is TransitionBeforePreparationEvent;
@@ -30,8 +30,8 @@ export declare class TransitionBeforeSwapEvent extends BeforeEvent {
30
30
  readonly direction: Direction | string;
31
31
  readonly viewTransition: ViewTransition;
32
32
  swap: () => void;
33
- constructor(afterPreparation: BeforeEvent, viewTransition: ViewTransition, swap: (event: TransitionBeforeSwapEvent) => void);
33
+ constructor(afterPreparation: BeforeEvent, viewTransition: ViewTransition);
34
34
  }
35
35
  export declare function doPreparation(from: URL, to: URL, direction: Direction | string, navigationType: NavigationTypeString, sourceElement: Element | undefined, info: any, signal: AbortSignal, formData: FormData | undefined, defaultLoader: (event: TransitionBeforePreparationEvent) => Promise<void>): Promise<TransitionBeforePreparationEvent>;
36
- export declare function doSwap(afterPreparation: BeforeEvent, viewTransition: ViewTransition, defaultSwap: (event: TransitionBeforeSwapEvent) => void): TransitionBeforeSwapEvent;
36
+ export declare function doSwap(afterPreparation: BeforeEvent, viewTransition: ViewTransition): TransitionBeforeSwapEvent;
37
37
  export {};
@@ -1,4 +1,5 @@
1
1
  import { updateScrollPosition } from "./router.js";
2
+ import { swap } from "./swap-functions.js";
2
3
  const TRANSITION_BEFORE_PREPARATION = "astro:before-preparation";
3
4
  const TRANSITION_AFTER_PREPARATION = "astro:after-preparation";
4
5
  const TRANSITION_BEFORE_SWAP = "astro:before-swap";
@@ -67,7 +68,7 @@ class TransitionBeforeSwapEvent extends BeforeEvent {
67
68
  direction;
68
69
  viewTransition;
69
70
  swap;
70
- constructor(afterPreparation, viewTransition, swap) {
71
+ constructor(afterPreparation, viewTransition) {
71
72
  super(
72
73
  TRANSITION_BEFORE_SWAP,
73
74
  void 0,
@@ -82,7 +83,7 @@ class TransitionBeforeSwapEvent extends BeforeEvent {
82
83
  );
83
84
  this.direction = afterPreparation.direction;
84
85
  this.viewTransition = viewTransition;
85
- this.swap = swap.bind(this, this);
86
+ this.swap = () => swap(this.newDocument);
86
87
  Object.defineProperties(this, {
87
88
  direction: { enumerable: true },
88
89
  viewTransition: { enumerable: true },
@@ -114,8 +115,8 @@ async function doPreparation(from, to, direction, navigationType, sourceElement,
114
115
  }
115
116
  return event;
116
117
  }
117
- function doSwap(afterPreparation, viewTransition, defaultSwap) {
118
- const event = new TransitionBeforeSwapEvent(afterPreparation, viewTransition, defaultSwap);
118
+ function doSwap(afterPreparation, viewTransition) {
119
+ const event = new TransitionBeforeSwapEvent(afterPreparation, viewTransition);
119
120
  document.dispatchEvent(event);
120
121
  event.swap();
121
122
  return event;
@@ -1,4 +1,12 @@
1
1
  import { TRANSITION_AFTER_SWAP, doPreparation, doSwap } from "./events.js";
2
+ import {
3
+ deselectScripts,
4
+ restoreFocus,
5
+ saveFocus,
6
+ swapBodyElement,
7
+ swapHeadElements,
8
+ swapRootAttributes
9
+ } from "./swap-functions.js";
2
10
  const inBrowser = import.meta.env.SSR === false;
3
11
  const pushState = inBrowser && history.pushState.bind(history);
4
12
  const replaceState = inBrowser && history.replaceState.bind(history);
@@ -76,11 +84,9 @@ function getFallback() {
76
84
  function runScripts() {
77
85
  let wait = Promise.resolve();
78
86
  for (const script of Array.from(document.scripts)) {
79
- if (script.dataset.astroExec === "")
80
- continue;
87
+ if (script.dataset.astroExec === "") continue;
81
88
  const type = script.getAttribute("type");
82
- if (type && type !== "module" && type !== "text/javascript")
83
- continue;
89
+ if (type && type !== "module" && type !== "text/javascript") continue;
84
90
  const newScript = document.createElement("script");
85
91
  newScript.innerHTML = script.innerHTML;
86
92
  for (const attr of script.attributes) {
@@ -173,97 +179,10 @@ function preloadStyleLinks(newDocument) {
173
179
  return links;
174
180
  }
175
181
  async function updateDOM(preparationEvent, options, currentTransition, historyState, fallback) {
176
- const persistedHeadElement = (el, newDoc) => {
177
- const id = el.getAttribute(PERSIST_ATTR);
178
- const newEl = id && newDoc.head.querySelector(`[${PERSIST_ATTR}="${id}"]`);
179
- if (newEl) {
180
- return newEl;
181
- }
182
- if (el.matches("link[rel=stylesheet]")) {
183
- const href = el.getAttribute("href");
184
- return newDoc.head.querySelector(`link[rel=stylesheet][href="${href}"]`);
185
- }
186
- return null;
187
- };
188
- const saveFocus = () => {
189
- const activeElement = document.activeElement;
190
- if (activeElement?.closest(`[${PERSIST_ATTR}]`)) {
191
- if (activeElement instanceof HTMLInputElement || activeElement instanceof HTMLTextAreaElement) {
192
- const start = activeElement.selectionStart;
193
- const end = activeElement.selectionEnd;
194
- return { activeElement, start, end };
195
- }
196
- return { activeElement };
197
- } else {
198
- return { activeElement: null };
199
- }
200
- };
201
- const restoreFocus = ({ activeElement, start, end }) => {
202
- if (activeElement) {
203
- activeElement.focus();
204
- if (activeElement instanceof HTMLInputElement || activeElement instanceof HTMLTextAreaElement) {
205
- if (typeof start === "number")
206
- activeElement.selectionStart = start;
207
- if (typeof end === "number")
208
- activeElement.selectionEnd = end;
209
- }
210
- }
211
- };
212
- const shouldCopyProps = (el) => {
213
- const persistProps = el.dataset.astroTransitionPersistProps;
214
- return persistProps == null || persistProps === "false";
215
- };
216
- const defaultSwap = (beforeSwapEvent) => {
217
- const html = document.documentElement;
218
- const astroAttributes = [...html.attributes].filter(
219
- ({ name }) => (html.removeAttribute(name), name.startsWith("data-astro-"))
220
- );
221
- [...beforeSwapEvent.newDocument.documentElement.attributes, ...astroAttributes].forEach(
222
- ({ name, value }) => html.setAttribute(name, value)
223
- );
224
- for (const s1 of document.scripts) {
225
- for (const s2 of beforeSwapEvent.newDocument.scripts) {
226
- if (
227
- // Check if the script should be rerun regardless of it being the same
228
- !s2.hasAttribute("data-astro-rerun") && // Inline
229
- (!s1.src && s1.textContent === s2.textContent || // External
230
- s1.src && s1.type === s2.type && s1.src === s2.src)
231
- ) {
232
- s2.dataset.astroExec = "";
233
- break;
234
- }
235
- }
236
- }
237
- for (const el of Array.from(document.head.children)) {
238
- const newEl = persistedHeadElement(el, beforeSwapEvent.newDocument);
239
- if (newEl) {
240
- newEl.remove();
241
- } else {
242
- el.remove();
243
- }
244
- }
245
- document.head.append(...beforeSwapEvent.newDocument.head.children);
246
- const oldBody = document.body;
247
- const savedFocus = saveFocus();
248
- document.body.replaceWith(beforeSwapEvent.newDocument.body);
249
- for (const el of oldBody.querySelectorAll(`[${PERSIST_ATTR}]`)) {
250
- const id = el.getAttribute(PERSIST_ATTR);
251
- const newEl = document.querySelector(`[${PERSIST_ATTR}="${id}"]`);
252
- if (newEl) {
253
- newEl.replaceWith(el);
254
- if (newEl.localName === "astro-island" && shouldCopyProps(el)) {
255
- el.setAttribute("ssr", "");
256
- el.setAttribute("props", newEl.getAttribute("props"));
257
- }
258
- }
259
- }
260
- restoreFocus(savedFocus);
261
- };
262
182
  async function animate(phase) {
263
183
  function isInfinite(animation) {
264
184
  const effect = animation.effect;
265
- if (!effect || !(effect instanceof KeyframeEffect) || !effect.target)
266
- return false;
185
+ if (!effect || !(effect instanceof KeyframeEffect) || !effect.target) return false;
267
186
  const style = window.getComputedStyle(effect.target, effect.pseudoElement);
268
187
  return style.animationIterationCount === "infinite";
269
188
  }
@@ -282,7 +201,7 @@ async function updateDOM(preparationEvent, options, currentTransition, historySt
282
201
  }
283
202
  }
284
203
  const pageTitleForBrowserHistory = document.title;
285
- const swapEvent = doSwap(preparationEvent, currentTransition.viewTransition, defaultSwap);
204
+ const swapEvent = doSwap(preparationEvent, currentTransition.viewTransition);
286
205
  moveToLocation(swapEvent.to, swapEvent.from, options, pageTitleForBrowserHistory, historyState);
287
206
  triggerEvent(TRANSITION_AFTER_SWAP);
288
207
  if (fallback === "animate") {
@@ -302,8 +221,7 @@ function abortAndRecreateMostRecentNavigation() {
302
221
  async function transition(direction, from, to, options, historyState) {
303
222
  const currentNavigation = abortAndRecreateMostRecentNavigation();
304
223
  if (!transitionEnabledOnThisPage() || location.origin !== to.origin) {
305
- if (currentNavigation === mostRecentNavigation)
306
- mostRecentNavigation = void 0;
224
+ if (currentNavigation === mostRecentNavigation) mostRecentNavigation = void 0;
307
225
  location.href = to.href;
308
226
  return;
309
227
  }
@@ -314,8 +232,7 @@ async function transition(direction, from, to, options, historyState) {
314
232
  if (samePage(from, to)) {
315
233
  if (direction !== "back" && to.hash || direction === "back" && from.hash) {
316
234
  moveToLocation(to, from, options, document.title, historyState);
317
- if (currentNavigation === mostRecentNavigation)
318
- mostRecentNavigation = void 0;
235
+ if (currentNavigation === mostRecentNavigation) mostRecentNavigation = void 0;
319
236
  return;
320
237
  }
321
238
  }
@@ -331,8 +248,7 @@ async function transition(direction, from, to, options, historyState) {
331
248
  defaultLoader
332
249
  );
333
250
  if (prepEvent.defaultPrevented || prepEvent.signal.aborted) {
334
- if (currentNavigation === mostRecentNavigation)
335
- mostRecentNavigation = void 0;
251
+ if (currentNavigation === mostRecentNavigation) mostRecentNavigation = void 0;
336
252
  if (!prepEvent.signal.aborted) {
337
253
  location.href = to.href;
338
254
  }
@@ -387,8 +303,7 @@ async function transition(direction, from, to, options, historyState) {
387
303
  }
388
304
  const currentTransition = await abortAndRecreateMostRecentTransition();
389
305
  if (prepEvent.signal.aborted) {
390
- if (currentNavigation === mostRecentNavigation)
391
- mostRecentNavigation = void 0;
306
+ if (currentNavigation === mostRecentNavigation) mostRecentNavigation = void 0;
392
307
  return;
393
308
  }
394
309
  document.documentElement.setAttribute(DIRECTION_ATTR, prepEvent.direction);
@@ -423,10 +338,8 @@ async function transition(direction, from, to, options, historyState) {
423
338
  });
424
339
  currentTransition.viewTransition.finished.finally(() => {
425
340
  currentTransition.viewTransition = void 0;
426
- if (currentTransition === mostRecentTransition)
427
- mostRecentTransition = void 0;
428
- if (currentNavigation === mostRecentNavigation)
429
- mostRecentNavigation = void 0;
341
+ if (currentTransition === mostRecentTransition) mostRecentTransition = void 0;
342
+ if (currentNavigation === mostRecentNavigation) mostRecentNavigation = void 0;
430
343
  document.documentElement.removeAttribute(DIRECTION_ATTR);
431
344
  document.documentElement.removeAttribute(OLD_NEW_ATTR);
432
345
  });
@@ -476,8 +389,7 @@ if (inBrowser) {
476
389
  originalLocation = new URL(location.href);
477
390
  addEventListener("popstate", onPopState);
478
391
  addEventListener("load", onPageLoad);
479
- if ("onscrollend" in window)
480
- addEventListener("scrollend", onScrollEnd);
392
+ if ("onscrollend" in window) addEventListener("scrollend", onScrollEnd);
481
393
  else {
482
394
  let intervalId, lastY, lastX, lastIndex;
483
395
  const scrollInterval = () => {
@@ -498,8 +410,7 @@ if (inBrowser) {
498
410
  addEventListener(
499
411
  "scroll",
500
412
  () => {
501
- if (intervalId !== void 0)
502
- return;
413
+ if (intervalId !== void 0) return;
503
414
  lastIndex = history.state.index, lastY = scrollY, lastX = scrollX;
504
415
  intervalId = window.setInterval(scrollInterval, 50);
505
416
  },
@@ -543,10 +454,8 @@ async function prepareForClientOnlyComponents(newDocument, toLocation, signal) {
543
454
  }
544
455
  return new Promise(async (r) => {
545
456
  for (let count = 0; count <= 20; ++count) {
546
- if (signal.aborted)
547
- break;
548
- if (!loadingPage.contentDocument.body.querySelector("astro-island[ssr]"))
549
- break;
457
+ if (signal.aborted) break;
458
+ if (!loadingPage.contentDocument.body.querySelector("astro-island[ssr]")) break;
550
459
  await new Promise((r2) => setTimeout(r2, 50));
551
460
  }
552
461
  r();
@@ -0,0 +1,12 @@
1
+ export type SavedFocus = {
2
+ activeElement: HTMLElement | null;
3
+ start?: number | null;
4
+ end?: number | null;
5
+ };
6
+ export declare function deselectScripts(doc: Document): void;
7
+ export declare function swapRootAttributes(doc: Document): void;
8
+ export declare function swapHeadElements(doc: Document): void;
9
+ export declare function swapBodyElement(newElement: Element, oldElement: Element): void;
10
+ export declare const saveFocus: () => (() => void);
11
+ export declare const restoreFocus: ({ activeElement, start, end }: SavedFocus) => void;
12
+ export declare const swap: (doc: Document) => void;
@@ -0,0 +1,105 @@
1
+ const PERSIST_ATTR = "data-astro-transition-persist";
2
+ function deselectScripts(doc) {
3
+ for (const s1 of document.scripts) {
4
+ for (const s2 of doc.scripts) {
5
+ if (
6
+ // Check if the script should be rerun regardless of it being the same
7
+ !s2.hasAttribute("data-astro-rerun") && // Inline
8
+ (!s1.src && s1.textContent === s2.textContent || // External
9
+ s1.src && s1.type === s2.type && s1.src === s2.src)
10
+ ) {
11
+ s2.dataset.astroExec = "";
12
+ break;
13
+ }
14
+ }
15
+ }
16
+ }
17
+ function swapRootAttributes(doc) {
18
+ const html = document.documentElement;
19
+ const astroAttributes = [...html.attributes].filter(
20
+ ({ name }) => (html.removeAttribute(name), name.startsWith("data-astro-"))
21
+ );
22
+ [...doc.documentElement.attributes, ...astroAttributes].forEach(
23
+ ({ name, value }) => html.setAttribute(name, value)
24
+ );
25
+ }
26
+ function swapHeadElements(doc) {
27
+ for (const el of Array.from(document.head.children)) {
28
+ const newEl = persistedHeadElement(el, doc);
29
+ if (newEl) {
30
+ newEl.remove();
31
+ } else {
32
+ el.remove();
33
+ }
34
+ }
35
+ document.head.append(...doc.head.children);
36
+ }
37
+ function swapBodyElement(newElement, oldElement) {
38
+ oldElement.replaceWith(newElement);
39
+ for (const el of oldElement.querySelectorAll(`[${PERSIST_ATTR}]`)) {
40
+ const id = el.getAttribute(PERSIST_ATTR);
41
+ const newEl = newElement.querySelector(`[${PERSIST_ATTR}="${id}"]`);
42
+ if (newEl) {
43
+ newEl.replaceWith(el);
44
+ if (newEl.localName === "astro-island" && shouldCopyProps(el)) {
45
+ el.setAttribute("ssr", "");
46
+ el.setAttribute("props", newEl.getAttribute("props"));
47
+ }
48
+ }
49
+ }
50
+ }
51
+ const saveFocus = () => {
52
+ const activeElement = document.activeElement;
53
+ if (activeElement?.closest(`[${PERSIST_ATTR}]`)) {
54
+ if (activeElement instanceof HTMLInputElement || activeElement instanceof HTMLTextAreaElement) {
55
+ const start = activeElement.selectionStart;
56
+ const end = activeElement.selectionEnd;
57
+ return () => restoreFocus({ activeElement, start, end });
58
+ }
59
+ return () => restoreFocus({ activeElement });
60
+ } else {
61
+ return () => restoreFocus({ activeElement: null });
62
+ }
63
+ };
64
+ const restoreFocus = ({ activeElement, start, end }) => {
65
+ if (activeElement) {
66
+ activeElement.focus();
67
+ if (activeElement instanceof HTMLInputElement || activeElement instanceof HTMLTextAreaElement) {
68
+ if (typeof start === "number") activeElement.selectionStart = start;
69
+ if (typeof end === "number") activeElement.selectionEnd = end;
70
+ }
71
+ }
72
+ };
73
+ const persistedHeadElement = (el, newDoc) => {
74
+ const id = el.getAttribute(PERSIST_ATTR);
75
+ const newEl = id && newDoc.head.querySelector(`[${PERSIST_ATTR}="${id}"]`);
76
+ if (newEl) {
77
+ return newEl;
78
+ }
79
+ if (el.matches("link[rel=stylesheet]")) {
80
+ const href = el.getAttribute("href");
81
+ return newDoc.head.querySelector(`link[rel=stylesheet][href="${href}"]`);
82
+ }
83
+ return null;
84
+ };
85
+ const shouldCopyProps = (el) => {
86
+ const persistProps = el.dataset.astroTransitionPersistProps;
87
+ return persistProps == null || persistProps === "false";
88
+ };
89
+ const swap = (doc) => {
90
+ deselectScripts(doc);
91
+ swapRootAttributes(doc);
92
+ swapHeadElements(doc);
93
+ const restoreFocusFunction = saveFocus();
94
+ swapBodyElement(doc.body, document.body);
95
+ restoreFocusFunction();
96
+ };
97
+ export {
98
+ deselectScripts,
99
+ restoreFocus,
100
+ saveFocus,
101
+ swap,
102
+ swapBodyElement,
103
+ swapHeadElements,
104
+ swapRootAttributes
105
+ };
@@ -71,8 +71,7 @@ async function enhanceCompileError({
71
71
  const scannedFrontmatter = frontmatterRE.exec(source);
72
72
  if (scannedFrontmatter) {
73
73
  const frontmatter = scannedFrontmatter[1].replace(/\breturn\b/g, "throw");
74
- if (lineText && !frontmatter.includes(lineText))
75
- throw err;
74
+ if (lineText && !frontmatter.includes(lineText)) throw err;
76
75
  try {
77
76
  await transformWithEsbuild(frontmatter, id, {
78
77
  loader: "ts",
@@ -7,8 +7,7 @@ async function handleHotUpdate(ctx, { logger, astroFileToCompileMetadata }) {
7
7
  }
8
8
  }
9
9
  const oldCode = astroFileToCompileMetadata.get(ctx.file)?.originalCode;
10
- if (oldCode == null)
11
- return;
10
+ if (oldCode == null) return;
12
11
  const newCode = await ctx.read();
13
12
  if (isStyleOnlyChanged(oldCode, newCode)) {
14
13
  logger.debug("watch", "style-only change");
@@ -19,26 +18,22 @@ async function handleHotUpdate(ctx, { logger, astroFileToCompileMetadata }) {
19
18
  const scriptRE = /<script(?:\s.*?)?>.*?<\/script>/gs;
20
19
  const styleRE = /<style(?:\s.*?)?>.*?<\/style>/gs;
21
20
  function isStyleOnlyChanged(oldCode, newCode) {
22
- if (oldCode === newCode)
23
- return false;
21
+ if (oldCode === newCode) return false;
24
22
  let oldFrontmatter = "";
25
23
  let newFrontmatter = "";
26
24
  oldCode = oldCode.replace(frontmatterRE, (m) => (oldFrontmatter = m, ""));
27
25
  newCode = newCode.replace(frontmatterRE, (m) => (newFrontmatter = m, ""));
28
- if (oldFrontmatter !== newFrontmatter)
29
- return false;
26
+ if (oldFrontmatter !== newFrontmatter) return false;
30
27
  const oldScripts = [];
31
28
  const newScripts = [];
32
29
  oldCode = oldCode.replace(scriptRE, (m) => (oldScripts.push(m), ""));
33
30
  newCode = newCode.replace(scriptRE, (m) => (newScripts.push(m), ""));
34
- if (!isArrayEqual(oldScripts, newScripts))
35
- return false;
31
+ if (!isArrayEqual(oldScripts, newScripts)) return false;
36
32
  const oldStyles = [];
37
33
  const newStyles = [];
38
34
  oldCode = oldCode.replace(styleRE, (m) => (oldStyles.push(m), ""));
39
35
  newCode = newCode.replace(styleRE, (m) => (newStyles.push(m), ""));
40
- if (oldCode !== newCode)
41
- return false;
36
+ if (oldCode !== newCode) return false;
42
37
  return oldStyles.length === newStyles.length && !isArrayEqual(oldStyles, newStyles);
43
38
  }
44
39
  function isArrayEqual(a, b) {
@@ -57,8 +57,7 @@ function astro({ settings, logger }) {
57
57
  if (!compileMetadata) {
58
58
  if (server) {
59
59
  const code = await loadId(server.pluginContainer, filename);
60
- if (code != null)
61
- await compile(code, filename);
60
+ if (code != null) await compile(code, filename);
62
61
  } else if (config.experimental.contentCollectionCache) {
63
62
  await this.load({
64
63
  id: filename,
@@ -191,8 +190,7 @@ File: ${id}`
191
190
  return [prePlugin, normalPlugin];
192
191
  }
193
192
  function appendSourceMap(content, map) {
194
- if (!map)
195
- return content;
193
+ if (!map) return content;
196
194
  return `${content}
197
195
  //# sourceMappingURL=data:application/json;charset=utf-8;base64,${Buffer.from(
198
196
  map
@@ -1,4 +1,4 @@
1
- import type { AstroSettings, ComponentInstance, RouteData, SSRLoadedRenderer, SSRManifest } from '../@types/astro.js';
1
+ import type { AstroSettings, ComponentInstance, ManifestData, RewritePayload, RouteData, SSRLoadedRenderer, SSRManifest } from '../@types/astro.js';
2
2
  import type { HeadElements } from '../core/base-pipeline.js';
3
3
  import type { Logger } from '../core/logger/core.js';
4
4
  import type { ModuleLoader } from '../core/module-loader/index.js';
@@ -10,10 +10,15 @@ export declare class DevPipeline extends Pipeline {
10
10
  readonly settings: AstroSettings;
11
11
  readonly config: import("../@types/astro.js").AstroConfig;
12
12
  renderers: SSRLoadedRenderer[];
13
+ manifestData: ManifestData | undefined;
14
+ componentInterner: WeakMap<RouteData, ComponentInstance>;
13
15
  private constructor();
14
- static create({ loader, logger, manifest, settings, }: Pick<DevPipeline, 'loader' | 'logger' | 'manifest' | 'settings'>): DevPipeline;
16
+ static create(manifestData: ManifestData, { loader, logger, manifest, settings, }: Pick<DevPipeline, 'loader' | 'logger' | 'manifest' | 'settings'>): DevPipeline;
15
17
  headElements(routeData: RouteData): Promise<HeadElements>;
16
18
  componentMetadata(routeData: RouteData): Promise<Map<string, import("../@types/astro.js").SSRComponentMetadata>>;
17
- preload(filePath: URL): Promise<ComponentInstance>;
19
+ preload(routeData: RouteData, filePath: URL): Promise<ComponentInstance>;
18
20
  clearRouteCache(): void;
21
+ getComponentByRoute(routeData: RouteData): Promise<ComponentInstance>;
22
+ tryRewrite(payload: RewritePayload): Promise<[RouteData, ComponentInstance]>;
23
+ setManifestData(manifestData: ManifestData): void;
19
24
  }
@@ -1,11 +1,11 @@
1
- import url from "node:url";
1
+ import { fileURLToPath } from "node:url";
2
2
  import { getInfoOutput } from "../cli/info/index.js";
3
3
  import { ASTRO_VERSION, DEFAULT_404_COMPONENT } from "../core/constants.js";
4
4
  import { enhanceViteSSRError } from "../core/errors/dev/index.js";
5
- import { AggregateError, CSSError, MarkdownError } from "../core/errors/index.js";
5
+ import { RouteNotFound } from "../core/errors/errors-data.js";
6
+ import { AggregateError, AstroError, CSSError, MarkdownError } from "../core/errors/index.js";
6
7
  import { Pipeline, loadRenderer } from "../core/render/index.js";
7
- import { isPage, resolveIdToUrl, viteID } from "../core/util.js";
8
- import { isServerLikeOutput } from "../prerender/utils.js";
8
+ import { isPage, isServerLikeOutput, resolveIdToUrl, viteID } from "../core/util.js";
9
9
  import { PAGE_SCRIPT_ID } from "../vite-plugin-scripts/index.js";
10
10
  import { getStylesForURL } from "./css.js";
11
11
  import { getComponentMetadata } from "./metadata.js";
@@ -28,13 +28,17 @@ class DevPipeline extends Pipeline {
28
28
  // renderers are loaded on every request,
29
29
  // so it needs to be mutable here unlike in other environments
30
30
  renderers = new Array();
31
- static create({
31
+ manifestData;
32
+ componentInterner = /* @__PURE__ */ new WeakMap();
33
+ static create(manifestData, {
32
34
  loader,
33
35
  logger,
34
36
  manifest,
35
37
  settings
36
38
  }) {
37
- return new DevPipeline(loader, logger, manifest, settings);
39
+ const pipeline = new DevPipeline(loader, logger, manifest, settings);
40
+ pipeline.manifestData = manifestData;
41
+ return pipeline;
38
42
  }
39
43
  async headElements(routeData) {
40
44
  const {
@@ -43,7 +47,7 @@ class DevPipeline extends Pipeline {
43
47
  mode,
44
48
  settings
45
49
  } = this;
46
- const filePath = new URL(`./${routeData.component}`, root);
50
+ const filePath = new URL(`${routeData.component}`, root);
47
51
  const { scripts } = settings.config.experimental.directRenderScript ? { scripts: /* @__PURE__ */ new Set() } : await getScriptsForURL(filePath, settings.config.root, loader);
48
52
  if (isPage(filePath, settings) && mode === "development") {
49
53
  scripts.add({
@@ -54,7 +58,7 @@ class DevPipeline extends Pipeline {
54
58
  const src = await resolveIdToUrl(loader, "astro/runtime/client/dev-toolbar/entrypoint.js");
55
59
  scripts.add({ props: { type: "module", src }, children: "" });
56
60
  const additionalMetadata = {
57
- root: url.fileURLToPath(settings.config.root),
61
+ root: fileURLToPath(settings.config.root),
58
62
  version: ASTRO_VERSION,
59
63
  latestAstroVersion: settings.latestAstroVersion,
60
64
  debugInfo: await getInfoOutput({ userConfig: settings.config, print: false })
@@ -93,10 +97,10 @@ class DevPipeline extends Pipeline {
93
97
  config: { root },
94
98
  loader
95
99
  } = this;
96
- const filePath = new URL(`./${routeData.component}`, root);
100
+ const filePath = new URL(`${routeData.component}`, root);
97
101
  return getComponentMetadata(filePath, loader);
98
102
  }
99
- async preload(filePath) {
103
+ async preload(routeData, filePath) {
100
104
  const { loader } = this;
101
105
  if (filePath.href === new URL(DEFAULT_404_COMPONENT, this.config.root).href) {
102
106
  return { default: default404Page };
@@ -105,7 +109,9 @@ class DevPipeline extends Pipeline {
105
109
  const renderers_ = await Promise.all(renderers__);
106
110
  this.renderers = renderers_.filter((r) => Boolean(r));
107
111
  try {
108
- return await loader.import(viteID(filePath));
112
+ const componentInstance = await loader.import(viteID(filePath));
113
+ this.componentInterner.set(routeData, componentInstance);
114
+ return componentInstance;
109
115
  } catch (error) {
110
116
  if (MarkdownError.is(error) || CSSError.is(error) || AggregateError.is(error)) {
111
117
  throw error;
@@ -115,6 +121,48 @@ class DevPipeline extends Pipeline {
115
121
  }
116
122
  clearRouteCache() {
117
123
  this.routeCache.clearAll();
124
+ this.componentInterner = /* @__PURE__ */ new WeakMap();
125
+ }
126
+ async getComponentByRoute(routeData) {
127
+ const component = this.componentInterner.get(routeData);
128
+ if (component) {
129
+ return component;
130
+ } else {
131
+ const filePath = new URL(`${routeData.component}`, this.config.root);
132
+ return await this.preload(routeData, filePath);
133
+ }
134
+ }
135
+ async tryRewrite(payload) {
136
+ let foundRoute;
137
+ if (!this.manifestData) {
138
+ throw new Error("Missing manifest data. This is an internal error, please file an issue.");
139
+ }
140
+ for (const route of this.manifestData.routes) {
141
+ if (payload instanceof URL) {
142
+ if (route.pattern.test(payload.pathname)) {
143
+ foundRoute = route;
144
+ break;
145
+ }
146
+ } else if (payload instanceof Request) {
147
+ const url = new URL(payload.url);
148
+ if (route.pattern.test(url.pathname)) {
149
+ foundRoute = route;
150
+ break;
151
+ }
152
+ } else if (route.pattern.test(decodeURI(payload))) {
153
+ foundRoute = route;
154
+ break;
155
+ }
156
+ }
157
+ if (foundRoute) {
158
+ const componentInstance = await this.getComponentByRoute(foundRoute);
159
+ return [foundRoute, componentInstance];
160
+ } else {
161
+ throw new AstroError(RouteNotFound);
162
+ }
163
+ }
164
+ setManifestData(manifestData) {
165
+ this.manifestData = manifestData;
118
166
  }
119
167
  }
120
168
  export {