Skip to content
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

Add new configuration property "sitemapPath" to change the sitemap path #74

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/cli/src/ci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ async function run() {

cli.option('--budget <budget>', 'Budget (1-100), the minimum score which can pass.')
cli.option('--build-static <build-static>', 'Build a static website for the reports which can be uploaded.')
cli.option('--sitemap-path <sitemap-path>', 'Set a custom path for the sitemap.')

const { options } = cli.parse() as unknown as { options: CiOptions }

Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/createCli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default function createCli() {
cli.option('--mobile', 'Simulate device as mobile.')

cli.option('--site <site>', 'Host URL to scan')
cli.option('--sitemap-path <sitemap-path>', 'Sitemap path to use for scanning.')
cli.option('--samples <samples>', 'Specify the amount of samples to run.')
cli.option('--throttle', 'Enable the throttling')
cli.option('--enable-javascript', 'When inspecting the HTML wait for the javascript to execute. Useful for SPAs.')
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface CliOptions {
disableI18nPages?: boolean
enableJavascript?: boolean
disableJavascript?: boolean
sitemapPath?: string
}

export interface CiOptions extends CliOptions {
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ export function pickOptions(options: CiOptions | CliOptions): UserConfig {
if (options.enableJavascript)
picked.scanner.skipJavascript = false

if (options.sitemapPath)
picked.scanner.sitemapPath = options.sitemapPath

else if (options.disableJavascript)
picked.scanner.skipJavascript = true

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/discovery/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const resolveReportableRoutes: () => Promise<NormalisedRoute[]> = async (

// if sitemap scanning is enabled
if (resolvedConfig.scanner.sitemap) {
const sitemapUrls = await extractSitemapRoutes(resolvedConfig.site)
const sitemapUrls = await extractSitemapRoutes(resolvedConfig.site, resolvedConfig.scanner.sitemapPath)
if (sitemapUrls.length) {
logger.info(`Discovered ${sitemapUrls.length} routes from sitemap.xml.`)
sitemapUrls.forEach(url => urls.add(url))
Expand Down
10 changes: 8 additions & 2 deletions packages/core/src/discovery/sitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useLogger } from '../logger'
*
* @param site
*/
export const extractSitemapRoutes = async (site: string) => {
export const extractSitemapRoutes = async (site: string, sitemapPath?: string) => {
// make sure we're working from the host name
site = new $URL(site).origin
const unlighthouse = useUnlighthouse()
Expand All @@ -18,7 +18,13 @@ export const extractSitemapRoutes = async (site: string) => {
debug: unlighthouse.resolvedConfig.debug,
})

const sitemapUrl = `${site}/sitemap.xml`
let sitemapUrl = `${site}/sitemap.xml`
// if sitemapPath exists, check if start with "/" and remove it
if (sitemapPath) {
sitemapPath = sitemapPath.startsWith('/') ? sitemapPath.slice(1) : sitemapPath
sitemapUrl = `${site}/${sitemapPath}`
}

logger.debug(`Attempting to fetch sitemap at ${sitemapUrl}`)
const { sites } = await sitemap.fetch(sitemapUrl)
logger.debug(`Fetched sitemap with ${sites.length} URLs.`)
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,12 @@ export interface ResolvedUserConfig {
* @default true
*/
sitemap: boolean
/**
* Path where get the sitemap.
*
* @default undefined
*/
sitemapPath?: string
/**
* Alias to switch the device used for scanning.
* Set to false if you want to manually configure it.
Expand Down
20 changes: 20 additions & 0 deletions test/fixtures/unitedpets.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export default {
site: 'https://unitedpets.com',
outputPath: 'unitedpets-uci',
routerPrefix: '/skin-unlighthouse',
ci: {
// budget: {
// performance: 50,
// accessibility: 100,
// 'best-practices': 90,
// seo: 90,
// },
buildStatic: true
},
scanner: {
sitemapPath: 'sitemap/index.xml',
maxRoutes: 50,
device: 'desktop'
},
debug: true
}