Skip to content

Commit

Permalink
fix: support running on subpaths
Browse files Browse the repository at this point in the history
Fixes #196
  • Loading branch information
harlan-zw committed Mar 7, 2024
1 parent 92612ed commit 6d01af9
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export async function validateHost(resolvedConfig: ResolvedUserConfig) {
// change the URL to the redirect one, make sure it's not to a file (i.e /index.php)
if (redirected && redirectUrl && !redirectUrl.includes('.')) {
logger.success(`Request to site \`${site}\` redirected to \`${redirectUrl}\`, using that as the site.`)
resolvedConfig.site = normaliseHost(redirectUrl)
resolvedConfig.site = normaliseHost(redirectUrl).toString()
}
else {
logger.success(`Successfully connected to \`${site}\`. (Status: \`${response.status}\`).`)
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/puppeteer/tasks/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from 'fs-extra'
import type { CheerioAPI } from 'cheerio'
import cheerio from 'cheerio'
import type { Page } from 'puppeteer-core'
import { $URL, withoutTrailingSlash } from 'ufo'
import { withoutTrailingSlash } from 'ufo'
import chalk from 'chalk'
import type { HTMLExtractPayload, PuppeteerTask } from '../../types'
import { useUnlighthouse } from '../../unlighthouse'
Expand Down Expand Up @@ -122,7 +122,7 @@ export const inspectHtmlTask: PuppeteerTask = async (props) => {
if (response.redirected) {
// strip any protocols from the url
const siteHost = runtimeSettings.siteUrl.host.split(':')[0]
const redirectHost = new $URL(response.redirected).host.split(':')[0]
const redirectHost = new URL(response.redirected).host.split(':')[0]
// allow subdomains
if (siteHost !== redirectHost && !redirectHost.endsWith(`.${siteHost}`)) {
routeReport.tasks.inspectHtmlTask = 'ignore'
Expand Down
15 changes: 13 additions & 2 deletions packages/core/src/resolveConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,17 @@ export const resolveUserConfig: (userConfig: UserConfig) => Promise<ResolvedUser
// it's possible we don't know the site at runtime
if (config.site) {
// normalise site
config.site = normaliseHost(config.site)
const siteUrl = normaliseHost(config.site)
if (siteUrl.pathname !== '/' && siteUrl.pathname !== '') {
logger.warn('You are providing a site with a path, disabling sitemap, robots and dynamic sampling.')
config.scanner.sitemap = false
config.scanner.robotsTxt = false
config.scanner.dynamicSampling = false
config.site = siteUrl.toString()
}
else {
config.site = siteUrl.origin
}
}
if (config.lighthouseOptions) {
if (config.lighthouseOptions.onlyCategories?.length) {
Expand Down Expand Up @@ -76,7 +86,8 @@ export const resolveUserConfig: (userConfig: UserConfig) => Promise<ResolvedUser
uploadThroughputKbps: 750,
cpuSlowdownMultiplier: 1,
}
} else if (!config.site || config.site.includes('localhost') || config.scanner?.throttle === false) {
}
else if (!config.site || config.site.includes('localhost') || config.scanner?.throttle === false) {
config.lighthouseOptions.throttlingMethod = 'provided'
config.lighthouseOptions.throttling = {
rttMs: 0,
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/router/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { basename } from 'node:path'
import { $URL, hasProtocol, isRelative, withBase, withLeadingSlash } from 'ufo'
import { hasProtocol, isRelative, withBase, withLeadingSlash } from 'ufo'
import type { NormalisedRoute } from '../types'
import { hashPathName, trimSlashes } from '../util'
import { useUnlighthouse } from '../unlighthouse'
Expand All @@ -10,7 +10,7 @@ export function isScanOrigin(url: string): boolean {

const { runtimeSettings } = useUnlighthouse()

const $url = new $URL(url)
const $url = new URL(url)
if ($url.hostname === runtimeSettings.siteUrl.hostname)
return true
// allow subdomains
Expand All @@ -32,7 +32,7 @@ export function normaliseRoute(url: string): NormalisedRoute {
url = withBase(url, runtimeSettings.siteUrl.origin)
}

const $url = new $URL(url)
const $url = new URL(url)
// make sure we start with a leading slash
const path = withLeadingSlash($url.pathname)

Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type http from 'node:http'
import type https from 'node:https'
import type { $URL, QueryObject } from 'ufo'
import type { QueryObject } from 'ufo'
import type LH from 'lighthouse/types/lh'
import type { LaunchOptions, Page, PuppeteerNodeLaunchOptions } from 'puppeteer-core'
import type { Hookable, NestedHooks } from 'hookable'
Expand Down Expand Up @@ -38,7 +38,7 @@ export interface NormalisedRoute {
id: string
path: string
url: string
$url: $URL
$url: URL
definition: RouteDefinition
/**
* A runtime path that the route was discovered from, useful if the route is a 404 and we want to know what directed
Expand Down Expand Up @@ -517,7 +517,7 @@ export interface RuntimeSettings {
/**
* A URL instance of the site for easier use of the host.
*/
siteUrl: $URL
siteUrl: URL
/**
* The URL of the server running the API and client.
*/
Expand Down
13 changes: 7 additions & 6 deletions packages/core/src/unlighthouse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ export async function createUnlighthouse(userConfig: UserConfig, provider?: Prov

ctx.setSiteUrl = async (url: string) => {
const site = normaliseHost(url)
ctx.runtimeSettings.siteUrl = new $URL(site)
ctx.runtimeSettings.siteUrl = site

logger.debug(`Setting Unlighthouse Site URL [Site: ${site}]`)
logger.debug(`Setting Unlighthouse Site URL [Site: ${site.toString()}]`)

const outputPath = join(
resolvedConfig.outputPath,
Expand All @@ -224,15 +224,16 @@ export async function createUnlighthouse(userConfig: UserConfig, provider?: Prov
runtimeSettings.configCacheKey || '',
)

ctx.resolvedConfig.site = site
if (!ctx.resolvedConfig.site)
ctx.resolvedConfig.site = site.toString()
ctx.runtimeSettings.outputPath = outputPath
ctx.runtimeSettings.generatedClientPath = outputPath

await hooks.callHook('site-changed', site)
await hooks.callHook('site-changed', ctx.resolvedConfig.site)
}

ctx.setServerContext = async ({ url, server, app }) => {
const $server = new $URL(url)
const $server = new URL(url)

logger.debug(`Setting Unlighthouse Server Context [Server: ${$server}]`)

Expand Down Expand Up @@ -298,7 +299,7 @@ export async function createUnlighthouse(userConfig: UserConfig, provider?: Prov

ctx.routes = await resolveReportableRoutes()
logger.debug('Resolved reportable routes', ctx.routes.length)
await createBroadcastingEvents()
createBroadcastingEvents()
worker.queueRoutes(ctx.routes)

if (provider?.name !== 'ci') {
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ export function normaliseHost(host: string) {
host = `http${host.startsWith('localhost') ? '' : 's'}:https://${host}`
host = host.includes('.') ? host : withTrailingSlash(host)
// strip pathname from host
const url = new URL(host)
return `${url.protocol}//${url.host}/`
return new URL(host)
}

/**
Expand Down

0 comments on commit 6d01af9

Please sign in to comment.