Add Astro.generator (#4012)

* feat: add generator property and component

* chore: remove `Generator` component

* chore: update generator description

* chore: include generator in examples

* chore: update lockfile

* chore: format

* fix: do not reference core from server runtime

* chore: remove component test

Co-authored-by: Nate Moore <nate@astro.build>
This commit is contained in:
Nate Moore 2022-08-08 12:34:05 -05:00 committed by GitHub
parent 2c710e4edb
commit f207c417e0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 101 additions and 0 deletions

View file

@ -0,0 +1,18 @@
---
'astro': patch
---
Adds `Astro.generator` which can be used to add a [`<meta name="generator">`](https://html.spec.whatwg.org/multipage/semantics.html#meta-generator) tag.
```astro
<html>
<head>
<meta name="generator" content={Astro.generator} />
</head>
<body>
<footer>
<p>Built with <a href="https://astro.build">{Astro.generator}</a></p>
</footer>
</body>
</html>
```

View file

@ -12,6 +12,7 @@ const { title } = Astro.props as Props;
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
<meta name="generator" content={Astro.generator} />
<title>{title}</title>
</head>
<body>

View file

@ -14,6 +14,7 @@ const { title, description } = Astro.props;
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
<meta name="generator" content={Astro.generator} />
<!-- Primary Meta Tags -->
<title>{title}</title>

View file

@ -6,6 +6,7 @@ import * as Component from '@example/my-component';
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<title>Welcome to Astro</title>
<style is:global>
h {

View file

@ -6,6 +6,7 @@ import '../styles/index.css';
<!-- Global Metadata -->
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<link rel="alternate icon" type="image/x-icon" href="/favicon.ico" />

View file

@ -12,6 +12,7 @@ console.log({ SSR, PUBLIC_SOME_KEY });
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<title>Astro</title>
</head>
<body>

View file

@ -10,6 +10,7 @@ import Counter from '../components/Counter.astro';
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
<style>
html,

View file

@ -19,6 +19,7 @@ import SvelteCounter from '../components/SvelteCounter.svelte';
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
</head>
<body>

View file

@ -10,6 +10,7 @@ import Counter from '../components/Counter';
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
<style>
html,

View file

@ -13,6 +13,7 @@ const someProps = {
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
<style>
html,

View file

@ -10,6 +10,7 @@ import Counter from '../components/Counter';
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
<style>
html,

View file

@ -10,6 +10,7 @@ import Counter from '../components/Counter.svelte';
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
<style>
html,

View file

@ -10,6 +10,7 @@ import Counter from '../components/Counter.vue';
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
<style>
html,

View file

@ -5,6 +5,7 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<title>Astro</title>
</head>
<body>

View file

@ -2,6 +2,7 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<title>Astro</title>
</head>
<body>

View file

@ -9,6 +9,7 @@ const {
<meta charset="UTF-8" />
<meta name="description" property="og:description" content={description} />
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<title>{title}</title>
<link rel="icon" type="image/x-icon" href="/favicon.ico" />

View file

@ -11,6 +11,7 @@ import Time from '../components/Time.jsx';
<meta charset="utf-8" />
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<title>Welcome to Astro</title>
</head>

View file

@ -14,6 +14,7 @@ const { title } = Astro.props as Props;
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="generator" content={Astro.generator} />
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
<title>{title}</title>
</head>

View file

@ -11,6 +11,7 @@ import Button from '../components/Button.astro';
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
<meta name="generator" content={Astro.generator} />
<title>Astro + TailwindCSS</title>
</head>

View file

@ -6,6 +6,7 @@
<meta charset="utf-8" />
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<title>Welcome to Astro</title>
</head>

View file

@ -249,6 +249,16 @@ export interface AstroGlobalPartial {
* [Astro reference](https://docs.astro.build/en/reference/api-reference/#astrosite)
*/
site: URL | undefined;
/**
* Returns a string with the current version of Astro.
*
* Useful for using `<meta name="generator" content={Astro.generator} />` or crediting Astro in a site footer.
*
* [HTML Specification for `generator`](https://html.spec.whatwg.org/multipage/semantics.html#meta-generator)
*
* [Astro reference](https://docs.astro.build/en/reference/api-reference/#astrogenerator)
*/
generator: string;
}
type ServerConfig = {

View file

@ -38,6 +38,8 @@ const htmlBooleanAttributes =
const htmlEnumAttributes = /^(contenteditable|draggable|spellcheck|value)$/i;
// Note: SVG is case-sensitive!
const svgEnumAttributes = /^(autoReverse|externalResourcesRequired|focusable|preserveAlpha)$/i;
// process.env.PACKAGE_VERSION is injected when we build and publish the astro package.
const ASTRO_VERSION = process.env.PACKAGE_VERSION ?? 'development';
// INVESTIGATE:
// 2. Less anys when possible and make it well known when they are needed.
@ -530,6 +532,7 @@ export function createAstro(
const projectRoot = new URL(projectRootStr);
return {
site,
generator: `Astro v${ASTRO_VERSION}`,
fetchContent: createDeprecatedFetchContentFn(),
glob: createAstroGlobFn(),
// INVESTIGATE is there a use-case for multi args?

View file

@ -0,0 +1,23 @@
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';
describe('Astro generator', () => {
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/astro-generator/',
});
await fixture.build();
});
describe('build', () => {
it('Defines Astro.generator', async () => {
const html = await fixture.readFile(`/index.html`);
const $ = cheerio.load(html);
expect($('meta[name="generator"]').attr('content')).to.match(/^Astro v/);
});
});
});

View file

@ -0,0 +1,8 @@
{
"name": "@test/astro-generator",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}

View file

@ -0,0 +1,14 @@
---
let title = 'My App'
---
<html>
<head>
<meta name="generator" content={Astro.generator}>
</head>
<body>
<footer>
Built with <a href="https://astro.build">{Astro.generator}</a>
</footer>
</body>
</html>

View file

@ -1189,6 +1189,12 @@ importers:
'@astrojs/preact': link:../../../../integrations/preact
astro: link:../../..
packages/astro/test/fixtures/astro-generator:
specifiers:
astro: workspace:*
dependencies:
astro: link:../../..
packages/astro/test/fixtures/astro-get-static-paths:
specifiers:
astro: workspace:*