Commit graph

213 commits

Author SHA1 Message Date
Alan Agius
b40d11eec4 build: consolidate domino bundling in platform-server
Move the domino bundling logic and related shims into a centralized third_party directory within packages/platform-server. This avoids duplication of the bundling logic and ensures consistent shimming across the platform-server package and its entry points.

Following a conversation with OSS licensing, this change also includes the domino LICENSE file in the generated npm package to comply with licensing requirements for bundled third-party code.

```
├── fesm2022
│   ├── init.mjs
│   ├── init.mjs.map
│   ├── platform-server.mjs
│   ├── platform-server.mjs.map
│   ├── _server-chunk.mjs
│   ├── _server-chunk.mjs.map
│   ├── testing.mjs
│   └── testing.mjs.map
├── LICENSE
├── package.json
├── README.md
├── third_party
│   └── domino
│       ├── bundled-domino.d.ts
│       ├── bundled-domino.mjs
│       ├── bundled-domino.mjs.map
│       └── LICENSE
└── types
    ├── init.d.ts
    ├── platform-server.d.ts
    └── testing.d.ts
```
2026-03-25 13:31:05 -07:00
arturovt
d3f67f6ca8 refactor(core): mark VERSION as @__PURE__ for better tree-shaking
Annotate the `new Version(...)` call with `/* @__PURE__ */` to signal to optimizers that the constructor is side-effect free.

Without this hint, bundlers such as Terser or ESBuild may conservatively retain the `VERSION` instantiation even when unused. With the annotation, the constant can be tree-shaken away in production builds if not referenced, reducing bundle size.
2025-11-10 12:04:04 -08:00
SkyZeroZx
5e4437baf8 refactor(platform-server): remove redundant providedIn: 'root' from injection tokens
Removes unnecessary `providedIn: 'root'` declarations from injection tokens
2025-11-04 00:31:52 +00:00
Alan Agius
062a696673 refactor(platform-server): use URL constructor for robust parsing (#64494)
The existing implementation of `PlatformLocation` uses a custom URL parsing mechanism that can be brittle and doesn't properly update the `href` property. This change refactors the URL parsing to use the native `URL` constructor, providing more robust and accurate parsing of URLs, which also correctly updates the `href` property.

The tests for `PlatformLocation` have also been moved to a dedicated file to improve organization and clarity.

PR Close #64494
2025-10-17 18:17:15 +00:00
Kristiyan Kostadinov
8f59295019 refactor(core): remove unnecessary deps arrays (#63823)
We don't need to use the `deps` array syntax anymore since we have the `inject` function. These changes clean up the relevant usages.

PR Close #63823
2025-09-16 16:51:52 +00:00
Alan Agius
28926ba92c feat(core): introduce BootstrapContext for improved server bootstrapping (#63562)
This commit introduces a number of changes to the server bootstrapping process to make it more robust and less error-prone, especially for concurrent requests.

Previously, the server rendering process relied on a module-level global platform injector. This could lead to issues in server-side rendering environments where multiple requests are processed concurrently, as they could inadvertently share or overwrite the global injector state.

The new approach introduces a `BootstrapContext` that is passed to the `bootstrapApplication` function. This context provides a platform reference that is scoped to the individual request, ensuring that each server-side render has an isolated platform injector. This prevents state leakage between concurrent requests and makes the overall process more reliable.

BREAKING CHANGE:
The server-side bootstrapping process has been changed to eliminate the reliance on a global platform injector.

Before:
```ts
const bootstrap = () => bootstrapApplication(AppComponent, config);
```

After:
```ts
const bootstrap = (context: BootstrapContext) =>
  bootstrapApplication(AppComponent, config, context);
```

A schematic is provided to automatically update `main.server.ts` files to pass the `BootstrapContext` to the `bootstrapApplication` call.

In addition, `getPlatform()` and `destroyPlatform()` will now return `null` and be a no-op respectively when running in a server environment.

PR Close #63562
2025-09-09 10:57:09 -07:00
Alan Agius
49d5de4233 refactor(platform-server): simplify transfer state serialization tracking (#63525)
The logic to track if the transfer state has been serialized is simplified by removing the need for `APP_ID`. Instead of maintaining a `Set` of application IDs, a simple boolean flag is used.

PR Close #63525
2025-09-03 09:15:52 -07:00
Alan Agius
ee73dc9553 fix(platform-server): prevent false warning for duplicate state serialization (#63525)
The `TRANSFER_STATE_SERIALIZED_FOR_APPID` provider was previously configured at the platform level, causing its state to be shared across all concurrent server-side rendering requests. This created a race condition where one request could see the `appId` from a different, concurrent request, leading to false warnings about duplicate state serialization.

This commit changes the provider's scope to ensure that each application instance gets its own unique state. This correctly isolates the serialization check to each individual request, resolving the issue of false warnings in concurrent environments.

Closes #63524

PR Close #63525
2025-09-03 09:15:52 -07:00
Paul Gschwendtner
3a106a35bc build: move private testing helpers outside platform-browser/testing (#61472)
These helpers are often imported by various tests throughout the
repository, but the helpers aren't exported/exposed from the public
entry-point; even though they confusingly reside in there.

This commit fixes this, and moves the helpers into
`packages/private/testing`. This is a preparation for the `ts_project`
migration where we don't want to leverage deep imports between packages.

PR Close #61472
2025-05-20 10:00:43 +00:00
Kristiyan Kostadinov
fb65ad157c perf(platform-server): speed up resolution of base (#61392)
The `getBaseHref` method is called several times per request and currently queries through the entire document. We can speed it up by taking advantage of the fact that the `<base>` can only be a direct child of the `<head>` and is usually defined towards the beginning. Below are some benchmarks for a "Hello world" app before and after this change.

### Before:
```
Running 60s test @ http://localhost:4202
100 connections with 10 pipelining factor

┌─────────┬────────┬────────┬────────┬────────┬───────────┬──────────┬─────────┐
│ Stat    │ 2.5%   │ 50%    │ 97.5%  │ 99%    │ Avg       │ Stdev    │ Max     │
├─────────┼────────┼────────┼────────┼────────┼───────────┼──────────┼─────────┤
│ Latency │ 568 ms │ 853 ms │ 901 ms │ 904 ms │ 866.58 ms │ 437.6 ms │ 9915 ms │
└─────────┴────────┴────────┴────────┴────────┴───────────┴──────────┴─────────┘
┌───────────┬─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬─────────┐
│ Stat      │ 1%      │ 2.5%    │ 50%     │ 97.5%   │ Avg     │ Stdev   │ Min     │
├───────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Req/Sec   │ 490     │ 826     │ 1,006   │ 1,643   │ 1,129.3 │ 234.69  │ 490     │
├───────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Bytes/Sec │ 10.4 MB │ 17.4 MB │ 21.3 MB │ 34.7 MB │ 23.9 MB │ 4.96 MB │ 10.3 MB │
└───────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┘

Req/Bytes counts sampled once per second.
# of samples: 60

69k requests in 60.04s, 1.43 GB read
90 errors (90 timeouts)
```

### After

```
Running 60s test @ http://localhost:4202
100 connections with 10 pipelining factor

┌─────────┬────────┬────────┬────────┬─────────┬───────────┬───────────┬─────────┐
│ Stat    │ 2.5%   │ 50%    │ 97.5%  │ 99%     │ Avg       │ Stdev     │ Max     │
├─────────┼────────┼────────┼────────┼─────────┼───────────┼───────────┼─────────┤
│ Latency │ 471 ms │ 831 ms │ 889 ms │ 1668 ms │ 835.91 ms │ 467.89 ms │ 9720 ms │
└─────────┴────────┴────────┴────────┴─────────┴───────────┴───────────┴─────────┘
┌───────────┬─────────┬─────────┬─────────┬─────────┬──────────┬────────┬─────────┐
│ Stat      │ 1%      │ 2.5%    │ 50%     │ 97.5%   │ Avg      │ Stdev  │ Min     │
├───────────┼─────────┼─────────┼─────────┼─────────┼──────────┼────────┼─────────┤
│ Req/Sec   │ 390     │ 860     │ 1,145   │ 1,572   │ 1,156.77 │ 222.65 │ 390     │
├───────────┼─────────┼─────────┼─────────┼─────────┼──────────┼────────┼─────────┤
│ Bytes/Sec │ 8.24 MB │ 18.2 MB │ 24.2 MB │ 33.2 MB │ 24.4 MB  │ 4.7 MB │ 8.24 MB │
└───────────┴─────────┴─────────┴─────────┴─────────┴──────────┴────────┴─────────┘

Req/Bytes counts sampled once per second.
# of samples: 60

71k requests in 60.03s, 1.47 GB read
140 errors (140 timeouts)
```

PR Close #61392
2025-05-16 09:03:38 +00:00
Jan Martin
06d6da345f fix(platform-server): less aggressive ngServerMode cleanup (#61106)
Other code may depend on `ngServerMode` and it might have been set
globally / via a bundler. Forcing it to `undefined` in those situations
can lead to hard debug issues where the only symptom is that "suddenly"
browser-specific code paths run on the server and (obviously) break.

PR Close #61106
2025-05-06 09:08:47 -07:00
Alan Agius
bf5d995caa refactor(platform-server): expose internal render method for extensibility (#60416)
The `renderApplication` and `renderModule` methods currently encapsulate the entire rendering process, making it difficult to intercept key phases from a non-Angular context. This change exports the internal `render` method, allowing us to perform operations such as:

- Flushing headers before hydration preparation
- Handling non static redirects (e.g., 302 responses)
- Intercepting router events for additional processing

This refactor serves as an experimental step toward improving the API for better customization and integration in the future.

PR Close #60416
2025-03-18 10:31:44 +01:00
Kristiyan Kostadinov
fc5d187da5 fix(platform-server): decouple server from animations module (#59762)
Removes the hard dependency between `platform-server` and `platform-browser/animations` since now the animations module will disable itself automatically.

PR Close #59762
2025-01-29 10:52:18 -08:00
kirjs
300b141cc8 fix(platform-server): Warn user when transfer state happens more than once (#58935)
This can happen if server providers are provided more than twice. We detect it on the state transfer phase by flagging app id as transferred in a set.

Resolves #58531

PR Close #58935
2024-12-12 14:47:28 -08:00
Kristiyan Kostadinov
d010e11b73 feat(core): add event listener options to renderer (#59092)
Updates the `Renderer2.listen` signature to accept event options, as well as all adjacent types to it.

PR Close #59092
2024-12-10 13:39:47 -08:00
hawkgs
0513fbc9fc docs: set syntax highlighting of code examples MD code blocks (#59026)
Set the syntax highlighting based on the code examples' language.

PR Close #59026
2024-12-04 17:30:28 +01:00
Andrew Kushnir
7f6f5f95ea refactor(core): use ApplicationRef.whenStable instead of a custom util function (#58834)
This commit removes a custom `whenStable` util in favor of standard `ApplicationRef.whenStable` API.

There is also an important different between the custom `whenStable` function and `ApplicationRef.whenStable` implementation: the `whenStable` was caching the "stable" promise on per-ApplicationRef basis, which resulted in unexpected behavior with zoneless, when some code ended up getting a stale resolved promise, when an application was not stable yet, this causing order of operations issues. This commit also has an extra test that covers that case.

PR Close #58834
2024-11-25 15:25:10 +00:00
Alan Agius
378284fa08 refactor(core): introduce ngServerMode as global (#58386)
This commit adds the `ngServerMode` as global, which allows for the tree-shaking of server-only code from the bundles. When this flag is unset at runtime, server-specific code will be excluded by Closure, optimizing bundle size.

**Internal Angular Flag:** This is an internal Angular flag (not a public API), avoid relying on it in application code.

PR Close #58386
2024-10-30 10:13:28 -07:00
Krzysztof Platis
9e82559de4 fix(platform-server): destroy PlatformRef when error happens during the bootstrap() phase (#58112)
The `bootstrap()` phase might fail e.g. due to an rejected promise in some `APP_INIIALIZER`.
If `PlatformRef` is not destroyed, then the main app's injector is not destroyed and therefore `ngOnDestroy` hooks of singleton services is not called on the end (failure) of SSR.

This could lead to possible memory leaks in custom SSR apps, if their singleton services' `ngOnDestroy` hooks contained an important teardown logic (e.g. unsubscribing from RxJS observable).

Note: I needed to fix by the way another thing too: now we destroy `moduleRef` when `platformInjector` is destroyed - by setting a `PLATFORM_DESTROY_LISTENER`

fixes #58111

PR Close #58112
2024-10-09 12:12:34 +00:00
Matthieu Riegler
84b6896956 refactor(platform-server): Add an ssr benchmark setup. (#57647)
In order to investigate the performances of SSR, this commit introduces a benchmark suite which will measure several step of the rendering.

PR Close #57647
2024-10-04 10:45:22 -07:00
Matthieu Riegler
f2bea3b73d refactor(platform-server): remove HttpClientModule import (#58016)
This import is not necessary anymore.

fixes #57983

PR Close #58016
2024-09-30 13:41:48 -07:00
Joey Perrott
9dbe6fc18b refactor: update license text to point to angular.dev (#57901)
Update license text to point to angular.dev instead of angular.io

PR Close #57901
2024-09-24 15:33:00 +02:00
Tom Wilkinson
d011c9f7ba refactor(core): Refactor EarlyEventContract to prepare for using it as a container. (#56900)
This is the first step towards combining `EarlyEventContract` and `EventContract`. It contains a few refactors, such as making names more consistent.

The goal of this refactor is to remove the `EarlyEventContract` class altogether, as well as `EventContract`.

To install the early event contract with the default events in early script tag, users will call:

`bootstrapGlobalEarlyEventContract()`

And for boostraping:

`registerGlobalDispatcher(dispatcher)`

PR Close #56900
2024-07-10 13:53:03 -07:00
Matthieu Riegler
31b235b94b refactor(platform-server): Add private profiler. (#56274)
The commit adds a private profiler to investigate SSR performance.

PR Close #56274
2024-07-09 12:47:45 +02:00
Thomas Nguyen
690b0fb9af refactor(core): Create event types that are able to be serialized, captured, or are mouse events. (#55799)
Use these constants across jsaction and Angular.

PR Close #55799
2024-05-23 16:10:47 +02:00
Thomas Nguyen
629b255ddb refactor(core): Add four tests and fix code to make tests pass. (#55747)
The first test asserts that bubbling does not work right now.

The second asserts that stopPropagation works, which should pass when test #1 passes too.

The third test asserts properties about the events passed to the event handler.

THe fourth test asserts that mouse events do not translate to jsaction nor help emit the jsaction binary. This required a change in code to make this pass.

PR Close #55747
2024-05-14 09:38:43 -07:00
iteriani
28fb385eec refactor(core): Use early event contract instead of the event contract in bootstrap. (#55587)
This also fixes an existing bug where we erase the jsaction attribute too early.

Now the event contract binary is 608 bytes :D.

PR Close #55587
2024-05-09 14:34:10 -07:00
Alan Agius
cf84acf44c fix(platform-server): remove event dispatch script from HTML when hydration is disabled (#55681)
Prior to this commit, the included event dispatcher remained in the HTML even when hydration was disabled.

PR Close #55681
2024-05-06 12:33:09 -07:00
Alan Agius
cf2e1b3e5d refactor(platform-server): add link that explains nonce application/json (#55505)
Add a link to the specs explaining why nonce is not needed for `application/json`.

PR Close #55505
2024-04-24 10:48:38 -07:00
Andrew Kushnir
307bc1d6b6 refactor(platform-server): event contract script should follow event dispatch script (#55502)
This commit fixes an issue where event contract init script was injected into the page before the inlined event dispatch script. That resulted in runtime exceptions, since event contract relies on some code being present on a page already.

PR Close #55502
2024-04-24 10:32:36 -07:00
Alan Agius
5674c644ab fix(platform-server): add nonce attribute to event record script (#55495)
This commit fixes an issue where the nonce attribute was not added when `CSP_NONCE` token was provided.

PR Close #55495
2024-04-23 13:12:47 -07:00
Andrew Kushnir
ba0036c5c3 refactor(platform-server): remove inlined JSAction script when no events to replay (#55428)
JSAction script is inlined into the HTML by the build process to avoid extra blocking request. The script looks like this:

```
<script type="text/javascript" id="ng-event-dispatch-contract">...</script>
```

This commit updates the logic to remove JSAction if event replay feature is disabled or if there are no events to replay.

PR Close #55428
2024-04-22 11:36:21 -07:00
Joey Perrott
b1dffa4abe refactor: migrate platform-* to prettier formatting (#55423)
Migrate formatting to prettier for platform-* from clang-format

PR Close #55423
2024-04-19 13:49:24 -07:00
Thomas Nguyen
a730f09ae9 feat(core): Add a public API to establish events to be replayed and an attribute to mark an element with an event handler. (#55356)
These will be consumed by the event-dispatch contract to replay events. The contract and the dispatcher inclusion will be in followups.

PR Close #55356
2024-04-19 13:12:21 -07:00
Alan Agius
2357d3566c refactor(platform-server): remove legacy URL handling logic (#54874)
BREAKING CHANGE:

Legacy handling or Node.js URL parsing has been removed from `ServerPlatformLocation`.

The main differences are;
  - `pathname` is always suffixed with a `/`.
  - `port` is empty when `http:` protocol and port in url is `80`
 - `port` is empty when `https:` protocol and port in url is `443`

PR Close #54874
2024-03-18 16:37:07 -07:00
Alan Agius
3b1967ca64 refactor(platform-server): remove deprecated useAbsoluteUrl and baseUrl from PlatformConfig (#54874)
BREAKING CHANGE: deprecated `useAbsoluteUrl` and `baseUrl` been removed from `PlatformConfig`. Provide and absolute `url` instead.

PR Close #54874
2024-03-18 16:37:07 -07:00
Alan Agius
07ac017731 refactor(platform-server): remove deprecated platformDynamicServer API (#54874)
BREAKING CHANGE: deprecated `platformDynamicServer` has been removed. Add an `import @angular/compiler` and replace the usage with `platformServer`

PR Close #54874
2024-03-18 16:37:06 -07:00
Alan Agius
e8b588d8b7 refactor(platform-server): remove deprecated ServerTransferStateModule API (#54874)
BREAKING CHANGE: deprecated `ServerTransferStateModule` has been removed. `TransferState` can be use without providing this module.

PR Close #54874
2024-03-18 16:37:06 -07:00
Alan Agius
dcc4a80615 refactor(platform-server): replace global usages with globalThis (#52342)
This commit updates a number of `global` usages to `globalThis` to better support other platforms like CF workers

PR Close #52342
2023-10-24 09:26:50 -07:00
Alan Agius
6b112cc621 refactor(platform-server): remove dependency between @angular/platform-server and @angular/platform-server/init (#52342)
Previously, the cross entry-point dependency was created to share the `setDomTypes` function. This however, causes extra transformations "linking" during the application build since other Angular code in `@angular/platform-server` is pulled in.

With this commit we remove the cross dependency and thus remove the need for extra transformations in the server polyfill bundle with the result of having a slightly faster build.

See: https://github.com/angular/angular-cli/pull/26113

PR Close #52342
2023-10-24 09:26:50 -07:00
Alan Agius
405ec8c796 fix(platform-server): resolve relative requests URL (#52326)
Prior to this commit relative HTTP requests were not being resolved to absolute even thought the behaviour is documented in https://angular.io/guide/universal#using-absolute-urls-for-http-data-requests-on-the-server.

This caused relative HTTP requests to fail when done on the server because of missing request context. This change is also required to eventually support HTTP requests handled during prerendering (SSG).

Closes #51626

PR Close #52326
2023-10-23 12:02:21 -07:00
Andrew Kushnir
d10f2b29d7 refactor(core): use performance API for hydration-related features (#52288)
This commit adds a standard performance marker that can be viewed in Chrome dev tools and other tooling. See more info at https://developer.mozilla.org/en-US/docs/Web/API/Performance/mark

PR Close #52288
2023-10-23 09:29:36 -07:00
Alan Agius
7978b408df refactor(platform-server): replace Node.js URL API usage (#52231)
This commit replace the usage of URL node API to WHATWG URL API.

See: https://nodejs.org/api/url.html#the-whatwg-url-api

PR Close #52231
2023-10-19 09:26:44 -07:00
Andrew Kushnir
194c1719ab docs: promote renderApplication to stable (#52197)
This commit removes the `@developerPreview` annotation from the `renderApplication`, promoting it to stable.

PR Close #52197
2023-10-18 18:21:48 +02:00
Alan Agius
8413b64a6b refactor(core): add whenStable private API (#51807)
Prior to this change `this.isStable.pipe(first((isStable) => isStable)).toPromise()` had to be done in multiple places across the framework and the Angular CLI see https://github.com/angular/angular-cli/pull/25856#discussion_r1328158846. In the majority of cases an Observable based `isStable` API is not needed. This also removes the need for RXJS operator imports.

PR Close #51807
2023-09-27 10:31:56 -07:00
Matthieu Riegler
2df83e2253 refactor(platform-server): Use the Writable type when overwriting readonly properties. (#49754)
The `Writable` type is usefull when we want overwrite readonly properties and we still want to maintain code navigation/reference. It should be use instead of `any` type assertions for example.

PR Close #49754
2023-09-22 10:02:13 -07:00
Matthieu Riegler
c5daa6ce77 feat(platform-browser): expose EventManagerPlugin in the public API. (#49969)
The exposed type of the `EVENT_MANAGER_PLUGINS` token should be in the public API.

PR Close #49969
2023-08-29 16:40:06 +00:00
Jeremy Mowery
be3edad60e refactor: add readonly to public InjectionToken types (#51125)
We enabled a lint rule internally to require that multi-provided
`InjectionToken`s have a `readonly` array type, the tokens in this
PR do not follow this rule and are causing lint violations.

Fixes #51124

PR Close #51125
2023-08-14 17:17:35 -07:00
Matthieu Riegler
0a38dc3c26 refactor(core): throw an error when hydration marker is missing from DOM (#51170)
non-destructive hydration expects the DOM tree to have the same structure in both places.
With this commit, the app will throw an error if comments are stripped out by the http server (eg by some CDNs).

fixes #51160

PR Close #51170
2023-08-04 11:31:49 -04:00
Alan Agius
4550fe42f7 refactor: use queueMicrotask to schedule micro tasks instead of various helpers (#50485)
`queueMicrotask` is an API which is supported by all browser and Node.js versions.

PR Close #50485
2023-06-15 16:38:21 +02:00