Skip to content

Commit

Permalink
feat: add sitemap generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Feb 22, 2024
1 parent b961e6c commit 3d13520
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 0 deletions.
13 changes: 13 additions & 0 deletions app/controllers/content_distribution_controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { inject } from '@adonisjs/core'
import { HttpContext } from '@adonisjs/core/http'

import { SitemapGenerator } from '#services/sitemap_generator'

export default class ContentDistributionController {
@inject()
async getSitemap({ response }: HttpContext, sitemapGenerator: SitemapGenerator) {
response.header('Content-Type', 'application/xml')

return await sitemapGenerator.generate()
}
}
27 changes: 27 additions & 0 deletions app/services/sitemap_generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import cache from '@adonisjs/cache/services/main'
import { SitemapStream, streamToPromise } from 'sitemap'

import type { PackageInfo } from '#types/main'

export class SitemapGenerator {
constructor(protected packages: PackageInfo[]) {}

async #generateSitemap() {
const sitemap = new SitemapStream({ hostname: 'https://packages.adonisjs.com' })

sitemap.write({ url: '/', changefreq: 'daily', priority: 1 })

this.packages.forEach((pkg) =>
sitemap.write({ url: `/packages/${pkg.slug}`, changefreq: 'weekly', priority: 0.7 }),
)

sitemap.end()

const buffer = await streamToPromise(sitemap)
return buffer.toString()
}

async generate() {
return cache.getOrSet('sitemap', () => this.#generateSitemap(), { ttl: '1d' })
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"sanitize-html": "^2.11.0",
"satori": "^0.10.11",
"satori-html": "^0.3.2",
"sitemap": "^7.1.1",
"sqlite3": "^5.1.7",
"vue": "^3.4.7"
},
Expand Down
32 changes: 32 additions & 0 deletions pnpm-lock.yaml

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

3 changes: 3 additions & 0 deletions providers/app_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { ApplicationService } from '@adonisjs/core/types'

import { PackageFetcher } from '#services/package_fetcher'
import { PackagesFetcher } from '#services/packages_fetcher'
import { SitemapGenerator } from '#services/sitemap_generator'
import { PackagesDataRefresher } from '#services/packages_data_refresher'

export default class AppProvider {
Expand All @@ -32,6 +33,8 @@ export default class AppProvider {
return new PackagesFetcher(await resolver.make(PackageFetcher), packagesFile)
})

this.app.container.bind(SitemapGenerator, async () => new SitemapGenerator(packagesFile))

/**
* Helper for removing double slashes from urls
*/
Expand Down
2 changes: 2 additions & 0 deletions start/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import router from '@adonisjs/core/services/router'

const PackagesController = () => import('#controllers/packages_controller')
const OgImagesController = () => import('#controllers/og_images_controller')
const ContentDistributionController = () => import('#controllers/content_distribution_controller')

router.get('/', [PackagesController, 'getHome'])
router.get('/packages/:name', [PackagesController, 'getPackage'])
router.get('/packages/:name/og.png', [OgImagesController]).as('og_image')
router.get('/sitemap.xml', [ContentDistributionController, 'getSitemap'])

router.get('/healthcheck', ({ response }) => response.noContent())

0 comments on commit 3d13520

Please sign in to comment.