Commit graph

152 commits

Author SHA1 Message Date
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
Alan Agius
ad483007b4 refactor(platform-server): remove Injector dependency for initDominoAdapter factory (#48231)
The injector is not used by this factory.

PR Close #48231
2022-11-28 09:12:35 -08:00
Alan Agius
790ee17e80 fix(platform-server): call onSerialize when state is empty (#47888)
Commit a0b2d36415 (diff-3975e0ee5aa3e06ecbcd76f5fa5134612f7fd2e6802ca7d370973bd410aab55cR25-R31) changed the serialization phase logic so
that when the state is empty the script tag is not added to the document. As a side effect, this caused the `toJson` not called which caused the `onSerialize` callbacks also not to be called.
These callbacks are used to provide the value for a key when `toJson` is called. Example: https://github.com/ngrx/platform/issues/101#issuecomment-351998548

Closes #47172

PR Close #47888
2022-10-28 02:27:56 -07:00
Kristiyan Kostadinov
54e32dad3e fix(platform-server): align server renderer interface with base renderer (#47868)
The `ServerRenderer` wasn't aligned with the `Renderer2` interface which meant that it was still referring to the old `debugInfo` parameters. It also wasn't implementing the `preserveContent` argument of `selectRootElement` which can lead to incosistencies between the server and the client.

Fixes #47844.

PR Close #47868
2022-10-25 14:51:46 +02:00
Alex Rickabaugh
7de1469be6 feat(core): introduce EnvironmentProviders wrapper type (#47669)
This commit introduces a new type `EnvironmentProviders` which can be used
in contexts where Angular accepted `Provider`s destined for
`EnvironmentInjector`s. This includes contexts such as `@NgModule.providers`
and `Route.providers`.

The new type is useful for preventing such providers from accidentally
ending up in `@Component.providers`. It can be used as the return type of
provider functions (such as `provideRouter`) to enforce this safety.

Because `Provider` allows `any[]` nested arrays, the compile-time safety
provided by `EnvironmentProviders` is easily circumvented. However, the
runtime shape of `EnvironmentProviders` is not compatible with component
injectors and will result in a runtime error if it leaks through (NG0207).

A new function `makeEnvironmentProviders` is used to construct this new type
from an array of providers.

The existing `importProvidersFrom` operation previously returned a very
similar type `ImportedNgModuleProviders` which had the same goal. This
machinery is switched over to use the new `EnvironmentProviders` interface
instead (in fact, `ImportedNgModuleProviders` is now just an alias to
`EnvironmentProviders`).

PR Close #47669
2022-10-07 14:03:13 -07:00
Alex Rickabaugh
d7683a611a refactor(http): introduce the functional interceptors API (#47502)
This commit introduces new types and symbols related to functional HTTP
interceptors - interceptors which are plain functions with access to DI via
the `inject()` operation.

This new form of interceptor is not exposed publically in this commit, but
the legacy class-based interceptors are refactored to be built on top of the
new API internally.

PR Close #47502
2022-10-06 19:02:45 +00:00
Andrew Kushnir
6c7cd6ba64 refactor(platform-server): add a marker to specify how a page was rendered (#47103)
This commit updates the `renderApplication`, `renderModule` and `renderModuleFactory` functions to append a special marker (in a form of an attribute, called `ng-server-context`) to the component host elements. This marker is needed to analyze how a page was rendered.

PR Close #47103
2022-09-12 19:05:48 -07:00
Andrew Kushnir
2b4d7f6733 feat(platform-server): support document reference in render functions (#47032)
This commit updates the `renderModule` and `renderApplication` functions to also accept a document reference (in addition to the serialized document contents as a string). This should provide the necessary flexibility to NgUniversal (and other API consumers) to structure the logic to avoid serializing and parsing document multiple times during the SSR request.

PR Close #47032
2022-08-04 13:37:54 -07:00
Andrew Kushnir
07606e3181 feat(platform-browser): add isEmpty method to the TransferState class (#46915)
This commit adds the `isEmpty` method to the `TransferState` class to make it possible to check whether the state is empty or not. This is helpful in situations when the `TransferState` should be serialized and the content is transferred to the client (if the state is empty - certain operations can be omitted).

PR Close #46915
2022-07-22 09:17:47 +00:00
Andrew Kushnir
46c44de0cc refactor(platform-server): include TransferState providers into ServerModule (#46899)
This commit updates the code to include the TransferState providers (used for serialization) into the `ServerModule` instead of having the need to import the `ServerTransferStateModule` separately.

The list of providers in the `ServerTransferStateModule` is now empty and importing it is a noop. This is not a breaking change, since the `ServerModule` must be included anyways to make server rendering work correctly.

PR Close #46899
2022-07-21 12:17:43 +00:00
Andrew Kushnir
a0b2d36415 refactor(platform-server): make TransferState standalone-friendly (#46879)
This commit updates the `TransferState` to make it `providedIn: 'root'`. This makes the entire `BrowserTransferStateModule` module unnecessary, so it got deprecated as well.

The `ServerTransferStateModule` is still retained, but the `renderApplication` function now also includes the necessary tokens to serialize the `TransferState` automatically, so when using the `renderApplication` function, there is no need to include `ServerTransferStateModule` as well.

This change is a part of the ongoing efforts to update the shape of the FW APIs to make them standalone-friendly (so there is no need to import any NgModules).

PR Close #46879
2022-07-20 11:05:08 -07:00
Pawel Kozlowski
4b377d3a6d feat(core): introduce createApplication API (#46475)
The `createApplication` function makes it possible to create an
application instance (represented by the `ApplicationRef`)
without bootstrapping any components. It is useful in the
situations where ones wants to decouple and delay components
rendering and / or render multiple root components in one
application. Angular elements can use this API to create
custom element types with an environment linked to a
created application.

PR Close #46475
2022-07-18 18:20:27 +00:00
Pawel Kozlowski
71e606d3c3 feat(core): expose EnvironmentInjector on ApplicationRef (#46665)
There are legitimate cases where access to an EnvironmentInjector
of a bootstrapped application is required / convenient. It will be also
required for support of standalone components with Angular elements.

PR Close #46665
2022-07-07 13:05:27 -07:00
Kristiyan Kostadinov
08d3db232c fix(platform-server): invalid style attribute being generated for null values (#46433)
Fixes that the server renderer was producing an invalid `style` attribute when a null value is passed in. Also aligns the behavior with the DOM renderer by removing the attribute when it's empty.

Fixes #46385.

PR Close #46433
2022-06-21 11:54:52 -07:00
Alex Rickabaugh
025903566e docs(core): mark the standalone APIs as Developer Preview (#46050)
This commit adds the new `@developerPreview` tag to all of the standalone
component related APIs. With this, AIO will show an API status label which
links to the documentation on Developer Preview.

PR Close #46050
2022-06-01 16:01:59 -07:00
Andrew Kushnir
c9b40b5100 refactor(core): make Testability compatible with tree-shaking (#45657)
This commit refactors the `Testability`-related logic to extract the necessary providers into a separate array, so that it can later become it's own NgModule (or exposed as an array of providers) and be excluded from the new APIs by default.

PR Close #45657
2022-05-05 11:50:23 -07:00
Andrew Kushnir
d380bb49b7 refactor(core): rename internal bootstrap function (#45896)
This commit renames an internal function that implements the core bootstrap logic. The function is exported as a private symbol (with `ɵ`), but in order to avoid any possible confusion, we include "internal" into the function name as well.

PR Close #45896
2022-05-05 10:55:56 -07:00
Alex Rickabaugh
d322052db3 refactor(core): guard against importProvidersFrom in components (#45838)
`importProvidersFrom` provides a bridge from the world of NgModule-based DI
configuration to the new, "standalone" world of direct providers and
environment injectors. Early user feedback suggested some confusion around
where this function was supposed to be used, particularly around importing
NgModule-based providers into standalone component `providers` arrays, which
is not the intended use. This confusion is exacerbated by the fact that due
to the unified `Provider` type, this kind of misconfiguration was happily
accepted by the type system.

This commit changes the return type of `importProvidersFrom` to wrap the
returned providers in an opaque type that prevents them from being used in
component provider contexts. This, together with stronger documentation
around the purpose and functionality of `importProvidersFrom`, should
address some of the above confusion.

PR Close #45838
2022-05-02 15:50:44 -07:00
Andrew Kushnir
22c71be94c fix(platform-server): update renderApplication to move appId to options (#45844)
This commit updates the `renderApplication` function to move the `appId` argument to the options object. The goal is to achieve a symmetry with the `bootstrapApplication` call (use to bootstrap apps for the browser environment).

PR Close #45844
2022-05-02 14:57:55 -07:00
Andrew Kushnir
dff5586d52 feat(platform-server): implement renderApplication function (#45785)
This commit adds the `renderApplication` function to bootstrap an Angular app using a root standalone component to support SSR scenarios.

PR Close #45785
2022-04-29 17:18:54 -04:00
Paul Gschwendtner
7a8a929da7 refactor: fix duplication in platform-server NPM package (#45405)
The platform-server init entry-point imported code from another
entry-point using a relative import. This resulted in the code to be
bundled into the `init` entry-point as well. This has no breaking
impact but resulted in a little code duplication that we should
clean up.

PR Close #45405
2022-04-21 11:09:39 -07:00
Tobias Speicher
4ddcf81e61 refactor: replace deprecated String.prototype.substr() (#45397)
.substr() is deprecated so we replace it with functions which work similarily but aren't deprecated

Signed-off-by: Tobias Speicher <rootcommander@gmail.com>

PR Close #45397
2022-03-24 11:48:09 -07:00
Alan Agius
ba0efd1f80 refactor: replace deprecated HTMLDocument with Document (#45282)
`HTMLDocument` is deprecated in favor of `Document`. This change replaces the usages of `HTMLDocument`.

See: 20c93d3b1d/lib/lib.dom.d.ts (L6370-L6376)

PR Close #45282
2022-03-08 13:12:14 -08:00
Kristiyan Kostadinov
9fa6f5a552 fix(core): incorrectly inserting elements inside <template> element (#43429)
Currently whenever we insert element we do it directly on the node using `appendChild` or `insertBefore`, however this doesn't work with `<template>` elements where the `template.content` has to be used.

These changes add a few checks to call `appendChild` and `insertBefore` on the `content` of the template.

Fixes #15557.

PR Close #43429
2022-03-08 10:22:18 -08:00