Skip to content

Commit

Permalink
Avoid implicit head injection when there is a head element in the tree (
Browse files Browse the repository at this point in the history
#6638)

* Avoid implicit head injection when there is a head element in the tree

* more

* only do it once

* Update the tests

* Update more tests

* update compiler version

* See if scope stuff can be removed now

* Move up where head injection occurs

* Remove result scoping
  • Loading branch information
matthewp authored Mar 24, 2023
1 parent 8bd0ca0 commit 7daef9a
Show file tree
Hide file tree
Showing 37 changed files with 260 additions and 300 deletions.
5 changes: 5 additions & 0 deletions .changeset/tender-hounds-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Avoid implicit head injection when a head is in the tree
2 changes: 1 addition & 1 deletion packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"test:e2e:match": "playwright test -g"
},
"dependencies": {
"@astrojs/compiler": "^1.2.0",
"@astrojs/compiler": "^1.3.0",
"@astrojs/language-server": "^0.28.3",
"@astrojs/markdown-remark": "^2.1.2",
"@astrojs/telemetry": "^2.1.0",
Expand Down
8 changes: 7 additions & 1 deletion packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1578,6 +1578,7 @@ export interface SSRMetadata {
hasHydrationScript: boolean;
hasDirectives: Set<string>;
hasRenderedHead: boolean;
headInTree: boolean;
}

/**
Expand All @@ -1592,11 +1593,16 @@ export interface SSRMetadata {
*/
export type PropagationHint = 'none' | 'self' | 'in-tree';

export type SSRComponentMetadata = {
propagation: PropagationHint,
containsHead: boolean
};

export interface SSRResult {
styles: Set<SSRElement>;
scripts: Set<SSRElement>;
links: Set<SSRElement>;
propagation: Map<string, PropagationHint>;
componentMetadata: Map<string, SSRComponentMetadata>;
propagators: Map<AstroComponentFactory, AstroComponentInstance>;
extraHead: Array<string>;
cookies: AstroCookies | undefined;
Expand Down
3 changes: 1 addition & 2 deletions packages/astro/src/content/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { prependForwardSlash } from '../core/path.js';
import {
createComponent,
createHeadAndContent,
createScopedResult,
renderComponent,
renderScriptElement,
renderStyleElement,
Expand Down Expand Up @@ -180,7 +179,7 @@ async function render({
return createHeadAndContent(
unescapeHTML(styles + links + scripts) as any,
renderTemplate`${renderComponent(
createScopedResult(result),
result,
'Content',
mod.Content,
props,
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/core/app/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ export function deserializeManifest(serializedManifest: SerializedSSRManifest):
}

const assets = new Set<string>(serializedManifest.assets);
const propagation = new Map(serializedManifest.propagation);
const componentMetadata = new Map(serializedManifest.componentMetadata);

return {
...serializedManifest,
assets,
propagation,
componentMetadata,
routes,
};
}
2 changes: 1 addition & 1 deletion packages/astro/src/core/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export class App {
request,
origin: url.origin,
pathname,
propagation: this.#manifest.propagation,
componentMetadata: this.#manifest.componentMetadata,
scripts,
links,
route: routeData,
Expand Down
8 changes: 4 additions & 4 deletions packages/astro/src/core/app/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { MarkdownRenderingOptions } from '@astrojs/markdown-remark';
import type {
ComponentInstance,
PropagationHint,
SSRComponentMetadata,
RouteData,
SerializedRouteData,
SSRLoadedRenderer,
Expand Down Expand Up @@ -36,13 +36,13 @@ export interface SSRManifest {
renderers: SSRLoadedRenderer[];
entryModules: Record<string, string>;
assets: Set<string>;
propagation: SSRResult['propagation'];
componentMetadata: SSRResult['componentMetadata'];
}

export type SerializedSSRManifest = Omit<SSRManifest, 'routes' | 'assets' | 'propagation'> & {
export type SerializedSSRManifest = Omit<SSRManifest, 'routes' | 'assets' | 'componentMetadata'> & {
routes: SerializedRouteInfo[];
assets: string[];
propagation: readonly [string, PropagationHint][];
componentMetadata: [string, SSRComponentMetadata][];
};

export type AdapterCreateExports<T = any> = (
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ async function generatePath(
origin,
pathname,
request: createRequest({ url, headers: new Headers(), logging, ssr }),
propagation: internals.propagation,
componentMetadata: internals.componentMetadata,
scripts,
links,
route: pageData.route,
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/core/build/graph.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { GetModuleInfo, ModuleInfo } from 'rollup';
import type { ViteDevServer } from 'vite';

import { resolvedPagesVirtualModuleId } from '../app/index.js';

Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/core/build/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export interface BuildInternals {
staticFiles: Set<string>;
// The SSR entry chunk. Kept in internals to share between ssr/client build steps
ssrEntryChunk?: OutputChunk;
propagation: SSRResult['propagation'];
componentMetadata: SSRResult['componentMetadata'];
}

/**
Expand Down Expand Up @@ -107,7 +107,7 @@ export function createBuildInternals(): BuildInternals {
discoveredClientOnlyComponents: new Map(),
discoveredScripts: new Set(),
staticFiles: new Set(),
propagation: new Map(),
componentMetadata: new Map(),
};
}

Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/core/build/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { astroConfigBuildPlugin } from '../../../content/vite-plugin-content-assets.js';
import { astroHeadPropagationBuildPlugin } from '../../../vite-plugin-head-propagation/index.js';
import { astroHeadBuildPlugin } from '../../../vite-plugin-head/index.js';
import type { AstroBuildPluginContainer } from '../plugin';
import { pluginAliasResolve } from './plugin-alias-resolve.js';
import { pluginAnalyzer } from './plugin-analyzer.js';
Expand All @@ -18,7 +18,7 @@ export function registerAllPlugins({ internals, options, register }: AstroBuildP
register(pluginInternals(internals));
register(pluginPages(options, internals));
register(pluginCSS(options, internals));
register(astroHeadPropagationBuildPlugin(options, internals));
register(astroHeadBuildPlugin(options, internals));
register(pluginPrerender(options, internals));
register(astroConfigBuildPlugin(options, internals));
register(pluginHoistedScripts(options, internals));
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/build/plugins/plugin-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ function buildManifest(
base: settings.config.base,
markdown: settings.config.markdown,
pageMap: null as any,
propagation: Array.from(internals.propagation),
componentMetadata: Array.from(internals.componentMetadata),
renderers: [],
entryModules,
assets: staticFiles.map((s) => settings.config.base + s),
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/core/create-vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { vitePluginAstroServer } from '../vite-plugin-astro-server/index.js';
import astroVitePlugin from '../vite-plugin-astro/index.js';
import configAliasVitePlugin from '../vite-plugin-config-alias/index.js';
import envVitePlugin from '../vite-plugin-env/index.js';
import astroHeadPropagationPlugin from '../vite-plugin-head-propagation/index.js';
import astroHeadPlugin from '../vite-plugin-head/index.js';
import htmlVitePlugin from '../vite-plugin-html/index.js';
import { astroInjectEnvTsPlugin } from '../vite-plugin-inject-env-ts/index.js';
import astroIntegrationsContainerPlugin from '../vite-plugin-integrations-container/index.js';
Expand Down Expand Up @@ -121,7 +121,7 @@ export async function createVite(
astroPostprocessVitePlugin({ settings }),
astroIntegrationsContainerPlugin({ settings, logging }),
astroScriptsPageSSRPlugin({ settings }),
astroHeadPropagationPlugin({ settings }),
astroHeadPlugin({ settings }),
astroScannerPlugin({ settings }),
astroInjectEnvTsPlugin({ settings, logging, fs }),
astroContentVirtualModPlugin({ settings }),
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/render/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface RenderContext {
scripts?: Set<SSRElement>;
links?: Set<SSRElement>;
styles?: Set<SSRElement>;
propagation?: SSRResult['propagation'];
componentMetadata?: SSRResult['componentMetadata'];
route?: RouteData;
status?: number;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/render/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export async function renderPage(mod: ComponentInstance, ctx: RenderContext, env
params,
props: pageProps,
pathname: ctx.pathname,
propagation: ctx.propagation,
componentMetadata: ctx.componentMetadata,
resolve: env.resolve,
renderers: env.renderers,
request: ctx.request,
Expand Down
34 changes: 0 additions & 34 deletions packages/astro/src/core/render/dev/head.ts

This file was deleted.

10 changes: 5 additions & 5 deletions packages/astro/src/core/render/dev/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { createRenderContext, renderPage as coreRenderPage } from '../index.js';
import { filterFoundRenderers, loadRenderer } from '../renderer.js';
import { getStylesForURL } from './css.js';
import type { DevelopmentEnvironment } from './environment';
import { getPropagationMap } from './head.js';
import { getComponentMetadata } from './metadata.js';
import { getScriptsForURL } from './scripts.js';
export { createDevelopmentEnvironment } from './environment.js';
export type { DevelopmentEnvironment };
Expand Down Expand Up @@ -142,9 +142,9 @@ async function getScriptsAndStyles({ env, filePath }: GetScriptsAndStylesParams)
});
});

const propagationMap = await getPropagationMap(filePath, env.loader);
const metadata = await getComponentMetadata(filePath, env.loader);

return { scripts, styles, links, propagationMap };
return { scripts, styles, links, metadata };
}

export async function renderPage(options: SSROptions): Promise<Response> {
Expand All @@ -154,7 +154,7 @@ export async function renderPage(options: SSROptions): Promise<Response> {
// The new instances are passed through.
options.env.renderers = renderers;

const { scripts, links, styles, propagationMap } = await getScriptsAndStyles({
const { scripts, links, styles, metadata } = await getScriptsAndStyles({
env: options.env,
filePath: options.filePath,
});
Expand All @@ -166,7 +166,7 @@ export async function renderPage(options: SSROptions): Promise<Response> {
scripts,
links,
styles,
propagation: propagationMap,
componentMetadata: metadata,
route: options.route,
});

Expand Down
47 changes: 47 additions & 0 deletions packages/astro/src/core/render/dev/metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { SSRResult, SSRComponentMetadata } from '../../../@types/astro';

import type { ModuleInfo, ModuleLoader } from '../../module-loader/index';

import { getAstroMetadata } from '../../../vite-plugin-astro/index.js';
import { viteID } from '../../util.js';
import { crawlGraph } from './vite.js';

export async function getComponentMetadata(
filePath: URL,
loader: ModuleLoader
): Promise<SSRResult['componentMetadata']> {
const map: SSRResult['componentMetadata'] = new Map();

const rootID = viteID(filePath);
addMetadata(map, loader.getModuleInfo(rootID));
for await (const moduleNode of crawlGraph(loader, rootID, true)) {
const id = moduleNode.id;
if (id) {
addMetadata(map, loader.getModuleInfo(id));
}
}

return map;
}

function addMetadata(
map: SSRResult['componentMetadata'],
modInfo: ModuleInfo | null
) {
if (modInfo) {
const astro = getAstroMetadata(modInfo);
if(astro) {
let metadata: SSRComponentMetadata = {
containsHead: false,
propagation: 'none'
};
if(astro.propagation) {
metadata.propagation = astro.propagation;
}
if(astro.containsHead) {
metadata.containsHead = astro.containsHead;
}
map.set(modInfo.id, metadata);
}
}
}
Loading

0 comments on commit 7daef9a

Please sign in to comment.