Skip to content

Commit

Permalink
fix: pass a URL instance
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Mar 28, 2023
1 parent 97bc88f commit a3bc6c3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 42 deletions.
14 changes: 7 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ const fetch = (
return reflect
? resolve({ isRejected: true, error })
: resolve({
url,
html: '',
mode: 'fetch',
headers: error.response ? error.response.headers : {},
statusCode: error.response ? error.response.statusCode : undefined
})
url,
html: '',
mode: 'fetch',
headers: error.response ? error.response.headers : {},
statusCode: error.response ? error.response.statusCode : undefined
})
}
})

Expand Down Expand Up @@ -136,7 +136,7 @@ const isFetchMode = url =>
autoDomains.includes(parseUrl(url).domainWithoutSuffix)

const determinateMode = (url, { prerender }) => {
if (prerender === false || isMediaUrl(url)) return 'fetch'
if (prerender === false || isMediaUrl(url.toString())) return 'fetch'
if (prerender === true) return 'prerender'
return isFetchMode(url) ? 'fetch' : 'prerender'
}
Expand Down
74 changes: 39 additions & 35 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,50 @@ const wait = async (promise, prop) => {
return prop ? res[prop] : res
}

test('reachable URL', async t => {
const url = 'https://example.com'
const [prerenderDisabled, prerenderEnabled] = await Promise.all([
getHTML(url, { prerender: false, getBrowserless }),
getHTML(url, {
prerender: true,
getBrowserless,
puppeteerOpts: { adblock: false }
})
])

t.is(
await wait(
;[
{ title: 'from URL', url: 'https://example.com' },
{ title: 'from URL instance', url: new URL('https://example.com') }
].forEach(({ title, url }) => {
test(title, async t => {
const [prerenderDisabled, prerenderEnabled] = await Promise.all([
getHTML(url, { prerender: false, getBrowserless }),
'statusCode'
),
200
)
t.is(
await wait(
getHTML(url, {
prerender: true,
getBrowserless,
puppeteerOpts: { adblock: false }
}),
'statusCode'
),
200
)

t.is(prerenderDisabled.statusCode, prerenderEnabled.statusCode)
t.is(prerenderDisabled.statusCode, 200)

t.true(Object.keys(prerenderDisabled.headers).length > 0)
t.true(Object.keys(prerenderEnabled.headers).length > 0)
t.is(typeof prerenderDisabled.headers, typeof prerenderEnabled.headers)

t.true(prerenderDisabled.html.length > 0)
t.true(prerenderEnabled.html.length > 0)
t.is(typeof prerenderDisabled.html, typeof prerenderEnabled.html)
})
])

t.is(
await wait(
getHTML(url, { prerender: false, getBrowserless }),
'statusCode'
),
200
)
t.is(
await wait(
getHTML(url, {
prerender: true,
getBrowserless,
puppeteerOpts: { adblock: false }
}),
'statusCode'
),
200
)

t.is(prerenderDisabled.statusCode, prerenderEnabled.statusCode)
t.is(prerenderDisabled.statusCode, 200)

t.true(Object.keys(prerenderDisabled.headers).length > 0)
t.true(Object.keys(prerenderEnabled.headers).length > 0)
t.is(typeof prerenderDisabled.headers, typeof prerenderEnabled.headers)

t.true(prerenderDisabled.html.length > 0)
t.true(prerenderEnabled.html.length > 0)
t.is(typeof prerenderDisabled.html, typeof prerenderEnabled.html)
})
})

test('timeout URL', async t => {
Expand Down

0 comments on commit a3bc6c3

Please sign in to comment.