Skip to content

Commit

Permalink
improve early hint test (#1574)
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Mar 28, 2024
1 parent b1d6b48 commit 9ab6a14
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions examples/render-modes/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,20 @@ async function startServer() {
} else {
const { statusCode, headers, earlyHints } = httpResponse

// Assert no JavaScript early hint for HTML-only
earlyHints.forEach((h) => {
if (h.assetType === 'script' && pageContext.urlPathname === '/html-only') {
throw new Error(
`Unexpected early hint for the ${pageContext.urlPathname} page: ${JSON.stringify(h, null, 2)}`
)
}
})
// No JavaScript early hint <=> HTML-only without +client.js
{
const hasJavaScriptEarlyHint = earlyHints.some((h) => h.assetType === 'script')
const htmlOnlyPage = '/html-only'
const { urlPathname } = pageContext
assert(
hasJavaScriptEarlyHint === (urlPathname !== htmlOnlyPage),
`Unexpected early hints for the page ${urlPathname}`
)
assert(
[htmlOnlyPage, '/', '/html-js', '/spa', '/ssr'].includes(urlPathname),
'Assertion at server/index.js needs to be updated'
)
}

if (res.writeEarlyHints) res.writeEarlyHints({ link: earlyHints.map((e) => e.earlyHintLink) })
headers.forEach(([name, value]) => res.setHeader(name, value))
Expand All @@ -62,3 +68,8 @@ async function startServer() {
app.listen(port)
console.log(`Server running at http://localhost:${port}`)
}

function assert(condition, msg) {
if (condition) return
throw new Error(msg)
}

0 comments on commit 9ab6a14

Please sign in to comment.