@angular-helpers/worker-http 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +55 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -406,6 +406,61 @@ manual control.
|
|
|
406
406
|
|
|
407
407
|
---
|
|
408
408
|
|
|
409
|
+
## SSR + hydration
|
|
410
|
+
|
|
411
|
+
Worker HTTP integrates transparently with Angular SSR. The two problems SSR
|
|
412
|
+
creates for worker-based HTTP are handled out of the box:
|
|
413
|
+
|
|
414
|
+
**1. Workers do not exist on the server.**
|
|
415
|
+
During SSR, `typeof Worker === 'undefined'`. `WorkerHttpBackend` detects this
|
|
416
|
+
and falls back to `FetchBackend` automatically (controlled by
|
|
417
|
+
`withWorkerFallback()`). The request is fulfilled on the server thread
|
|
418
|
+
exactly like a plain `HttpClient.get()` would do.
|
|
419
|
+
|
|
420
|
+
**2. Avoiding a re-fetch after hydration.**
|
|
421
|
+
Add `provideClientHydration()` from `@angular/platform-browser` at the app
|
|
422
|
+
root (standard Angular SSR setup). That enables Angular's HTTP transfer
|
|
423
|
+
cache by default. The transfer cache interceptor sits in the `HttpClient`
|
|
424
|
+
pipeline — **before** `WorkerHttpBackend`:
|
|
425
|
+
|
|
426
|
+
- On the server: `WorkerHttpBackend` falls back to fetch → returns a
|
|
427
|
+
response → the interceptor captures it into `TransferState`.
|
|
428
|
+
- On the browser: a matching request hits the interceptor first → it replays
|
|
429
|
+
the cached response synchronously, **without ever reaching
|
|
430
|
+
`WorkerHttpBackend`**. No worker is booted for hydrated requests.
|
|
431
|
+
|
|
432
|
+
```ts
|
|
433
|
+
// app.config.server.ts (or your SSR bootstrap)
|
|
434
|
+
export const serverConfig: ApplicationConfig = {
|
|
435
|
+
providers: [
|
|
436
|
+
provideServerRendering(),
|
|
437
|
+
provideWorkerHttpClient(
|
|
438
|
+
withWorkerConfigs([
|
|
439
|
+
{ id: 'api', workerUrl: new URL('./workers/api.worker', import.meta.url) },
|
|
440
|
+
]),
|
|
441
|
+
),
|
|
442
|
+
],
|
|
443
|
+
};
|
|
444
|
+
|
|
445
|
+
// app.config.ts (browser bootstrap)
|
|
446
|
+
export const appConfig: ApplicationConfig = {
|
|
447
|
+
providers: [
|
|
448
|
+
provideClientHydration(), // ← enables HTTP transfer cache automatically
|
|
449
|
+
provideWorkerHttpClient(
|
|
450
|
+
withWorkerConfigs([
|
|
451
|
+
{ id: 'api', workerUrl: new URL('./workers/api.worker', import.meta.url) },
|
|
452
|
+
]),
|
|
453
|
+
),
|
|
454
|
+
],
|
|
455
|
+
};
|
|
456
|
+
```
|
|
457
|
+
|
|
458
|
+
To customise which headers are captured or to cache `POST` requests, pass
|
|
459
|
+
`withHttpTransferCacheOptions(...)` to `provideClientHydration()` — both are
|
|
460
|
+
re-exported from `@angular/platform-browser`.
|
|
461
|
+
|
|
462
|
+
---
|
|
463
|
+
|
|
409
464
|
## Design principles
|
|
410
465
|
|
|
411
466
|
- **Zero main-thread cost** — `fetch()` runs entirely in the worker; the main thread only handles the `postMessage` handoff
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@angular-helpers/worker-http",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Angular HTTP over Web Workers — off-main-thread HTTP pipelines with configurable interceptors, WebCrypto security, and pluggable serialization",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"angular",
|