-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Simplified head injection #6034
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Ensure CSS injections properly when using multiple layouts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
import type { SSRResult } from '../../../@types/astro'; | ||
|
||
import { markHTMLString } from '../escape.js'; | ||
import { renderChild } from './any.js'; | ||
import { renderElement } from './util.js'; | ||
|
||
// Filter out duplicate elements in our set | ||
|
@@ -13,14 +12,8 @@ const uniqueElements = (item: any, index: number, all: any[]) => { | |
); | ||
}; | ||
|
||
async function* renderExtraHead(result: SSRResult, base: string) { | ||
yield base; | ||
for (const part of result.extraHead) { | ||
yield* renderChild(part); | ||
} | ||
} | ||
|
||
function renderAllHeadContent(result: SSRResult) { | ||
export function renderAllHeadContent(result: SSRResult) { | ||
result._metadata.hasRenderedHead = true; | ||
const styles = Array.from(result.styles) | ||
.filter(uniqueElements) | ||
.map((style) => renderElement('style', style)); | ||
|
@@ -35,29 +28,31 @@ function renderAllHeadContent(result: SSRResult) { | |
.filter(uniqueElements) | ||
.map((link) => renderElement('link', link, false)); | ||
|
||
const baseHeadContent = markHTMLString(links.join('\n') + styles.join('\n') + scripts.join('\n')); | ||
let content = links.join('\n') + styles.join('\n') + scripts.join('\n'); | ||
|
||
if (result.extraHead.length > 0) { | ||
return renderExtraHead(result, baseHeadContent); | ||
} else { | ||
return baseHeadContent; | ||
for (const part of result.extraHead) { | ||
content += part; | ||
} | ||
} | ||
} | ||
|
||
export function createRenderHead(result: SSRResult) { | ||
result._metadata.hasRenderedHead = true; | ||
return renderAllHeadContent.bind(null, result); | ||
return markHTMLString(content); | ||
} | ||
|
||
export const renderHead = createRenderHead; | ||
export function * renderHead(result: SSRResult) { | ||
yield { type: 'head', result } as const; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a rendering instruction, like what we have for client directives, which tells the page renderer to inject head at the point (if it hasn't already been injected). |
||
} | ||
|
||
// This function is called by Astro components that do not contain a <head> component | ||
// This accommodates the fact that using a <head> is optional in Astro, so this | ||
// is called before a component's first non-head HTML element. If the head was | ||
// already injected it is a noop. | ||
export async function* maybeRenderHead(result: SSRResult) { | ||
export function* maybeRenderHead(result: SSRResult) { | ||
if (result._metadata.hasRenderedHead) { | ||
return; | ||
} | ||
yield createRenderHead(result)(); | ||
|
||
// This is an instruction informing the page rendering that head might need rendering. | ||
// This allows the page to deduplicate head injections. | ||
yield { type: 'head', result } as const; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,15 @@ | ||
import type { SSRResult } from '../../../@types/astro'; | ||
import type { HydrationMetadata } from '../hydration.js'; | ||
|
||
export interface RenderInstruction { | ||
export type RenderDirectiveInstruction = { | ||
type: 'directive'; | ||
result: SSRResult; | ||
hydration: HydrationMetadata; | ||
}; | ||
|
||
export type RenderHeadInstruction = { | ||
type: 'head'; | ||
result: SSRResult; | ||
} | ||
|
||
export type RenderInstruction = RenderDirectiveInstruction | RenderHeadInstruction; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
import { expect } from 'chai'; | ||
|
||
import { | ||
createComponent, | ||
render, | ||
renderComponent, | ||
renderSlot, | ||
maybeRenderHead, | ||
renderHead, | ||
Fragment | ||
} from '../../../dist/runtime/server/index.js'; | ||
import { | ||
createBasicEnvironment, | ||
createRenderContext, | ||
renderPage, | ||
} from '../../../dist/core/render/index.js'; | ||
import { defaultLogging as logging } from '../../test-utils.js'; | ||
import * as cheerio from 'cheerio'; | ||
|
||
const createAstroModule = (AstroComponent) => ({ default: AstroComponent }); | ||
|
||
describe('core/render', () => { | ||
describe('Injected head contents', () => { | ||
let env; | ||
before(async () => { | ||
env = createBasicEnvironment({ | ||
logging, | ||
renderers: [], | ||
}); | ||
}); | ||
|
||
it('Multi-level layouts and head injection, with explicit head', async () => { | ||
const BaseLayout = createComponent((result, _props, slots) => { | ||
return render`<html> | ||
<head> | ||
${renderSlot(result, slots['head'])} | ||
${renderHead(result)} | ||
</head> | ||
${maybeRenderHead(result)} | ||
<body> | ||
${renderSlot(result, slots['default'])} | ||
</body> | ||
</html>`; | ||
}) | ||
|
||
const PageLayout = createComponent((result, _props, slots) => { | ||
return render`${renderComponent(result, 'Layout', BaseLayout, {}, { | ||
'default': () => render` | ||
${maybeRenderHead(result)} | ||
<main> | ||
${renderSlot(result, slots['default'])} | ||
</main> | ||
`, | ||
'head': () => render` | ||
${renderComponent(result, 'Fragment', Fragment, { slot: 'head' }, { | ||
'default': () => render`${renderSlot(result, slots['head'])}` | ||
})} | ||
` | ||
})} | ||
`; | ||
}); | ||
|
||
const Page = createComponent((result, _props) => { | ||
return render`${renderComponent(result, 'PageLayout', PageLayout, {}, { | ||
'default': () => render`${maybeRenderHead(result)}<div>hello world</div>`, | ||
'head': () => render` | ||
${renderComponent(result, 'Fragment', Fragment, {slot: 'head'}, { | ||
'default': () => render`<meta charset="utf-8">` | ||
})} | ||
` | ||
})}`; | ||
}); | ||
|
||
const ctx = createRenderContext({ | ||
request: new Request('http://example.com/'), | ||
links: [ | ||
{ name: 'link', props: {rel:'stylesheet', href:'/main.css'}, children: '' } | ||
] | ||
}); | ||
const PageModule = createAstroModule(Page); | ||
|
||
const response = await renderPage(PageModule, ctx, env); | ||
|
||
const html = await response.text(); | ||
const $ = cheerio.load(html); | ||
|
||
expect($('head link')).to.have.a.lengthOf(1); | ||
expect($('body link')).to.have.a.lengthOf(0); | ||
}); | ||
|
||
it('Multi-level layouts and head injection, without explicit head', async () => { | ||
const BaseLayout = createComponent((result, _props, slots) => { | ||
return render`<html> | ||
${renderSlot(result, slots['head'])} | ||
${maybeRenderHead(result)} | ||
<body> | ||
${renderSlot(result, slots['default'])} | ||
</body> | ||
</html>`; | ||
}) | ||
|
||
const PageLayout = createComponent((result, _props, slots) => { | ||
return render`${renderComponent(result, 'Layout', BaseLayout, {}, { | ||
'default': () => render` | ||
${maybeRenderHead(result)} | ||
<main> | ||
${renderSlot(result, slots['default'])} | ||
</main> | ||
`, | ||
'head': () => render` | ||
${renderComponent(result, 'Fragment', Fragment, { slot: 'head' }, { | ||
'default': () => render`${renderSlot(result, slots['head'])}` | ||
})} | ||
` | ||
})} | ||
`; | ||
}); | ||
|
||
const Page = createComponent((result, _props) => { | ||
return render`${renderComponent(result, 'PageLayout', PageLayout, {}, { | ||
'default': () => render`${maybeRenderHead(result)}<div>hello world</div>`, | ||
'head': () => render` | ||
${renderComponent(result, 'Fragment', Fragment, {slot: 'head'}, { | ||
'default': () => render`<meta charset="utf-8">` | ||
})} | ||
` | ||
})}`; | ||
}); | ||
|
||
const ctx = createRenderContext({ | ||
request: new Request('http://example.com/'), | ||
links: [ | ||
{ name: 'link', props: {rel:'stylesheet', href:'/main.css'}, children: '' } | ||
] | ||
}); | ||
const PageModule = createAstroModule(Page); | ||
|
||
const response = await renderPage(PageModule, ctx, env); | ||
|
||
const html = await response.text(); | ||
const $ = cheerio.load(html); | ||
|
||
expect($('head link')).to.have.a.lengthOf(1); | ||
expect($('body link')).to.have.a.lengthOf(0); | ||
}); | ||
|
||
it('Multi-level layouts and head injection, without any content in layouts', async () => { | ||
const BaseLayout = createComponent((result, _props, slots) => { | ||
return render`${renderSlot(result, slots['default'])}`; | ||
}) | ||
|
||
const PageLayout = createComponent((result, _props, slots) => { | ||
return render`${renderComponent(result, 'Layout', BaseLayout, {}, { | ||
'default': () => render`${renderSlot(result, slots['default'])} `, | ||
})} | ||
`; | ||
}); | ||
|
||
const Page = createComponent((result, _props) => { | ||
return render`${renderComponent(result, 'PageLayout', PageLayout, {}, { | ||
'default': () => render`${maybeRenderHead(result)}<div>hello world</div>`, | ||
})}`; | ||
}); | ||
|
||
const ctx = createRenderContext({ | ||
request: new Request('http://example.com/'), | ||
links: [ | ||
{ name: 'link', props: {rel:'stylesheet', href:'/main.css'}, children: '' } | ||
] | ||
}); | ||
const PageModule = createAstroModule(Page); | ||
|
||
const response = await renderPage(PageModule, ctx, env); | ||
|
||
const html = await response.text(); | ||
const $ = cheerio.load(html); | ||
|
||
expect($('link')).to.have.a.lengthOf(1); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing this function because it causes head rendering to be async. We weren't utilizing this capability anyways. When/if we allow components to inject head content we'll likely want to bring this back, through a small refactor.