Skip to content

Commit

Permalink
refactor: extract module to build the sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
tlaundal committed Feb 5, 2023
1 parent 31f8db4 commit f31500f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
32 changes: 3 additions & 29 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {writeFileSync} from 'node:fs';
import {join} from 'node:path';
import type {Adapter} from '@sveltejs/kit';
import {wrapAdapter} from './internal/wrap-adapter.js';
import {renderSitemap} from './internal/render-sitemap.js';
import {buildSitemap} from './internal/build-sitemap.js';

/**
* Details about a page in the sitemap.
Expand Down Expand Up @@ -118,24 +116,6 @@ const defaultOptions: Options = {

const nameTemplate = '%s + sitemap';

function buildPageEntries(
paths: IterableIterator<string>,
options: Options,
) {
const pages: Record<string, Partial<PageDetails>> = {};
for (const path of paths) {
if (!(path in pages)) {
pages[path] = {};
}
}

return Object.entries(pages).map(([path, page]) => ({
loc: options.origin + path,
...options.defaults,
...page,
}));
}

/**
* This function is the entry point for sveltekit-static-sitemap. It takes a
* SvelteKit adapter and returns a wrapped version which will retrieve a
Expand Down Expand Up @@ -189,15 +169,9 @@ export function sitemapWrapAdapter(adapter: Adapter, options?: Partial<Options>)
resolvedOptions.origin = resolvedOptions.origin ?? this.config.kit.prerender.origin;
},
writePrerendered(original, dest) {
const pages = buildPageEntries(this.prerendered.pages.keys(), resolvedOptions);
const sitemap = renderSitemap(pages);

const target = join(dest, resolvedOptions.sitemapFile);
writeFileSync(target, sitemap);

// We call the original here in the end, in case the prerendered
// We call the original last, in case the prerendered
// bundle contains a sitemap which should take priority
return [...original(dest), resolvedOptions.sitemapFile];
return [...buildSitemap(this, resolvedOptions, dest), ...original(dest)];
},
});
}
33 changes: 33 additions & 0 deletions src/internal/build-sitemap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {writeFileSync} from 'node:fs';
import {join} from 'node:path';
import type {Builder} from '@sveltejs/kit';
import type {Options, PageDetails} from '..';
import {renderSitemap} from './render-sitemap.js';

function buildPageEntries(
paths: IterableIterator<string>,
options: Options,
) {
const pages: Record<string, Partial<PageDetails>> = {};
for (const path of paths) {
if (!(path in pages)) {
pages[path] = {};
}
}

return Object.entries(pages).map(([path, page]) => ({
loc: options.origin + path,
...options.defaults,
...page,
}));
}

export function buildSitemap(builder: Builder, options: Options, dest: string) {
const pages = buildPageEntries(builder.prerendered.pages.keys(), options);
const sitemap = renderSitemap(pages);

const target = join(dest, options.sitemapFile);
writeFileSync(target, sitemap);

return [options.sitemapFile];
}

0 comments on commit f31500f

Please sign in to comment.