Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Head propagation graph walking on new pages #8646

Merged
merged 4 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fifty-comics-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix cases of head propagation not occuring in dev server
27 changes: 27 additions & 0 deletions packages/astro/src/vite-plugin-head/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,46 @@ export default function configHeadVitePlugin(): vite.Plugin {

return {
name: 'astro:head-metadata',
enforce: 'pre',
apply: 'serve',
configureServer(_server) {
server = _server;
},
resolveId(source, importer) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're handling the propagation in resolveId. Could we remove the similar code in the transform hook entirely? Or do they work differently?

Copy link
Contributor Author

@matthewp matthewp Sep 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about this... I think we probably could. I don't fully trust the test coverage though. I'd rather leave a TODO and try and remove it later when feeling more brave.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me 👍 Probably worth circle back whenever possible as it touches on perf

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);
const astro = info && getAstroMetadata(info);
if(astro) {
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;
}

// 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 ?? []) {
Expand Down
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/view-transitions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/view-transitions",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<pre transition:name="animate"></pre>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<slot/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
import Animate from './Animate.astro';
---
<Animate />
11 changes: 11 additions & 0 deletions packages/astro/test/fixtures/view-transitions/src/pages/one.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
import Animate from '../components/Animate.astro';
---
<html>
<head>
<title>Testing</title>
</head>
<body>
<Animate />
</body>
</html>
15 changes: 15 additions & 0 deletions packages/astro/test/fixtures/view-transitions/src/pages/two.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---

import AnimateContainer from '../components/AnimateContainer.astro';
import Animations from '../components/Animations.astro';
---
<html>
<head>
<title>Testing</title>
</head>
<body>
<AnimateContainer>
<Animations />
</AnimateContainer>
</body>
</html>
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.