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

Bug 6672 #7062

Merged
merged 13 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from 9 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/small-wombats-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': major
JerryWu1234 marked this conversation as resolved.
Show resolved Hide resolved
---

fix miss a head when the templaterender has a promise
3 changes: 3 additions & 0 deletions packages/astro/src/runtime/server/render/astro/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export class AstroComponentInstance {
value = await value;
}
if (isHeadAndContent(value)) {
if (this.result.extraHead.length === 0 && value.head) {
yield renderChild(value.head);
}
yield* value.content;
} else {
yield* renderChild(value);
Expand Down
45 changes: 45 additions & 0 deletions packages/astro/test/astro-content-css.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

describe('build css from the component', async () => {
let fixture;

before(async () => {
fixture = await loadFixture({ root: './fixtures/astro-content-css/' });
await fixture.build();
});

describe('Build', () => {
before(async () => {
await fixture.build();
});

it('including css and js from the component in pro', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
expect($('link[href$=".css"]').attr('href')).to.match(/^\/_astro\//);
expect($('script[src$=".js"]').attr('src')).to.match(/^\/_astro\//);
});
})

describe('Dev', () => {
let devServer
before(async () => {
devServer = await fixture.startDevServer();
});

after(async () => {
devServer.stop();
});

it('ncluding css and js from the component in Dev', async () => {
let res = await fixture.fetch(`/`);
expect(res.status).to.equal(200);
const html = await res.text();
const $ = cheerio.load(html);
expect($.html()).to.include('CornflowerBlue');
expect($('script[src$=".js"]').attr('src')).to.include('astro');
});
})
})
1 change: 1 addition & 0 deletions packages/astro/test/astro-content-css/src/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="astro/client" />
11 changes: 11 additions & 0 deletions packages/astro/test/fixtures/astro-content-css/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineConfig } from 'astro/config';

import mdx from "@astrojs/mdx";

// https://astro.build/config
export default defineConfig({
build: {
format: 'file'
},
integrations: [mdx()]
JerryWu1234 marked this conversation as resolved.
Show resolved Hide resolved
});
9 changes: 9 additions & 0 deletions packages/astro/test/fixtures/astro-content-css/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@test/astro-content-css",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*",
"@astrojs/mdx": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// 1. Import utilities from `astro:content`
import { z, defineCollection } from 'astro:content';
// 2. Define a schema for each collection you'd like to validate.
const dynamicCollection = defineCollection({
schema: z.object({
title: z.string(),
}),
});
// 3. Export a single `collections` object to register your collection(s)
export const collections = {
dynamic: dynamicCollection,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
const { text } = Astro.props;
---
<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8" /></head>
<body>
<div id="first">1st components with js. Props: {text}. <span>Styles</span>. JS: </div>
</body>
</html>
<script>
document.querySelector('#first').innerHTML += 'works';
</script>
<style>
#first > span {
color: CornflowerBlue;
}
</style>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: 'First component'
---

import FirstDynamicComponentWithJS from './FirstComponentWithJS.astro';

<FirstDynamicComponentWithJS text={props.mdProps} />

Additional text from mdx 'first-component-with-js'
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
import { getCollection } from 'astro:content';

const entries = await getCollection('dynamic');
---

<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8" /></head>
<body>
{entries.map(async entry => {
const { Content } = await entry.render();
return <Content mdProps="work" />;
})}
</body>
</html>
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

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