From cae268aa32e6c62f738ddc9b477fb1c0548aa966 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Fri, 22 Sep 2023 18:09:19 -0400 Subject: [PATCH 1/4] Head propagation graph walking on new pages --- packages/astro/src/vite-plugin-head/index.ts | 25 +++++++++++++++++++ .../fixtures/view-transitions/package.json | 8 ++++++ .../src/components/Animate.astro | 1 + .../src/components/AnimateContainer.astro | 1 + .../src/components/Animations.astro | 4 +++ .../view-transitions/src/pages/one.astro | 11 ++++++++ .../view-transitions/src/pages/two.astro | 15 +++++++++++ pnpm-lock.yaml | 6 +++++ 8 files changed, 71 insertions(+) create mode 100644 packages/astro/test/fixtures/view-transitions/package.json create mode 100644 packages/astro/test/fixtures/view-transitions/src/components/Animate.astro create mode 100644 packages/astro/test/fixtures/view-transitions/src/components/AnimateContainer.astro create mode 100644 packages/astro/test/fixtures/view-transitions/src/components/Animations.astro create mode 100644 packages/astro/test/fixtures/view-transitions/src/pages/one.astro create mode 100644 packages/astro/test/fixtures/view-transitions/src/pages/two.astro diff --git a/packages/astro/src/vite-plugin-head/index.ts b/packages/astro/src/vite-plugin-head/index.ts index f8a13f925d33..4490eb7ee26c 100644 --- a/packages/astro/src/vite-plugin-head/index.ts +++ b/packages/astro/src/vite-plugin-head/index.ts @@ -45,9 +45,34 @@ export default function configHeadVitePlugin(): vite.Plugin { return { name: 'astro:head-metadata', + enforce: 'pre', + apply: 'serve', configureServer(_server) { server = _server; }, + resolveId(source, importer) { + if(importer) { + // Do propagation any time a new module is imported. This is because + // A module with propagation might be loaded before one of its parent pages + // is loaded, in which case that parent page won't have the in-tree and containsHead + // values. Walking up the tree in resolveId ensures that they do + return this.resolve(source, importer, { skipSelf: true }).then(result => { + if(result) { + let info = this.getModuleInfo(result.id); + if(info && getAstroMetadata(info)) { + const astro = getAstroMetadata(info)!; + if(astro.propagation === 'self' || astro.propagation === 'in-tree') { + propagateMetadata.call(this, importer, 'propagation', 'in-tree'); + } + if(astro.containsHead) { + propagateMetadata.call(this, importer, 'containsHead', true); + } + } + } + return result; + }); + } + }, transform(source, id) { if (!server) { return; diff --git a/packages/astro/test/fixtures/view-transitions/package.json b/packages/astro/test/fixtures/view-transitions/package.json new file mode 100644 index 000000000000..bc6790df4328 --- /dev/null +++ b/packages/astro/test/fixtures/view-transitions/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/view-transitions", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/view-transitions/src/components/Animate.astro b/packages/astro/test/fixtures/view-transitions/src/components/Animate.astro new file mode 100644 index 000000000000..251e44176359 --- /dev/null +++ b/packages/astro/test/fixtures/view-transitions/src/components/Animate.astro @@ -0,0 +1 @@ +

diff --git a/packages/astro/test/fixtures/view-transitions/src/components/AnimateContainer.astro b/packages/astro/test/fixtures/view-transitions/src/components/AnimateContainer.astro
new file mode 100644
index 000000000000..d0ea817d547f
--- /dev/null
+++ b/packages/astro/test/fixtures/view-transitions/src/components/AnimateContainer.astro
@@ -0,0 +1 @@
+
diff --git a/packages/astro/test/fixtures/view-transitions/src/components/Animations.astro b/packages/astro/test/fixtures/view-transitions/src/components/Animations.astro
new file mode 100644
index 000000000000..6c4a8733ef40
--- /dev/null
+++ b/packages/astro/test/fixtures/view-transitions/src/components/Animations.astro
@@ -0,0 +1,4 @@
+---
+import Animate from './Animate.astro';
+---
+
diff --git a/packages/astro/test/fixtures/view-transitions/src/pages/one.astro b/packages/astro/test/fixtures/view-transitions/src/pages/one.astro
new file mode 100644
index 000000000000..dbfaaadb8eb8
--- /dev/null
+++ b/packages/astro/test/fixtures/view-transitions/src/pages/one.astro
@@ -0,0 +1,11 @@
+---
+import Animate from '../components/Animate.astro';
+---
+
+
+	Testing
+
+
+	
+
+
diff --git a/packages/astro/test/fixtures/view-transitions/src/pages/two.astro b/packages/astro/test/fixtures/view-transitions/src/pages/two.astro
new file mode 100644
index 000000000000..dea87783091e
--- /dev/null
+++ b/packages/astro/test/fixtures/view-transitions/src/pages/two.astro
@@ -0,0 +1,15 @@
+---
+
+import AnimateContainer from '../components/AnimateContainer.astro';
+import Animations from '../components/Animations.astro';
+---
+
+
+	Testing
+
+
+	
+		
+	
+
+
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 0e2fbd876fd8..906026fd7885 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -3512,6 +3512,12 @@ importers:
         specifier: workspace:*
         version: link:../../..
 
+  packages/astro/test/fixtures/view-transitions:
+    dependencies:
+      astro:
+        specifier: workspace:*
+        version: link:../../..
+
   packages/astro/test/fixtures/virtual-astro-file:
     dependencies:
       astro:

From df6b8ad43d5c67f58ead081b1511d2d46a4eaefd Mon Sep 17 00:00:00 2001
From: Matthew Phillips 
Date: Fri, 22 Sep 2023 18:11:25 -0400
Subject: [PATCH 2/4] Add changeset

---
 .changeset/fifty-comics-cross.md | 5 +++++
 1 file changed, 5 insertions(+)
 create mode 100644 .changeset/fifty-comics-cross.md

diff --git a/.changeset/fifty-comics-cross.md b/.changeset/fifty-comics-cross.md
new file mode 100644
index 000000000000..6641dea15b26
--- /dev/null
+++ b/.changeset/fifty-comics-cross.md
@@ -0,0 +1,5 @@
+---
+'astro': patch
+---
+
+Fix cases of head propagation not occuring in dev server

From 393d4ac40a3b1a03c6ff8cf8aedb178b031ea970 Mon Sep 17 00:00:00 2001
From: Matthew Phillips 
Date: Mon, 25 Sep 2023 09:35:56 -0400
Subject: [PATCH 3/4] Avoid the bang

---
 packages/astro/src/vite-plugin-head/index.ts | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/packages/astro/src/vite-plugin-head/index.ts b/packages/astro/src/vite-plugin-head/index.ts
index 4490eb7ee26c..d5f61f0071fc 100644
--- a/packages/astro/src/vite-plugin-head/index.ts
+++ b/packages/astro/src/vite-plugin-head/index.ts
@@ -59,8 +59,8 @@ export default function configHeadVitePlugin(): vite.Plugin {
 				return this.resolve(source, importer, { skipSelf: true }).then(result => {
 					if(result) {
 						let info = this.getModuleInfo(result.id);
-						if(info && getAstroMetadata(info)) {
-							const astro = getAstroMetadata(info)!;
+						const astro = info && getAstroMetadata(info);
+						if(astro) {
 							if(astro.propagation === 'self' || astro.propagation === 'in-tree') {
 								propagateMetadata.call(this, importer, 'propagation', 'in-tree');
 							}

From 475600ea0703dadf5aba9fda665c91004b321ba4 Mon Sep 17 00:00:00 2001
From: Matthew Phillips 
Date: Mon, 25 Sep 2023 09:49:24 -0400
Subject: [PATCH 4/4] Add TODOs about handling in resolveId

---
 packages/astro/src/vite-plugin-head/index.ts | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/packages/astro/src/vite-plugin-head/index.ts b/packages/astro/src/vite-plugin-head/index.ts
index d5f61f0071fc..f89049c5e47c 100644
--- a/packages/astro/src/vite-plugin-head/index.ts
+++ b/packages/astro/src/vite-plugin-head/index.ts
@@ -78,11 +78,13 @@ export default function configHeadVitePlugin(): vite.Plugin {
 				return;
 			}
 
+			// TODO This could probably be removed now that this is handled in resolveId
 			let info = this.getModuleInfo(id);
 			if (info && getAstroMetadata(info)?.containsHead) {
 				propagateMetadata.call(this, id, 'containsHead', true);
 			}
 
+			// TODO This could probably be removed now that this is handled in resolveId
 			if (info && getAstroMetadata(info)?.propagation === 'self') {
 				const mod = server.moduleGraph.getModuleById(id);
 				for (const parent of mod?.importers ?? []) {