-
Notifications
You must be signed in to change notification settings - Fork 86
fix(astro): better default meta tags #342
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
fc65e52
better default meta tags
eric-burel 820d03f
reuse logic for computing the logo and favicon path, document BASE_URL
eric-burel 7a20abe
better default meta tags
eric-burel 8a3a029
reuse logic for computing the logo and favicon path, document BASE_URL
eric-burel f12f9fd
set metadata in a lesson
eric-burel 122de0e
use Astro.site
eric-burel 9b6a148
get site value from environment
eric-burel a9c633d
separate document title and meta title, finalize default meta tags
eric-burel d7729bc
make favicon URL non-absolute
eric-burel b967dac
rename public image to public asset
eric-burel af500a7
remove fr i18n param from site configuration link
eric-burel 4435b2c
use nullish coalescing and better meta image description
eric-burel 5b834e8
Merge branch 'main' into default-meta
AriPerkkio 211ba74
fix: code review
AriPerkkio 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 hidden or 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 hidden or 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 hidden or 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,31 @@ | ||
--- | ||
import type { MetaTagsConfig } from '@tutorialkit/types'; | ||
import { readLogoFile } from '../utils/logo'; | ||
import { readPublicAsset } from '../utils/publicAsset'; | ||
|
||
interface Props { | ||
meta?: MetaTagsConfig; | ||
} | ||
const { meta = {} } = Astro.props; | ||
let imageUrl; | ||
if (meta.image) { | ||
imageUrl = readPublicAsset(meta.image, true); | ||
if (!imageUrl) { | ||
console.warn(`Image ${meta.image} not found in "/public" folder`); | ||
} | ||
} | ||
imageUrl ??= readLogoFile('logo', true); | ||
--- | ||
|
||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width" /> | ||
<meta name="generator" content={Astro.generator} /> | ||
{meta.description ? <meta name="description" content={meta.description} /> : null} | ||
{/* open graph */} | ||
{meta.title ? <meta name="og:title" content={meta.title} /> : null} | ||
{meta.description ? <meta name="og:description" content={meta.description} /> : null} | ||
{imageUrl ? <meta name="og:image" content={imageUrl} /> : null} | ||
{/* twitter */} | ||
{meta.title ? <meta name="twitter:title" content={meta.title} /> : null} | ||
{meta.description ? <meta name="twitter:description" content={meta.description} /> : null} | ||
{imageUrl ? <meta name="twitter:image" content={imageUrl} /> : null} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,17 @@ | ||
import { LOGO_EXTENSIONS } from './constants'; | ||
import { readPublicAsset } from './publicAsset'; | ||
|
||
export function readLogoFile(logoPrefix: string = 'logo', absolute?: boolean) { | ||
let logo; | ||
|
||
for (const logoExt of LOGO_EXTENSIONS) { | ||
const logoFilename = `${logoPrefix}.${logoExt}`; | ||
logo = readPublicAsset(logoFilename, absolute); | ||
|
||
if (logo) { | ||
break; | ||
} | ||
} | ||
|
||
return logo; | ||
} |
This file contains hidden or 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,27 @@ | ||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
import { joinPaths } from './url'; | ||
|
||
export function readPublicAsset(filename: string, absolute?: boolean) { | ||
let asset; | ||
const exists = fs.existsSync(path.join('public', filename)); | ||
|
||
if (!exists) { | ||
return; | ||
} | ||
|
||
asset = joinPaths(import.meta.env.BASE_URL, filename); | ||
|
||
if (absolute) { | ||
const site = import.meta.env.SITE; | ||
|
||
if (!site) { | ||
// the SITE env variable inherits the value from Astro.site configuration | ||
console.warn('Trying to compute an absolute file URL but Astro.site is not set.'); | ||
} else { | ||
asset = joinPaths(site, asset); | ||
} | ||
} | ||
|
||
return asset; | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,16 @@ | ||
import { z } from 'zod'; | ||
|
||
export const metaTagsSchema = z.object({ | ||
image: z | ||
.string() | ||
.optional() | ||
/** | ||
* Ideally we would want to use `image` from: | ||
* https://docs.astro.build/en/guides/images/#images-in-content-collections . | ||
*/ | ||
.describe('A relative path to an image that lives in the public folder to show on social previews.'), | ||
description: z.string().optional().describe('A description for metadata'), | ||
title: z.string().optional().describe('A title to use specifically for metadata'), | ||
}); | ||
|
||
export type MetaTagsSchema = z.infer<typeof metaTagsSchema>; |
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.
Uh oh!
There was an error while loading. Please reload this page.