Commit graph

175 commits

Author SHA1 Message Date
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
Matthieu Riegler
f199305db6 docs(platform-server): Make links out of @see tags (#50110)
This commit is part of the work for angular#50097 to improve the linking on the online documentation.

PR Close #50110
2023-06-14 10:54:39 +02:00
Alan Agius
0875b519b9 fix(platform-server): surface errors during rendering (#50587)
Prior to this change in some cases errors tht happen during routing were not being surfaced. This is due to the fact that the router has floating promises, and the platform was being destroyed prior to these being settled.

PR Close #50587
2023-06-07 12:44:08 -07:00
Alan Agius
28c68f709c fix(core): update ApplicationRef.isStable to account for rendering pending tasks (#50425)
This commit updates the `ApplicationRef.isStable` API to account for
pending rendering task. This is needed as once a pending rendering task
is done, new macrotask and microtask could be created which previously caused these not
to be intercepted and thus ignored when doing SSR.

PR Close #50425
2023-05-30 12:58:22 -07:00
Andrew Kushnir
3a05e4cca1 refactor(platform-server): exclude provideHttpClient() call from provideServerRendering() (#50459)
Previously, we've used to have server-specific logic for HttpClient, so there was a need to re-provide it with the right config. Since v16, that logic was refactored and there is no need to re-provide HttpClient anymore. The code in the `provideServerRendering()` was retained for historical reasons and it makes application configuration more difficult, because it forces developers to copy HttpClient config into server config as well (otherwise it would not be take into account).

This commit removes the `provideHttpClient()` call from the `provideServerRendering()` function. This is **not** a breaking change, since we've also merge browser application config in the `app.config.server.ts`, so if an application is configured to use HttpClient, it will continue to work on both client and server sides.

Resolves #50454.

PR Close #50459
2023-05-25 14:41:34 +00:00
Alan Agius
02437224f5 refactor(core): simplify state transfer escaping (#50201)
This commit removes unnecessary transfer state escaping and updates this process to be done by the means of a `replacer` and `reviver` method as this removes the need to export the escaping and unescaping methods.

The only thing that we need to escape is `<script` and `</script` which are done by the browsers, but not Node.js.

PR Close #50201
2023-05-10 11:31:34 -07:00
Andrew Kushnir
59837f4749 refactor(platform-server): import xhr2 dynamically in the ServerXhr class (#50095)
This commit updates the `@angular/common/http` and `@angular/platform-server` packages to allow dynamic import of the `xhr2` dependency. The `xhr2` dependency has side-effects that rely on a global scope and as a result in some environments those side-effectful calls fail. With the changes from this PR, the import is delayed until it's actually needed, which gives a chance for the underlying platform to setup global scope (via shims) as needed.

Co-authored-by: alan-agius4 <17563226+alan-agius4@users.noreply.github.com>

PR Close #50095
2023-05-08 14:32:43 -07:00
Alan Agius
06452af31f fix(platform-server): remove dependency on @angular/platform-browser-dynamic (#50064)
Using `@angular/platform-browser-dynamic` is no longer required for JIT scenarios with Ivy. Instead `@angular/compiler` should be imported instead.

This change is part of the effort to reduce the server bundles sizes, which is needed to support cloud workers.

BREAKING CHANGE:
Users that are using SSR with JIT mode will now need to add  `import to @angular/compiler` before bootstrapping the application.

**NOTE:** this does not effect users using the Angular CLI.

PR Close #50064
2023-05-01 07:50:03 -07:00
Andrew Kushnir
744f7ffb61 refactor(platform-server): avoid using platformDynamicServer when ngJitMode is disabled (#50007)
This commit refactors the logic of the `platform-server` to avoid using `platformDynamicServer` when `ngJitMode` is disabled. This is needed to avoid including JIT compiler into the source code of the server bundle, when this compil
er is not needed.

PR Close #50007
2023-04-26 09:23:52 -07:00
Andrew Kushnir
96a4e74069 refactor(platform-server): refactor internal _render function (#49851)
The change in 8c3b92cfb3 inspired a followup refactor where the `_render` method can just accept a PlatformRef and an ApplicationRef instances directly.

PR Close #49851
2023-04-17 13:34:00 +00:00
Alan Agius
8c3b92cfb3 refactor(platform-server): simplify _render method (#49840)
This commits simplifies the `_render` method by using async/await.

PR Close #49840
2023-04-14 15:28:41 -04:00
Andrew Kushnir
54f18c832d refactor(core): rename internal DI token that indicates whether hydration is enabled (#49800)
This commit renames an internal token to better align it with the naming of the function (to highlight the fact that it's responsible for DOM part of the hydration).

PR Close #49800
2023-04-13 14:02:33 +00:00
Andrew Kushnir
7ee542d263 refactor(platform-server): include info about enabled features into ng-server-context (#49773)
This commit updates the logic that adds the "ng-server-context" attribute to the root elements to also include information about SSR feature enabled got an application.

PR Close #49773
2023-04-11 12:46:09 -07:00
Alan Agius
4f3abbcf93 refactor(platform-server): remove ServerRendererFactory2 and EmulatedEncapsulationServerRenderer2 (#49630)
These methods are no longer required following the fixes in https://github.com/angular/domino/pull/14/

PR Close #49630
2023-04-05 11:42:57 -07:00
Alan Agius
7870fb07fe feat(platform-server): rename provideServerSupport to provideServerRendering (#49678)
This commit renames `provideServerSupport` to `provideServerRendering`.

PR Close #49678
2023-04-03 15:00:13 -07:00
Alan Agius
2703fd6260 refactor: remove deprecated EventManager method addGlobalEventListener (#49645)
This commit removed the deprecated `EventManager` method  `addGlobalEventListener`.

BREAKING CHANGE: Deprecated `EventManager` method `addGlobalEventListener` has been removed as it is not used by Ivy.

PR Close #49645
2023-03-30 09:47:54 -07:00
Alan Agius
c024574f46 feat(core): expose makeStateKey, StateKey and TransferState (#49563)
This commits adds `makeStateKey`, `StateKey` and  `TransferState` methods in `@angular/core` as public API and deprecated the same exported symbols in `@angular/platform-browser`.

DEPRECATED:  `makeStateKey`, `StateKey` and  `TransferState` exports have been moved from `@angular/platform-browser` to `@angular/core`. Please update the imports.

```diff
- import {makeStateKey, StateKey, TransferState} from '@angular/platform-browser';
+ import {makeStateKey, StateKey, TransferState} from '@angular/core';
```

PR Close #49563
2023-03-29 10:18:02 -07:00
Alan Agius
e99460865e refactor(platform-server): deprecate useAbsoluteUrl and baseUrl (#49546)
These two options where created for a feature which was never completed. https://github.com/angular/universal/pull/1860

Eventually these options should be added to `withTransferCache` HTTP logic.

DEPRECATED: `PlatformConfig.baseUrl` and `PlatformConfig.useAbsoluteUrl` platform-server config options  are deprecated as these were not used.

PR Close #49546
2023-03-29 09:13:41 -07:00
Alan Agius
45a6ac09fd fix(http): force macro task creation during HTTP request (#49546)
This commit adds a background macrotask when an XHR request is performed. The macrotask is started during `loadstart` and ended during `loadend` event.

The macrotask is needed so that the application is not stabilized during HTTP calls. This is important for server rendering, as the application is rendering when the application is stabilized.
The application is stabilized when there are no longer pending Macro and Micro tasks intercepted by Zone.js, Since an XHR request is none of these, we create a background macrotask so that Zone.js is
made aware that there is something pending.

Prior to this change, we patched the `HttpHandler` in `@angular/platform-server` but this is not enough, as there can be multiple `HttpHandler` in an application, example when importing `HttpClient` in a lazy loaded component/module.
Which causes a new unpatched instance of `HttpHandler` to be created in the child injector which is not intercepted by Zone.js and thus the application is stabalized and rendered before the XHR request is finalized.

NB: Zone.js is fundamental for SSR and currently, it's not possible to do SSR without it.

Closes: #49425

PR Close #49546
2023-03-29 09:13:41 -07:00
Andrew Kushnir
478c5ac150 refactor(core): internal tracker of pending tasks during initial rendering (#49576)
This commit implements a simple tracker of the pending tasks during initial rendering. The class allows adding and removing tasks from the set. The class also exposes a promise that gets resolved once the last task is removed.

This tracker is needed to keep track of ongoing processes like Router navigation (and potentially HTTP requests) and acts as a signaling mechanism to SSR and hydration that the application is in the "stable" state and a serialization can be performed.

This class would also act as a future replacement for the `ApplicationRef.isStable` for zoneless applications.

PR Close #49576
2023-03-28 16:19:31 -07:00
Alan Agius
41f27ad086 refactor(platform-server): remove renderApplication overload that accepts a component (#49463)
This commit removes the `renderApplication` overload that accepts a root component as the first parameter.

BREAKING CHANGE:

`renderApplication` method no longer accepts a root component as first argument. Instead, provide a bootstrapping function that returns a `Promise<ApplicationRef>`.

Before
```ts
const output: string = await renderApplication(RootComponent, options);
```

Now
```ts
const bootstrap = () => bootstrapApplication(RootComponent, appConfig);
const output: string = await renderApplication(bootstrap, options);
```

PR Close #49463
2023-03-22 16:40:31 +01:00
Alan Agius
8cf1baee2a refactor(platform-server): remove ServerStylesHost (#49424)
With the recent changes and refactors of `dom_renderer` having a separate `ServerStylesHost` is redundant.

PR Close #49424
2023-03-17 10:46:03 +01:00
Alan Agius
82d6fbb109 refactor(core): generate a static application ID (#49422)
Prior to this change, a random application ID was generated each time which forced users using server rendering to provide an application ID themselves. This was needed to handle rare cases when multiple Angular applications are rendered on the same page.

With this change the application ID is no longer generated randomly and instead it is hard coded.

BREAKING CHANGE:

The `APP_ID` token value is no longer randomly generated. If you are bootstrapping multiple application on the same page you will need to set to provide the `APP_ID` yourself.

```ts
bootstrapApplication(ComponentA, {
  providers: [
   { provide: APP_ID, useValue: 'app-a' },
   // ... other providers ...
  ]
});
```

PR Close #49422
2023-03-15 17:08:17 -07:00
Alan Agius
056d68002f feat(platform-server): add provideServerSupport function to provide server capabilities to an application (#49380)
This commit adds the `provideServerSupport()` function that returns the `EnvironmentProviders` needed to setup server rendering without NgModules.

PR Close #49380
2023-03-14 09:23:13 -07:00
Alan Agius
86fc4d3e98 refactor(platform-browser): remove internal TRANSITION_ID token (#48253)
This was a mirror copy of the `APP_ID` token.

PR Close #48253
2023-03-09 09:33:34 -08:00
Alan Agius
9165ff2517 fix(platform-browser): reuse server generated component styles (#48253)
Prior to this change component styles generated on the server where removed prior to the client side component being rendered and attached it's own styles. In some cases this caused flickering. To mitigate this `initialNavigation: enabledBlocking'` was introduced which allowed the remove of server styles to be defer to a latter stage when the application has finished initialization.

This commit changes the need for this, by not removing the server generated component styles and reuse them for client side rendering.

PR Close #48253
2023-03-09 09:33:34 -08:00
Alan Agius
0e5f9ba6f4 fix(core): generate consistent component IDs (#48253)
Prior to this change the component IDs where generated based on a counter. This is problematic as there is no guarantee that a component will get the same ID that was assigned on the server when generated on the client side.

This is paramount to be able to re-use the component styles generated on the server.

PR Close #48253
2023-03-09 09:33:34 -08:00
Jessica Janiuk
4ae4090d3c refactor(platform-server): Implement hydration state transfer machinery (#49271)
**Important note**: this is a first commit in a series of commits that will be needed
to support non-destructive hydration. Stay tuned for further updates!

This commit lays the foundation on top of which more hydration logic will be
added in follow up PRs. This PR includes:

* Initial serialization of hydration data
* Data transfer of hydration annotations from server side to client
* Accessing hydration info and populating internal data structures
* Initial APIs (currently private) that enable hydration (in a tree-shakable manner)
* Cleanup of annotations post hydration
* Initial test infrastructure and basic test cases

This commit does **not** expose any public APIs. They'll be exposed later, when
more hydration logic is implemented to a state when it can cover most common
use-cases.

Co-authored-by: Jessica Janiuk <jessicajaniuk@google.com>
Co-authored-by: Andrew Kushnir <akushnir@google.com>

PR Close #49271
2023-03-06 16:53:37 +00:00
Alan Agius
b5278cc115 feat(platform-server): renderApplication now accepts a bootstrapping method (#49248)
The `renderApplication` now also accepts a bootstrapping function call with return `Promise<ApplicationRef>`as the first parameter.

Example:
```ts
 const bootstrap = () => bootstrapApplication(RootComponent, appConfig);

 const output: string = await renderApplication(bootstrap);
```

PR Close #49248
2023-03-01 22:31:33 +00:00
Alan Agius
17abe6dc96 refactor(platform-server): remove deprecated renderModuleFactory (#49247)
The deprecated `renderModuleFactory` has been removed as it is no longer necessary with Ivy.

BREAKING CHANGE: `renderModuleFactory` has been removed. Use `renderModule` instead.

PR Close #49247
2023-03-01 12:32:32 -08:00
Jessica Janiuk
a08a8ff108 fix(platform-server): bundle @angular/domino in via esbuild (#49229)
This removes one line that should effectively bundle the
angular fork of domino in via esbuild.

PR Close #49229
2023-02-28 13:34:50 -08:00
Andrew Kushnir
a5dc9960dc refactor(core): move TransferState implementation to core (#49222)
This commit moves the `TransferState` class implementation to `@angular/core`. This class will be needed in core in followup changes and `core` can not depend on `platform-browser` package.

The `core` package does **not** export those symbols into public API surface.
The `platform-browser` package re-exports the symbols under same names for backwards-compatibility.

PR Close #49222
2023-02-27 13:08:40 -08:00
Alan Agius
9b9c818f99 perf(core): change RendererType2.styles to accept a only a flat array (#49072)
While unlikely, prior to this change it was possible to provide a nested array of styles to the render. This required the framework to handle this by doing a flatten operation. This change also renames the `flattenStyles` method as it no longer flattens the styles.

BREAKING CHANGE: `RendererType2.styles` no longer accepts a nested arrays.

Closes #48317

PR Close #49072
2023-02-17 11:11:48 -08:00
Alan Agius
2d51b5f644 Revert "fix(platform-server): insert transfer state script before other script tags (#48868)" (#49112)
This reverts commit 2fc5b70fce as this change is no longer needed since `type=module` script are deferred by default. Which causes the transfer data to be queried after the browser has finished parsing the DOM.

PR Close #49112
2023-02-17 11:09:21 -08:00
Andrew Kushnir
9105c41f44 fix(platform-server): avoid duplicate TransferState info after renderApplication call (#49094)
This commit updates the `renderApplication` function to avoid duplicate serialization of the data from the `TransferState` class. The `renderApplication` function imports providers from the `ServerModule`, which already include the `TransferState` serialization providers, but the same providers were also included inside the `renderApplication` as well, which led to the duplication.

PR Close #49094
2023-02-16 16:00:29 -08:00
Alan Agius
02d5e8d79d fix(platform-browser): remove styles from DOM of destroyed components (#48298)
Currently style of components using `encapsulation`, `None` or `Emulated` will not be removed from the DOM once the component get destroyed.

This change addresses this by keeping track of the number of times a component is rendered, when the component is destroyed the counter is decreased and once this reaches zero the style element is removed from the DOM.

Currently, this new behaviour is on opt-in bases, but it will be changed in the next major version.

To opt-in, set the `REMOVE_STYLES_ON_COMPONENT_DESTROY` DI token to `true`.

Example
```ts
@NgModule({
 declarations: [
   AppComponent,
 ],
 imports: [
   BrowserModule
 ],
 providers: [
   { provide: REMOVE_STYLES_ON_COMPONENT_DESTROY, useValue: true }
 ],
 bootstrap: [AppComponent]
})
export class AppModule { }
```

Closes #16670

PR Close #48298
2023-02-16 15:31:24 -08:00
Alan Agius
2fc5b70fce fix(platform-server): insert transfer state script before other script tags (#48868)
Previously, the state `script` was always appended as the last item in the `body` tag. This can result in the state not being available when the Angular application is bootstrap.
A workaround for this was to delay the bootstrapping of the application until by using the `DOMContentLoaded` event listener.

```ts
const bootstrap = () => platformBrowserDynamic().bootstrapModule(AppModule);

document.addEventListener('DOMContentLoaded', bootstrap);
```

With this change the above workaround is no longer necessary as the state `script` tag is now added prior of any other `script` which guarantees that the state is present prior of the Angular application is bootstrapped.

PR Close #48868
2023-01-30 16:54:07 -08:00
Paul Gschwendtner
211d178f0d refactor(platform-server): use proper ESM-compatible import to domino (#48521)
Now that everything runs from within ESM, we need to use the proper
default-import variant to be able to load `domino` from ESM.

PR Close #48521
2022-12-19 19:50:41 +00:00
Alan Agius
4f7456c71d refactor(platform-browser): simplify flattenStyles method (#48293)
With this change we simplify the `flattenStyles` logic using modern JavaScript.

PR Close #48293
2022-12-02 11:36:13 -08:00