Skip to content

perf: start the landing feed request from index.html (4G first feed render 1299 → 997 ms) - #951

Open
matthewguerra-cog wants to merge 1 commit into
masterfrom
devin/task-14-faster-first-load
Open

matthewguerra-cog wants to merge 1 commit into
masterfrom
devin/task-14-faster-first-load

Conversation

@matthewguerra-cog

@matthewguerra-cog matthewguerra-cog commented Nov 16, 2026 •

Copy link
Copy Markdown

Summary

First feed render on a cold load waited on a waterfall: HTML → main (79 kB gzip) → bootstrap → router → FeedComponent → feed request. An inline script in src/index.html now starts the landing feed request as soon as the HTML is parsed, and HackerNewsAPIService.fetchFeed takes that response over (once, matched by URL) instead of fetching again.

index.html (head):  /, /news|newest|show|ask|jobs/:page  →  window.__hnFeedPrefetch = { url, data: fetch(url).then(r => r.json()) }
fetchFeed(type, page): takePrefetchedFeed(url) || lazyFetch(url)   // single use; if the early request failed, it falls back to lazyFetch

Behaviour is unchanged: same endpoint and URL, still one feed request per load, and the item/user routes and later navigations go through lazyFetch as before. Angular is still 9.0.1 and there are no new dependencies. Promise.resolve(prefetch.data) brings the native promise into the Angular zone, so change detection runs.

Before / after

Production build (ng build --prod, Node 12), served with gzip and the SPA fallback like firebase.json. Each run used a fresh browser context with a cold cache. Numbers are the median of 5 runs under Lighthouse mobile 4G (150 ms RTT, 1.6 Mbps down / 750 kbps up), and first feed render is the moment the first li.post a.title appears.

Metric master this PR
First feed render, 4G (SW blocked) 1299 ms 997 ms (−23%)
First feed render, 4G (SW allowed) 1323 ms 1000 ms
First feed render, unthrottled 368 ms 297 ms
Feed request start, 4G 943 ms (after main) 166 ms (with HTML)
main-es2015 raw / gzip 278.2 / 79.4 kB 278.5 / 79.5 kB

Render is now limited by main download and bootstrap (main finishes at about 930 ms), not by the API.

Baseline bundles, raw / gzip in kB: main-es2015 278.2 / 79.4, main-es5 330.7 / 85.5, polyfills-es2015 36.6 / 12.3, polyfills-es5 129.7 / 44.0, runtime 2.4 / 1.2, styles 10.7 / 1.3, item-details (lazy) 10.8 / 3.2, user (lazy) 3.9 / 1.6.

Looked at, not changed

  • main contents (source-map-explorer): @angular/core 112 kB, router 69 kB, common 13 kB, platform-browser 12 kB, service-worker 5 kB. App code and RxJS are small. rxjs-compat adds 0 bytes to main, because the deep imports resolve to the same RxJS 6 modules, so Drop rxjs-compat by importing from rxjs directly #946 is cleanup only and not a size win. Nothing unused in main is large enough to matter without changing behaviour.
  • Fonts: system fonts only, no web fonts. Analytics: analytics.js already loads async, and dropping it would change behaviour.
  • Service worker: it registers when stable, after first render, so the /*.js prefetch group (which includes the es5 bundles) does not compete with first load. SW-allowed numbers are in the table.

Overlap

How to reproduce

nvm use 12 && npx ng build --prod. Serve dist/angular-hnpwa with gzip and the SPA fallback, then run a Playwright script that opens / in a fresh context with 4G throttling and records the time the first li.post a.title appears. The bench scripts are in the Latency Optimizer's memory under files/angular2-hn-latency/.

Devin-Org: engineering


Latency Optimizer opened this PR for Matthew
Implemented in session Speed up angular2-hn first load
Review with Devin · Open in Devin Desktop


…trap

Co-Authored-By: Matthew Guerra <matthew.guerra@cognition.ai>
@devin-ai-integration

Copy link
Copy Markdown

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

Custom reviewers

  • 🧪 Test 2 reviewer: no findings · View
  • 🚩 TD Enterprise Architecture: no findings · View
  • 👁️ TD Information Security: no findings · View
  • 🧠 TD French Language (Bill 96): no findings · View
  • ⏱️ TD API Contracts: no findings · View

Devin Review

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 Missing required contributor credit

The PR omits the contributor-list update required by the contribution checklist. Neither credited author appears in the contributors list.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

@devin-ai-integration

Copy link
Copy Markdown

Verified the production build in Chromium against the real API.

  • Cold /: 30 stories from exactly one /news?page=1 request, which starts before main finishes loading.
  • Deep links /newest/1, /show/2, /ask/1, /jobs/1 each render with one matching request.
  • Header navigation, More, and browser back/forward work.
  • Item pages and comments work, and the lazy chunks load. A cold /item/:id makes no feed prefetch.
  • When the first prefetch is forced to fail, the app retries with a normal request (200) and renders the feed.
  • The service worker registers and activates, and a reload under its control renders 30 stories.
One early request Failed-prefetch recovery
Cold feed Retry recovery
Existing limitation: the HN API returns 404 for user profiles

/user/adriant93 and /user/GitRbit show "Could not load user" because node-hnapi returns 404 for them. A master build shows the same error, so a successful profile render is unverified rather than passed.

master this PR
master PR

Browser verification recording

Written by Devin

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant