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

httpResponse.earlyHints includes unneeded JS in HTML-only mode #1574

Closed
jonaskuske opened this issue Mar 23, 2024 · 10 comments
Closed

httpResponse.earlyHints includes unneeded JS in HTML-only mode #1574

jonaskuske opened this issue Mar 23, 2024 · 10 comments

Comments

@jonaskuske
Copy link
Contributor

jonaskuske commented Mar 23, 2024

Description

I'm using HTML-only mode, which loads no JS besides the +client.ts files. Still, earlyHints instruct the browser to preload not only the +clients.ts script, but also the regular index.ts and corresponding chunks.

This leads to overfetching and logs warnings in the console:

image

@jonaskuske
Copy link
Contributor Author

jonaskuske commented Mar 23, 2024

Workaround:

let earlyHints = httpResponse.earlyHints

// https://github.com/vikejs/vike/issues/1574
if (RENDER_MODE === 'MPA') {
  const { assetsManifest } = await getGlobalContextAsync()

  const hintSources = new Set(httpResponse.earlyHints.map((hint) => hint.src))
  const pageEntry = Object.values(assetsManifest!).find(
    (e) => e.src?.includes('pageConfigValuesAll') && hintSources.has('/' + e.file),
  )
  const pageEntryImports = (pageEntry?.imports ?? []).map((key) => assetsManifest![key].file)
  const pageEntrySources = new Set(
    [pageEntry?.file, ...pageEntryImports].map((file) => '/' + file),
  )

  earlyHints = httpResponse.earlyHints.filter((hint) => !pageEntrySources.has(hint.src))
}

@brillout
Copy link
Member

Indeed, I can reproduce. How do you get the warnings in the bowser console? I don't get any.

@jonaskuske
Copy link
Contributor Author

jonaskuske commented Mar 23, 2024

Hmm, I don't know, they just show up after a couple of seconds for me in Edge, should work the same in Chrome though. Have you checked whether EH work at all? They only work in prod for me, as they (or rather the fastify plugin I use) require HTTP2 or HTTP3.
(in console: performance.getEntries().filter(e => e.initiatorType === 'early-hints'))

@brillout
Copy link
Member

Actually, the two early hints I can reproduce are CSS early hints which is correct.

I cannot reproduce JavaScript early hints, see b1d6b48.

Can you provide a reproduction?

@brillout
Copy link
Member

Ah, wait, actually I can reproduce for a production build. Let me fix this.

@jonaskuske
Copy link
Contributor Author

Looking at the assertion, is your plan to exclude all JS in HTML-only mode? +client.ts and its imports can stay, no?

@brillout
Copy link
Member

Yes, early hints for client.js should stay.

(FYI I get an empty array upon performance.getEntries().filter(e => e.initiatorType === 'early-hints'), I guess my Express.js or Chrome setup doesn't support early hints 🤔)

brillout added a commit that referenced this issue Mar 28, 2024
@brillout
Copy link
Member

Fix pre-released as 0.4.167-commit-14e4b5e.

Btw. in case your company is up for it Vike is looking for sponsors — it makes a massive difference.

@jonaskuske
Copy link
Contributor Author

Thank you! And would love to – but unfortunately, I only use Vike privately (for my thesis on app render modes/architecture https://thesis.joku.co) and don't have the means to sponsor :(

@brillout
Copy link
Member

No problems :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants