Skip to content

Commit

Permalink
Merge pull request #148 from thegalactiks/improve-sitemap
Browse files Browse the repository at this point in the history
feat: improve sitemap generation
  • Loading branch information
emmanuelgautier authored Feb 29, 2024
2 parents 8109a1e + 81f0dbf commit 94ca328
Show file tree
Hide file tree
Showing 17 changed files with 349 additions and 86 deletions.
7 changes: 5 additions & 2 deletions examples/astro/package-lock.json

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

1 change: 1 addition & 0 deletions examples/astro/src/assets/.gitkeep
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
name: Website name
description: Website description.
identifier: example.com
url: https://example.com/
identifier: galactiks.com
url: https://www.galactiks.com/
dateCreated: 1970-01-01
---
4 changes: 2 additions & 2 deletions packages/adapters/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@
"@astrojs/partytown": "~2.0",
"@astrojs/react": "~3.0",
"@astrojs/rss": "~4.0",
"@astrojs/sitemap": "~3.1",
"@galactiks/sitemap": "^0.1.0",
"astro": "~4.4",
"astro-robots-txt": "~1.0"
},
"devDependencies": {
"@astrojs/partytown": "2.0.4",
"@astrojs/react": "3.0.10",
"@astrojs/rss": "4.0.5",
"@astrojs/sitemap": "3.1.1",
"@galactiks/sitemap": "workspace:^",
"@types/debug": "4.1.12",
"@types/react": "18.2.58",
"@types/react-dom": "18.2.19",
Expand Down
8 changes: 8 additions & 0 deletions packages/adapters/astro/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ export default function createPlugin(
updateConfig,
addWatchFile,
}) => {
let trailingSlash = config.trailingSlash;
if (trailingSlash === 'ignore') {
trailingSlash = config.build.format === 'directory' ? 'always' : 'never';
}

setConfig('trailingSlash', config.trailingSlash);

assetsPath = join(fileURLToPath(config.srcDir), 'assets');
galactiksConfig = setConfig('content.assets', assetsPath);

Expand All @@ -61,6 +68,7 @@ export default function createPlugin(

updateConfig({
site: galactiksConfig.webManifest.start_url,
trailingSlash,
prefetch: true,
vite: {
resolve: {
Expand Down
6 changes: 3 additions & 3 deletions packages/adapters/astro/src/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import {

export async function getStaticPaths() {
return (await getPages({ inLanguages: getLanguages() }))
.filter((_p) => _p.path && _p.path !== '/')
.map((page) => ({
params: { path: page.path.slice(1) },
params: { path: page.path.endsWith('/') ? page.path.slice(0, -1) : page.path },
props: { page },
}));
}))
.filter(page => page.params.path && page.params.path !== '/');
}

export function getIndexPage() {
Expand Down
112 changes: 112 additions & 0 deletions packages/adapters/astro/src/plugins/sitemap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import type { AstroConfig, AstroIntegration } from 'astro';
import { getDefaultLanguage } from '@galactiks/config';
import { type Content, getPageByURL } from '@galactiks/explorer';
import { generateSitemaps } from '@galactiks/sitemap';

const sitemapIndexOutput = 'sitemap-index.xml';

const createPlugin = (): AstroIntegration => {
let config: AstroConfig;

return {
name: '@galactiks/astro-integration/sitemap',

hooks: {
'astro:config:done': async ({ config: cfg }) => {
config = cfg;
},

'astro:build:done': async ({ dir, routes, pages, logger }) => {
if (!config.site) {
logger.warn(
'The Sitemap integration requires the `site` astro.config option. Skipping.'
);
return;
}

let finalSiteUrl: URL;
if (config.site) {
finalSiteUrl = new URL(config.base, config.site);
} else {
console.warn(
'The Sitemap integration requires the `site` astro.config option. Skipping.'
);
return;
}

let pageUrls = pages
.map((p) => {
if (p.pathname !== '' && !finalSiteUrl.pathname.endsWith('/'))
finalSiteUrl.pathname += '/';
if (p.pathname.startsWith('/')) p.pathname = p.pathname.slice(1);
const fullPath = finalSiteUrl.pathname + p.pathname;
return new URL(fullPath, finalSiteUrl).href;
});

const routeUrls = routes.reduce<string[]>((urls, r) => {
if (r.type !== 'page') return urls;

/**
* Dynamic URLs have entries with `undefined` pathnames
*/
if (r.pathname) {
// `finalSiteUrl` may end with a trailing slash
// or not because of base paths.
let fullPath = finalSiteUrl.pathname;
if (fullPath.endsWith('/')) fullPath += r.generate(r.pathname).substring(1);
else fullPath += r.generate(r.pathname);

const newUrl = new URL(fullPath, finalSiteUrl).href;

if (config.trailingSlash === 'never') {
urls.push(newUrl);
} else if (config.build.format === 'directory' && !newUrl.endsWith('/')) {
urls.push(newUrl + '/');
} else {
urls.push(newUrl);
}
}

return urls;
}, []);

pageUrls = Array.from(new Set([...pageUrls, ...routeUrls]));
logger.info(`Generating sitemap for ${pageUrls.length} pages`);
logger.info(pageUrls.join('\n'));

const contentPages = (
await Promise.all(
pageUrls.map(getPageByURL)
)
).filter((page) => page !== undefined) as Content[];
if (contentPages.length === 0) {
logger.warn(`No pages found!\n\`${sitemapIndexOutput}\` not created.`);
return;
}

const destDir = fileURLToPath(dir);
await generateSitemaps({
destinationDir: destDir,
hostname: finalSiteUrl.href,
pages: contentPages,
defaultLanguage: getDefaultLanguage(),
publication: {
name: 'Galactiks',
},
});
// await simpleSitemapAndIndex({
// hostname: finalSiteUrl.href,
// destinationDir: destDir,
// sourceData: urlData,
// limit: entryLimit,
// gzip: false,
// });
logger.info(`\`${sitemapIndexOutput}\` created at \`${path.relative(process.cwd(), destDir)}\``);
},
},
};
};

export default createPlugin;
18 changes: 2 additions & 16 deletions packages/adapters/astro/src/preset.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
import type { AstroIntegration } from 'astro';
import partytown from '@astrojs/partytown';
import react from '@astrojs/react';
import sitemap from '@astrojs/sitemap';
import { getDefaultLanguage, getLanguages } from '@galactiks/config';
import robotsTxt from 'astro-robots-txt';

import { sitemapSerialize } from './sitemap.js';
import sitemap from './plugins/sitemap.js';

export const integrationsPreset = (): AstroIntegration[] => {
const defaultLanguage = getDefaultLanguage();

return [
react(),
partytown(),
sitemap({
i18n: defaultLanguage
? {
defaultLocale: defaultLanguage,
locales: Object.fromEntries(
getLanguages().map((lang) => [lang, lang])
),
}
: undefined,
serialize: sitemapSerialize(),
}),
sitemap(),
robotsTxt(),
];
};
43 changes: 0 additions & 43 deletions packages/adapters/astro/src/sitemap.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/config/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const galactiksConfigFileSchema = z.object({
locales: localesSchema.optional(),
template: z.string(),
analytics: analyticsConfigSchema.optional(),
trailingSlash: z.enum(['always', 'never']).optional(),
trailingSlash: z.enum(['ignore', 'always', 'never']).optional(),
pages: z
.object({
articles: pagesObjectItemSchema,
Expand Down
2 changes: 2 additions & 0 deletions packages/explorer/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from './content/index.js';

export { Content } from './types/index.js';
28 changes: 28 additions & 0 deletions packages/sitemap/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# @galactiks/config

This package allows reading [Galactiks](https://www.galactiks.com) configurations files and get config during website generation.

## Installation

Install the `@galactiks/config` package using your preferred package manager:

```sh
# npm
npm i @galactiks/config

# yarn
yarn add @galactiks/config

# pnpm
pnpm i @galactiks/config
```

If you run into any issues, [feel free to report them to us on GitHub](https://github.com/thegalactiks/explorer/issues).

## Getting started

Coming soon

## License

MIT © [Galactiks](https://www.galactiks.com)
43 changes: 43 additions & 0 deletions packages/sitemap/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "@galactiks/sitemap",
"version": "0.0.1",
"description": "A simple sitemap generator for Galactiks",
"author": "thegalactiks",
"type": "module",
"types": "./dist/index.d.ts",
"license": "MIT",
"engines": {
"node": ">=18.0.0"
},
"bugs": "https://github.com/thegalactiks/explorer/issues",
"homepage": "https://www.galactiks.com",
"repository": {
"type": "git",
"url": "https://github.com/thegalactiks/explorer.git",
"directory": "packages/sitemap"
},
"main": "dist/index.js",
"scripts": {
"build": "tsc -p ./tsconfig.json",
"build:ci": "tsc -p ./tsconfig.json"
},
"exports": {
"./package.json": "./package.json",
".": "./dist/index.js"
},
"files": [
"dist"
],
"keywords": [
"galactiks",
"sitemap",
"seo"
],
"dependencies": {
"date-fns": "3.3.1",
"sitemap": "7.1.1"
},
"devDependencies": {
"@galactiks/explorer": "workspace:^"
}
}
Loading

0 comments on commit 94ca328

Please sign in to comment.