Skip to content

Commit

Permalink
build: new sitemap and robots
Browse files Browse the repository at this point in the history
  • Loading branch information
alextim committed Jun 21, 2022
1 parent 8783408 commit 2258aa9
Show file tree
Hide file tree
Showing 29 changed files with 807 additions and 275 deletions.
4 changes: 2 additions & 2 deletions examples/robots-txt/advanced/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"preview": "astro preview"
},
"devDependencies": {
"@astrojs/sitemap": "^0.1.2",
"astro": "^1.0.0-beta.47",
"@astrojs/sitemap": "^0.2.1",
"astro": "^1.0.0-beta.50",
"astro-robots-txt": "latest"
}
}
2 changes: 1 addition & 1 deletion examples/robots-txt/advanced/robots-txt.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const robotsConfig = {
disallow: siteConfig.disableIndexing ? '/' : '',
},
],
sitemapBaseFileName: 'your-sitemap', // default 'sitemap'
sitemapBaseFileName: 'sitemap-index', // default 'sitemap-index'
transform(content) {
const commentsList = ['Some text before main content', 'Second line', 'Third line'];
const comments = commentsList.map((item) => `# ${item}.`).join('\n');
Expand Down
4 changes: 2 additions & 2 deletions examples/robots-txt/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"preview": "astro preview"
},
"devDependencies": {
"@astrojs/sitemap": "^0.1.2",
"astro": "^1.0.0-beta.47",
"@astrojs/sitemap": "^0.2.1",
"astro": "^1.0.0-beta.50",
"astro-robots-txt": "latest"
}
}
4 changes: 2 additions & 2 deletions examples/sitemap/advanced/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Demo for astro-sitemap integration

This demo uses the `astro-sitemap` instead of official `@astrojs/sitemap` integration.
This demo shows the advanced usage of `astro-sitemap` integration with an external configuration.

External configuration is used.
XSL template code is based on this [repo](https://github.com/pedroborges/xml-sitemap-stylesheet).

Play this demo [online](https://stackblitz.com/fork/github/alextim/astro-lib/tree/main/examples/sitemap/advanced).
4 changes: 2 additions & 2 deletions examples/sitemap/advanced/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@example/sitemap-advanced",
"version": "0.0.1",
"version": "0.0.2",
"private": true,
"license": "MIT",
"type": "module",
Expand All @@ -11,7 +11,7 @@
"preview": "astro preview"
},
"devDependencies": {
"astro": "^1.0.0-beta.47",
"astro": "^1.0.0-beta.50",
"astro-sitemap": "latest"
}
}
206 changes: 206 additions & 0 deletions examples/sitemap/advanced/public/sitemap.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"
xmlns:xhtml="http://www.w3.org/1999/xhtml">

<xsl:output method="html" indent="yes" encoding="UTF-8"/>

<xsl:template match="/">
<html>
<head>
<title>
Sitemap
<xsl:if test="sitemap:sitemapindex">Index</xsl:if>
</title>
<link rel="stylesheet" href="https://unpkg.com/tachyons@4.6.1/css/tachyons.min.css"/>
</head>
<body class="ph3 pb3 mid-gray">
<header class="mw8 pv4 center">
<div class="flex items-center">
<h1 class="ma0 mr2 f2 blue">Sitemap</h1>
<xsl:if test="sitemap:sitemapindex">
<span class="dib mr2 ph3 pv1 f6 normal mid-gray bg-light-blue br-pill">Index</span>
</xsl:if>
<xsl:if test="sitemap:urlset/sitemap:url/image:image">
<span class="dib mr2 ph3 pv1 f6 normal mid-gray bg-light-blue br-pill">Images</span>
</xsl:if>
<xsl:if test="sitemap:urlset/sitemap:url/video:video">
<span class="dib mr2 ph3 pv1 f6 normal mid-gray bg-light-blue br-pill">Video</span>
</xsl:if>
<xsl:if test="sitemap:urlset/sitemap:url/xhtml:link">
<span class="dib mr2 ph3 pv1 f6 normal mid-gray bg-light-blue br-pill">Xhtml</span>
</xsl:if>
</div>
<h2 class="ma0 mt4 f4 normal">
<xsl:choose>
<xsl:when test="sitemap:sitemapindex">
This index contains
<strong class="blue"><xsl:value-of select="count(sitemap:sitemapindex/sitemap:sitemap)"/></strong>
sitemaps.
</xsl:when>
<xsl:otherwise>
This index contains
<strong class="blue"><xsl:value-of select="count(sitemap:urlset/sitemap:url)"/></strong>
URLs.
</xsl:otherwise>
</xsl:choose>
</h2>
<p>
This is an XML sitemap, meant for consumption by search engines.<br/>
You can find more information about XML sitemaps on <a href="https://sitemaps.org" class="link blue">sitemaps.org</a>.
</p>
</header>

<xsl:apply-templates/>

<footer class="mw8 center pv4 tc">
See more Astro integrations on <a href="https://github.com/alextim/astro-lib" title="Go to Github" class="link blue">GitHub</a>
</footer>

</body>
</html>
</xsl:template>


<xsl:template match="sitemap:sitemapindex">
<div class="mw8 center">
<div class="overflow-auto">
<table class="w-100 f6 b--silver ba bw1" cellspacing="0">
<thead class="bg-silver">
<tr>
<th class="pa3 fw6 tl dark-gray" style="width:60px"></th>
<th class="pa3 fw6 tl dark-gray">URL</th>
<th class="pa3 fw6 tr dark-gray" style="width:200px">Last Modified</th>
</tr>
</thead>
<tbody class="lh-copy bg-near-white">
<xsl:for-each select="sitemap:sitemap">
<tr class="hover-bg-white">
<xsl:variable name="loc">
<xsl:value-of select="sitemap:loc"/>
</xsl:variable>
<xsl:variable name="pno">
<xsl:value-of select="position()"/>
</xsl:variable>
<td class="pa3 tc b bb b--silver">
<xsl:value-of select="$pno"/>
</td>
<td class="pa3 b bb b--silver">
<a href="{$loc}" class="link blue">
<xsl:value-of select="sitemap:loc"/>
</a>
</td>
<xsl:if test="sitemap:lastmod">
<td class="pa3 tr bb b--silver">
<xsl:value-of select="concat(substring(sitemap:lastmod, 0, 11), concat(' ', substring(sitemap:lastmod, 12, 5)), concat(' ', substring(sitemap:lastmod, 20, 6)))"/>
</td>
</xsl:if>
<xsl:apply-templates/>
</tr>
</xsl:for-each>
</tbody>
</table>
</div>
</div>
</xsl:template>

<xsl:template match="sitemap:urlset">
<div class="mw8 center">
<div class="overflow-auto">
<table class="w-100 f6 b--silver ba bw1" cellspacing="0">
<thead class="bg-silver">
<tr>
<th class="pa3 fw6 tl dark-gray" style="width:60px"></th>
<th class="pa3 fw6 tl dark-gray">URL</th>
<xsl:if test="sitemap:url/sitemap:changefreq">
<th class="pa3 fw6 tr dark-gray" style="width:130px">Change Freq.</th>
</xsl:if>
<xsl:if test="sitemap:url/sitemap:priority">
<th class="pa3 fw6 tr dark-gray" style="width:90px">Priority</th>
</xsl:if>
<xsl:if test="sitemap:url/sitemap:lastmod">
<th class="pa3 fw6 tr dark-gray" style="width:200px">Last Modified</th>
</xsl:if>
</tr>
</thead>
<tbody class="lh-copy bg-near-white">
<xsl:for-each select="sitemap:url">
<tr class="hover-bg-white">
<xsl:variable name="loc">
<xsl:value-of select="sitemap:loc"/>
</xsl:variable>
<xsl:variable name="pno">
<xsl:value-of select="position()"/>
</xsl:variable>
<td class="pa3 tc b bb b--silver">
<xsl:value-of select="$pno"/>
</td>
<td class="pa3 bb b--silver">
<p>
<a href="{$loc}" class="link blue">
<xsl:value-of select="sitemap:loc"/>
</a>
</p>
<xsl:apply-templates select="xhtml:*"/>
<xsl:apply-templates select="image:*"/>
<xsl:apply-templates select="video:*"/>
</td>
<xsl:apply-templates select="sitemap:changefreq"/>
<xsl:apply-templates select="sitemap:priority"/>
<xsl:if test="sitemap:lastmod">
<td class="pa3 tr bb b--silver">
<xsl:value-of select="concat(substring(sitemap:lastmod, 0, 11), concat(' ', substring(sitemap:lastmod, 12, 5)), concat(' ', substring(sitemap:lastmod, 20, 6)))"/>
</td>
</xsl:if>
</tr>
</xsl:for-each>
</tbody>
</table>
</div>
</div>
</xsl:template>

<xsl:template match="sitemap:loc|sitemap:lastmod|image:loc|image:caption|video:*">
</xsl:template>

<xsl:template match="sitemap:changefreq|sitemap:priority">
<td class="pa3 tr bb b--silver">
<xsl:apply-templates/>
</td>
</xsl:template>

<xsl:template match="xhtml:link">
<xsl:variable name="altloc">
<xsl:value-of select="@href"/>
</xsl:variable>
<p>
<strong>Xhtml: </strong>
<a href="{$altloc}" class="mr2 link blue">
<xsl:value-of select="@href"/>
</a>

<xsl:if test="@hreflang">
<small class="dib mr2 ph1 pv1 tracked lh-solid white bg-silver br-pill">
<xsl:value-of select="@hreflang"/>
</small>
</xsl:if>

<xsl:if test="@rel">
<small class="dib mr2 ph2 pv1 tracked lh-solid white bg-silver br-pill">
<xsl:value-of select="@rel"/>
</small>
</xsl:if>

<xsl:if test="@media">
<small class="dib mr2 ph2 pv1 tracked lh-solid white bg-silver br-pill">
<xsl:value-of select="@media"/>
</small>
</xsl:if>
</p>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
55 changes: 54 additions & 1 deletion examples/sitemap/advanced/sitemap.config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const canonicalURL = 'https://example.com';
const canonicalURL = 'http://localhost:3000';

/** @type {import('astro-sitemap').SitemapOptions} */
const sitemapConfig = {
Expand All @@ -9,17 +9,70 @@ const sitemapConfig = {
createLinkInHead: true,

serialize(item) {
if (/virtual-one/.test(item.url)) {
return undefined;
}
if (/special-page/.test(item.url)) {
item.changefreq = 'daily';
item.lastmod = new Date();
item.priority = 0.9;

item.img = [
{
url: 'http://test.com/img1.jpg',
caption: 'An image',
title: 'The Title of Image One',
geoLocation: 'London, United Kingdom',
license: 'https://creativecommons.org/licenses/by/4.0/',
},
{
url: 'http://test.com/img2.jpg',
caption: 'Another image',
title: 'The Title of Image Two',
geoLocation: 'London, United Kingdom',
license: 'https://creativecommons.org/licenses/by/4.0/',
},
];
item.video = [
{
thumbnail_loc: 'http://test.com/tmbn1.jpg',
title: 'A video title',
description: 'This is a video',
},
{
thumbnail_loc: 'http://test.com/tmbn2.jpg',
title: 'A video with an attribute',
description: 'This is another video',
player_loc: 'http://www.example.com/videoplayer.mp4?video=123',
'player_loc:autoplay': 'ap=1',
'player_loc:allow_embed': 'yes',
},
];
}
return item;
},

// The integration creates a separate `sitemap-${i}.xml` file for each batch of 2, then adds this file to index - `sitemap-index.xml`.
entryLimit: 2, // default - 45000

// print date not time
lastmodDateOnly: true,

// style to transform to another format, ignored by search engines
xslUrl: `${canonicalURL}/sitemap.xsl`,

// set the xml namespace
xmlns: {
xhtml: true,
news: true,
image: true,
video: true,
custom: [
'xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"',
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"',
],
},

// sitemap specific
changefreq: 'yearly',
lastmod: new Date('2019-12-31'),
Expand Down
9 changes: 6 additions & 3 deletions examples/sitemap/advanced/src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
</head>
<body>
<h1>Astro</h1>
<a href="/second-page">Second page</a>
<a href="/special-page">Special page</a>
<a href="/exclude-this">Exclude this page</a>
<ul>
<li><a href="/second-page">Second page</a></li>
<li><a href="/special-page">Special page</a></li>
<li><a href="/exclude-this">Exclude this page</a></li>
<li><a href="/sitemap-index.xml">sitemap-index.xml</a></li>
</ul>
</body>
</html>
2 changes: 1 addition & 1 deletion examples/sitemap/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"preview": "astro preview"
},
"devDependencies": {
"astro": "^1.0.0-beta.47",
"astro": "^1.0.0-beta.50",
"astro-sitemap": "latest"
}
}
2 changes: 1 addition & 1 deletion examples/sitemap/i18n/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"preview": "astro preview"
},
"devDependencies": {
"astro": "^1.0.0-beta.47",
"astro": "^1.0.0-beta.50",
"astro-sitemap": "latest"
}
}
2 changes: 1 addition & 1 deletion examples/webmanifest/advanced/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"preview": "astro preview"
},
"devDependencies": {
"astro": "^1.0.0-beta.47",
"astro": "^1.0.0-beta.50",
"astro-webmanifest": "latest"
}
}
2 changes: 1 addition & 1 deletion examples/webmanifest/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"preview": "astro preview"
},
"devDependencies": {
"astro": "^1.0.0-beta.47",
"astro": "^1.0.0-beta.50",
"astro-webmanifest": "latest"
}
}
Loading

0 comments on commit 2258aa9

Please sign in to comment.