[ci] format
This commit is contained in:
parent
071e1dee7e
commit
bec45b3be5
5 changed files with 114 additions and 63 deletions
|
@ -12,10 +12,7 @@ export function isHeadAndContent(obj: unknown): obj is HeadAndContent {
|
||||||
return typeof obj === 'object' && !!(obj as any)[headAndContentSym];
|
return typeof obj === 'object' && !!(obj as any)[headAndContentSym];
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createHeadAndContent(
|
export function createHeadAndContent(head: string, content: RenderTemplateResult): HeadAndContent {
|
||||||
head: string,
|
|
||||||
content: RenderTemplateResult
|
|
||||||
): HeadAndContent {
|
|
||||||
return {
|
return {
|
||||||
[headAndContentSym]: true,
|
[headAndContentSym]: true,
|
||||||
head,
|
head,
|
||||||
|
|
|
@ -2,13 +2,13 @@ import type { SSRResult } from '../../../@types/astro';
|
||||||
import type { RenderInstruction } from './types.js';
|
import type { RenderInstruction } from './types.js';
|
||||||
|
|
||||||
import { HTMLBytes, markHTMLString } from '../escape.js';
|
import { HTMLBytes, markHTMLString } from '../escape.js';
|
||||||
import { renderAllHeadContent } from './head.js';
|
|
||||||
import {
|
import {
|
||||||
determineIfNeedsHydrationScript,
|
determineIfNeedsHydrationScript,
|
||||||
determinesIfNeedsDirectiveScript,
|
determinesIfNeedsDirectiveScript,
|
||||||
getPrescripts,
|
getPrescripts,
|
||||||
PrescriptType,
|
PrescriptType,
|
||||||
} from '../scripts.js';
|
} from '../scripts.js';
|
||||||
|
import { renderAllHeadContent } from './head.js';
|
||||||
import { isSlotString, type SlotString } from './slot.js';
|
import { isSlotString, type SlotString } from './slot.js';
|
||||||
|
|
||||||
export const Fragment = Symbol.for('astro:fragment');
|
export const Fragment = Symbol.for('astro:fragment');
|
||||||
|
@ -21,9 +21,9 @@ export const decoder = new TextDecoder();
|
||||||
// These directive instructions bubble all the way up to renderPage so that we
|
// These directive instructions bubble all the way up to renderPage so that we
|
||||||
// can ensure they are added only once, and as soon as possible.
|
// can ensure they are added only once, and as soon as possible.
|
||||||
export function stringifyChunk(result: SSRResult, chunk: string | SlotString | RenderInstruction) {
|
export function stringifyChunk(result: SSRResult, chunk: string | SlotString | RenderInstruction) {
|
||||||
if(typeof (chunk as any).type === 'string') {
|
if (typeof (chunk as any).type === 'string') {
|
||||||
const instruction = chunk as RenderInstruction;
|
const instruction = chunk as RenderInstruction;
|
||||||
switch(instruction.type) {
|
switch (instruction.type) {
|
||||||
case 'directive': {
|
case 'directive': {
|
||||||
const { hydration } = instruction;
|
const { hydration } = instruction;
|
||||||
let needsHydrationScript = hydration && determineIfNeedsHydrationScript(result);
|
let needsHydrationScript = hydration && determineIfNeedsHydrationScript(result);
|
||||||
|
@ -43,7 +43,7 @@ export function stringifyChunk(result: SSRResult, chunk: string | SlotString | R
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case 'head': {
|
case 'head': {
|
||||||
if(result._metadata.hasRenderedHead) {
|
if (result._metadata.hasRenderedHead) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
return renderAllHeadContent(result);
|
return renderAllHeadContent(result);
|
||||||
|
|
|
@ -39,7 +39,7 @@ export function renderAllHeadContent(result: SSRResult) {
|
||||||
return markHTMLString(content);
|
return markHTMLString(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function * renderHead(result: SSRResult) {
|
export function* renderHead(result: SSRResult) {
|
||||||
yield { type: 'head', result } as const;
|
yield { type: 'head', result } as const;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,6 @@ export type RenderDirectiveInstruction = {
|
||||||
export type RenderHeadInstruction = {
|
export type RenderHeadInstruction = {
|
||||||
type: 'head';
|
type: 'head';
|
||||||
result: SSRResult;
|
result: SSRResult;
|
||||||
}
|
};
|
||||||
|
|
||||||
export type RenderInstruction = RenderDirectiveInstruction | RenderHeadInstruction;
|
export type RenderInstruction = RenderDirectiveInstruction | RenderHeadInstruction;
|
||||||
|
|
|
@ -7,7 +7,7 @@ import {
|
||||||
renderSlot,
|
renderSlot,
|
||||||
maybeRenderHead,
|
maybeRenderHead,
|
||||||
renderHead,
|
renderHead,
|
||||||
Fragment
|
Fragment,
|
||||||
} from '../../../dist/runtime/server/index.js';
|
} from '../../../dist/runtime/server/index.js';
|
||||||
import {
|
import {
|
||||||
createBasicEnvironment,
|
createBasicEnvironment,
|
||||||
|
@ -41,41 +41,63 @@ describe('core/render', () => {
|
||||||
${renderSlot(result, slots['default'])}
|
${renderSlot(result, slots['default'])}
|
||||||
</body>
|
</body>
|
||||||
</html>`;
|
</html>`;
|
||||||
})
|
});
|
||||||
|
|
||||||
const PageLayout = createComponent((result, _props, slots) => {
|
const PageLayout = createComponent((result, _props, slots) => {
|
||||||
return render`${renderComponent(result, 'Layout', BaseLayout, {}, {
|
return render`${renderComponent(
|
||||||
'default': () => render`
|
result,
|
||||||
|
'Layout',
|
||||||
|
BaseLayout,
|
||||||
|
{},
|
||||||
|
{
|
||||||
|
default: () => render`
|
||||||
${maybeRenderHead(result)}
|
${maybeRenderHead(result)}
|
||||||
<main>
|
<main>
|
||||||
${renderSlot(result, slots['default'])}
|
${renderSlot(result, slots['default'])}
|
||||||
</main>
|
</main>
|
||||||
`,
|
`,
|
||||||
'head': () => render`
|
head: () => render`
|
||||||
${renderComponent(result, 'Fragment', Fragment, { slot: 'head' }, {
|
${renderComponent(
|
||||||
'default': () => render`${renderSlot(result, slots['head'])}`
|
result,
|
||||||
})}
|
'Fragment',
|
||||||
`
|
Fragment,
|
||||||
})}
|
{ slot: 'head' },
|
||||||
|
{
|
||||||
|
default: () => render`${renderSlot(result, slots['head'])}`,
|
||||||
|
}
|
||||||
|
)}
|
||||||
|
`,
|
||||||
|
}
|
||||||
|
)}
|
||||||
`;
|
`;
|
||||||
});
|
});
|
||||||
|
|
||||||
const Page = createComponent((result, _props) => {
|
const Page = createComponent((result, _props) => {
|
||||||
return render`${renderComponent(result, 'PageLayout', PageLayout, {}, {
|
return render`${renderComponent(
|
||||||
'default': () => render`${maybeRenderHead(result)}<div>hello world</div>`,
|
result,
|
||||||
'head': () => render`
|
'PageLayout',
|
||||||
${renderComponent(result, 'Fragment', Fragment, {slot: 'head'}, {
|
PageLayout,
|
||||||
'default': () => render`<meta charset="utf-8">`
|
{},
|
||||||
})}
|
{
|
||||||
`
|
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({
|
const ctx = createRenderContext({
|
||||||
request: new Request('http://example.com/'),
|
request: new Request('http://example.com/'),
|
||||||
links: [
|
links: [{ name: 'link', props: { rel: 'stylesheet', href: '/main.css' }, children: '' }],
|
||||||
{ name: 'link', props: {rel:'stylesheet', href:'/main.css'}, children: '' }
|
|
||||||
]
|
|
||||||
});
|
});
|
||||||
const PageModule = createAstroModule(Page);
|
const PageModule = createAstroModule(Page);
|
||||||
|
|
||||||
|
@ -97,41 +119,63 @@ describe('core/render', () => {
|
||||||
${renderSlot(result, slots['default'])}
|
${renderSlot(result, slots['default'])}
|
||||||
</body>
|
</body>
|
||||||
</html>`;
|
</html>`;
|
||||||
})
|
});
|
||||||
|
|
||||||
const PageLayout = createComponent((result, _props, slots) => {
|
const PageLayout = createComponent((result, _props, slots) => {
|
||||||
return render`${renderComponent(result, 'Layout', BaseLayout, {}, {
|
return render`${renderComponent(
|
||||||
'default': () => render`
|
result,
|
||||||
|
'Layout',
|
||||||
|
BaseLayout,
|
||||||
|
{},
|
||||||
|
{
|
||||||
|
default: () => render`
|
||||||
${maybeRenderHead(result)}
|
${maybeRenderHead(result)}
|
||||||
<main>
|
<main>
|
||||||
${renderSlot(result, slots['default'])}
|
${renderSlot(result, slots['default'])}
|
||||||
</main>
|
</main>
|
||||||
`,
|
`,
|
||||||
'head': () => render`
|
head: () => render`
|
||||||
${renderComponent(result, 'Fragment', Fragment, { slot: 'head' }, {
|
${renderComponent(
|
||||||
'default': () => render`${renderSlot(result, slots['head'])}`
|
result,
|
||||||
})}
|
'Fragment',
|
||||||
`
|
Fragment,
|
||||||
})}
|
{ slot: 'head' },
|
||||||
|
{
|
||||||
|
default: () => render`${renderSlot(result, slots['head'])}`,
|
||||||
|
}
|
||||||
|
)}
|
||||||
|
`,
|
||||||
|
}
|
||||||
|
)}
|
||||||
`;
|
`;
|
||||||
});
|
});
|
||||||
|
|
||||||
const Page = createComponent((result, _props) => {
|
const Page = createComponent((result, _props) => {
|
||||||
return render`${renderComponent(result, 'PageLayout', PageLayout, {}, {
|
return render`${renderComponent(
|
||||||
'default': () => render`${maybeRenderHead(result)}<div>hello world</div>`,
|
result,
|
||||||
'head': () => render`
|
'PageLayout',
|
||||||
${renderComponent(result, 'Fragment', Fragment, {slot: 'head'}, {
|
PageLayout,
|
||||||
'default': () => render`<meta charset="utf-8">`
|
{},
|
||||||
})}
|
{
|
||||||
`
|
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({
|
const ctx = createRenderContext({
|
||||||
request: new Request('http://example.com/'),
|
request: new Request('http://example.com/'),
|
||||||
links: [
|
links: [{ name: 'link', props: { rel: 'stylesheet', href: '/main.css' }, children: '' }],
|
||||||
{ name: 'link', props: {rel:'stylesheet', href:'/main.css'}, children: '' }
|
|
||||||
]
|
|
||||||
});
|
});
|
||||||
const PageModule = createAstroModule(Page);
|
const PageModule = createAstroModule(Page);
|
||||||
|
|
||||||
|
@ -147,26 +191,36 @@ describe('core/render', () => {
|
||||||
it('Multi-level layouts and head injection, without any content in layouts', async () => {
|
it('Multi-level layouts and head injection, without any content in layouts', async () => {
|
||||||
const BaseLayout = createComponent((result, _props, slots) => {
|
const BaseLayout = createComponent((result, _props, slots) => {
|
||||||
return render`${renderSlot(result, slots['default'])}`;
|
return render`${renderSlot(result, slots['default'])}`;
|
||||||
})
|
});
|
||||||
|
|
||||||
const PageLayout = createComponent((result, _props, slots) => {
|
const PageLayout = createComponent((result, _props, slots) => {
|
||||||
return render`${renderComponent(result, 'Layout', BaseLayout, {}, {
|
return render`${renderComponent(
|
||||||
'default': () => render`${renderSlot(result, slots['default'])} `,
|
result,
|
||||||
})}
|
'Layout',
|
||||||
|
BaseLayout,
|
||||||
|
{},
|
||||||
|
{
|
||||||
|
default: () => render`${renderSlot(result, slots['default'])} `,
|
||||||
|
}
|
||||||
|
)}
|
||||||
`;
|
`;
|
||||||
});
|
});
|
||||||
|
|
||||||
const Page = createComponent((result, _props) => {
|
const Page = createComponent((result, _props) => {
|
||||||
return render`${renderComponent(result, 'PageLayout', PageLayout, {}, {
|
return render`${renderComponent(
|
||||||
'default': () => render`${maybeRenderHead(result)}<div>hello world</div>`,
|
result,
|
||||||
})}`;
|
'PageLayout',
|
||||||
|
PageLayout,
|
||||||
|
{},
|
||||||
|
{
|
||||||
|
default: () => render`${maybeRenderHead(result)}<div>hello world</div>`,
|
||||||
|
}
|
||||||
|
)}`;
|
||||||
});
|
});
|
||||||
|
|
||||||
const ctx = createRenderContext({
|
const ctx = createRenderContext({
|
||||||
request: new Request('http://example.com/'),
|
request: new Request('http://example.com/'),
|
||||||
links: [
|
links: [{ name: 'link', props: { rel: 'stylesheet', href: '/main.css' }, children: '' }],
|
||||||
{ name: 'link', props: {rel:'stylesheet', href:'/main.css'}, children: '' }
|
|
||||||
]
|
|
||||||
});
|
});
|
||||||
const PageModule = createAstroModule(Page);
|
const PageModule = createAstroModule(Page);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue